示例#1
0
	void ConnectionPool::Execute()
	{
		while(!m_stopDesired)
		{
			time_t nowTime = time(NULL);
			if(!m_conf_lua_file.empty() && nowTime >= m_nextLoadTime)
			{
				time_t lastModify = JG_F::KFileUtil::GetLastModified(m_conf_lua_file.c_str());
				if(lastModify != m_lastModifiedTime)
				{
					PoolSettings::ConnectionSettingArray cpy(m_settings.m_settings);
					if(!m_settings.reload(m_conf_lua_file.c_str()))
					{
						Log(LOG_WARN, "warn: fail reload pool setting file '%s', recover", m_conf_lua_file.c_str());
						m_settings.m_settings = cpy;
					}
					m_lastModifiedTime = lastModify;
				}
				m_nextLoadTime = nowTime + 10;
			}
			if(nowTime > m_nextCheckIdleTime)
			{
				JG_S::KAutoThreadMutex mx(m_mx);
				ConnectionMap::iterator it = m_cnnMap.begin(), ite = m_cnnMap.end();
				for(; it != ite; it++)
				{
					ConnectionList tmp;
					ConnectionList& lst = it->second;
					PooledConnection* cnn;
					while(cnn = lst.pop_front())
					{
						if(cnn->isExpire(nowTime))
						{
							Log(LOG_DEBUG, "debug: %s expire, abandon", cnn->toString().c_str());
							cnn->destroy();
						}
						else
						{
							tmp.push_back(cnn);
						}
					}
					while(cnn = tmp.pop_front())
					{
						lst.push_back(cnn);
					}
				}
				m_nextCheckIdleTime = nowTime + 30;
			}
			msleep(100);
		}
	}
示例#2
0
	void ConnectionPool::finalize()
	{
		this->stop();

		JG_S::KAutoThreadMutex mx(m_mx);
		ConnectionMap::iterator it = m_cnnMap.begin();
		ConnectionMap::iterator ite = m_cnnMap.end();
		for(; it!=ite; it++)
		{
			ConnectionList& lst = it->second;
			PooledConnection* cnn;
			while(cnn = lst.pop_front(), cnn)
				cnn->destroy();
		}
		m_cnnMap.clear();
	}