void CoroutineService::on_update(const int64_t now){ Super::on_update(now); // wakeup sleeper if(m_sleeper_table->size() > 0){ // prepare wake up list Int64Array* wake_list =0; HashIterator* it =static_cast< HashIterator* >(m_sleeper_table->iterator()); while(it->next()){ Int64* cr_id =static_cast< Int64* >(it->getKey()); Int64* expire_time =static_cast< Int64* >(it->getValue()); if(expire_time->getValue() > now){ if(!wake_list){ wake_list =SafeNew<Int64Array>(); } wake_list->push_back(cr_id->getValue()); it->remove(); } } // wake up const int64_t n =wake_list ? wake_list->size() : 0; for(int64_t i=0; i<n; ++i){ _resume_coroutine(wake_list->get(i), SafeNew<Error>(ErrorCode::TIMEOUT), 0); } } // update int64_t cr_new_id=0; m_cr_pool->go(_update, 0, cr_new_id); }
void Hash::removeIf(bool (*pfn)(Object*, Object*)){ if(!pfn) return; HashIterator* it =dynamic_cast<HashIterator*>(iterator()); while(it->next()){ if(pfn(it->getKey(), it->getValue())){ it->remove(); } } }
void CommandService::_process_timeout(const int64_t now) { /* clear timeout command */ if(m_queue_tb->size()>0) { Int64Array* ls =0; HashIterator* it =static_cast< HashIterator* >(m_queue_tb->iterator()); while(it->next()) { const int64_t who =static_cast< Int64* >(it->getKey())->getValue(); Array* queue =static_cast< Array* >(it->getValue()); while(Command* cmd =dynamic_cast< Command* >(queue->front())) { if(cmd->isTimeout(now)) { if(cmd->isProcessing()) { WARN("service %s(%lld) who %lld command %lld cancel", name(), (long long)m_id, (long long)cmd->getWho(), (long long)cmd->getCommand()); } queue->pop_front(); } else { break; } } if(Command* cmd =dynamic_cast< Command* >(queue->front())) { if(cmd->isProcessing()) { continue; } ASSERT(cmd->isInit()); if(!ls) { ls =SafeNew<Int64Array>(); } ls->push_back(who); } } const int64_t n= ls ? ls->size() : 0; for(int64_t i=0; i<n; ++i) { _process_request(ls->get(i)); } } /* clear timeout rpc */ if(m_rpc_tb->size()>0) { Array* ls =0; HashIterator* it =static_cast< HashIterator* >(m_rpc_tb->iterator()); while(it->next()) { RpcInfo* ri =static_cast< RpcInfo* >(it->getValue()); if(now >= ri->getExpireTime()) { if(!ls) { ls =SafeNew<Array>(); } ls->push_back(ri); it->remove(); } } const int64_t n =ls ? ls->size() : 0; for(int64_t i=0; i<n; ++i) { RpcInfo* ri =static_cast< RpcInfo* >(ls->get(i)); WARN("service %s(%lld) rpc %lld cancel", name(), (long long)m_id, (long long)ri->getId()); ri->timeout(); } } }
void CommandService::_optimize(const int64_t now) { if(m_queue_tb->size() > OPTIMIZE_THRESHOLD) { HashIterator* it =static_cast< HashIterator* >(m_queue_tb->iterator()); while(it->next()) { Array* queue =static_cast< Array* >(it->getValue()); if(queue->empty()) { it->remove(); } } m_queue_tb->optimize(0); } }
Int64Array* ServiceManager::_get_need_unload_service_id(){ std::lock_guard<LOCK_TYPE> guard(m_lock); if(m_unloading_service_tb->empty()){ return 0; } const int64_t now =DateTime::Now(); Int64Array* arr =SafeNew<Int64Array>(); HashIterator* it =static_cast< HashIterator* >(m_unloading_service_tb->iterator()); while(it->next()){ const int64_t id_srv =static_cast< Int64* >(it->getKey())->getValue(); const int64_t t =static_cast< Int64* >(it->getValue())->getValue(); if(now >= t){ arr->push_back(id_srv); it->remove(); } } return arr; }