Peroperly use the maxThreads specified in a TCPServerParameters to have the TCPServerDispatcher create a Threadpool with the correct capacity.

This commit is contained in:
karlr42
2014-03-06 19:44:54 +00:00
parent 715a09ff47
commit b509b7e91a
6 changed files with 43 additions and 2 deletions

View File

@@ -162,6 +162,9 @@ public:
int currentThreads() const;
/// Returns the number of currently used connection threads.
int maxThreads() const;
/// Returns the maximum number of threads available.
int totalConnections() const;
/// Returns the total number of handled connections.

View File

@@ -86,6 +86,9 @@ public:
int currentThreads() const;
/// Returns the number of currently used threads.
int maxThreads() const;
/// Returns the maximum number of threads available.
int totalConnections() const;
/// Returns the total number of handled connections.

View File

@@ -52,19 +52,29 @@ namespace Net {
TCPServer::TCPServer(TCPServerConnectionFactory::Ptr pFactory, Poco::UInt16 portNumber, TCPServerParams::Ptr pParams):
_socket(ServerSocket(portNumber)),
_pDispatcher(new TCPServerDispatcher(pFactory, Poco::ThreadPool::defaultPool(), pParams)),
_thread(threadName(_socket)),
_stopped(true)
{
Poco::ThreadPool& pool = Poco::ThreadPool::defaultPool();
if(pParams && (pParams->getMaxThreads() > pool.capacity())){
pool.addCapacity(pParams->getMaxThreads());
}
_pDispatcher = new TCPServerDispatcher(pFactory, pool, pParams);
}
TCPServer::TCPServer(TCPServerConnectionFactory::Ptr pFactory, const ServerSocket& socket, TCPServerParams::Ptr pParams):
_socket(socket),
_pDispatcher(new TCPServerDispatcher(pFactory, Poco::ThreadPool::defaultPool(), pParams)),
_thread(threadName(socket)),
_stopped(true)
{
Poco::ThreadPool& pool = Poco::ThreadPool::defaultPool();
if(pParams && (pParams->getMaxThreads() > pool.capacity())){
pool.addCapacity(pParams->getMaxThreads());
}
_pDispatcher = new TCPServerDispatcher(pFactory, pool, pParams);
}
@@ -146,6 +156,11 @@ int TCPServer::currentThreads() const
return _pDispatcher->currentThreads();
}
int TCPServer::maxThreads() const
{
return _pDispatcher->maxThreads();
}
int TCPServer::totalConnections() const
{

View File

@@ -197,6 +197,13 @@ int TCPServerDispatcher::currentThreads() const
return _currentThreads;
}
int TCPServerDispatcher::maxThreads() const
{
FastMutex::ScopedLock lock(_mutex);
return _threadPool.capacity();
}
int TCPServerDispatcher::totalConnections() const
{

View File

@@ -175,6 +175,7 @@ void TCPServerTest::testMultiConnections()
srv.start();
assert (srv.currentConnections() == 0);
assert (srv.currentThreads() == 0);
assert (srv.maxThreads() >= 4);
assert (srv.queuedConnections() == 0);
assert (srv.totalConnections() == 0);
@@ -252,6 +253,16 @@ void TCPServerTest::testMultiConnections()
assert (srv.currentConnections() == 0);
}
void TCPServerTest::testThreadCapacity(){
ServerSocket svs(0);
TCPServerParams* pParams = new TCPServerParams;
pParams->setMaxThreads(64);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs, pParams);
srv.start();
assert (srv.maxThreads() >= 64);
}
void TCPServerTest::setUp()
{
@@ -270,6 +281,7 @@ CppUnit::Test* TCPServerTest::suite()
CppUnit_addTest(pSuite, TCPServerTest, testOneConnection);
CppUnit_addTest(pSuite, TCPServerTest, testTwoConnections);
CppUnit_addTest(pSuite, TCPServerTest, testMultiConnections);
CppUnit_addTest(pSuite, TCPServerTest, testThreadCapacity);
return pSuite;
}

View File

@@ -49,6 +49,7 @@ public:
void testOneConnection();
void testTwoConnections();
void testMultiConnections();
void testThreadCapacity();
void setUp();
void tearDown();