diff --git a/Net/include/Poco/Net/TCPServer.h b/Net/include/Poco/Net/TCPServer.h index d63c00769..f90185ebb 100644 --- a/Net/include/Poco/Net/TCPServer.h +++ b/Net/include/Poco/Net/TCPServer.h @@ -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. diff --git a/Net/include/Poco/Net/TCPServerDispatcher.h b/Net/include/Poco/Net/TCPServerDispatcher.h index 47360a4ce..41cfcd5ea 100644 --- a/Net/include/Poco/Net/TCPServerDispatcher.h +++ b/Net/include/Poco/Net/TCPServerDispatcher.h @@ -85,6 +85,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. diff --git a/Net/src/TCPServer.cpp b/Net/src/TCPServer.cpp index 702d5f87c..b3ce9ea52 100644 --- a/Net/src/TCPServer.cpp +++ b/Net/src/TCPServer.cpp @@ -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 { diff --git a/Net/src/TCPServerDispatcher.cpp b/Net/src/TCPServerDispatcher.cpp index dddab3cd8..54f369aa4 100644 --- a/Net/src/TCPServerDispatcher.cpp +++ b/Net/src/TCPServerDispatcher.cpp @@ -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 { diff --git a/Net/testsuite/src/TCPServerTest.cpp b/Net/testsuite/src/TCPServerTest.cpp index aabbf8e79..9b5b4f7e9 100644 --- a/Net/testsuite/src/TCPServerTest.cpp +++ b/Net/testsuite/src/TCPServerTest.cpp @@ -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(), 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; } diff --git a/Net/testsuite/src/TCPServerTest.h b/Net/testsuite/src/TCPServerTest.h index 16b8bb041..5904a5945 100644 --- a/Net/testsuite/src/TCPServerTest.h +++ b/Net/testsuite/src/TCPServerTest.h @@ -49,6 +49,7 @@ public: void testOneConnection(); void testTwoConnections(); void testMultiConnections(); + void testThreadCapacity(); void setUp(); void tearDown();