From: Thomas Markwalder Date: Wed, 16 Nov 2022 17:07:14 +0000 (-0500) Subject: [2635] Make idle timeout configurable X-Git-Tag: Kea-2.3.3~77 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c8037090f49386fd91da489dbbd83981ce1303f3;p=thirdparty%2Fkea.git [2635] Make idle timeout configurable modified: mt_tcp_listener_mgr.cc mt_tcp_listener_mgr.h tcp_listener.h tests/mt_tcp_listener_mgr_unittests.cc --- diff --git a/src/lib/tcp/mt_tcp_listener_mgr.cc b/src/lib/tcp/mt_tcp_listener_mgr.cc index 25830f43a7..66a27e4e98 100644 --- a/src/lib/tcp/mt_tcp_listener_mgr.cc +++ b/src/lib/tcp/mt_tcp_listener_mgr.cc @@ -31,7 +31,8 @@ MtTcpListenerMgr::MtTcpListenerMgr(TcpListenerFactory listener_factory, : listener_factory_(listener_factory), address_(address), port_(port), thread_io_service_(), tcp_listener_(), thread_pool_size_(thread_pool_size), thread_pool_(), - tls_context_(context), connection_filter_(connection_filter) { + tls_context_(context), connection_filter_(connection_filter), + idle_timeout_(TCP_IDLE_CONNECTION_TIMEOUT) { } MtTcpListenerMgr::~MtTcpListenerMgr() { @@ -56,9 +57,11 @@ MtTcpListenerMgr::start() { thread_io_service_.reset(new IOService()); // Create a new TCPListener derivation using the factory. - tcp_listener_ = listener_factory_(*thread_io_service_, address_, - port_, tls_context_, - TcpListener::IdleTimeout(TCP_IDLE_CONNECTION_TIMEOUT), + tcp_listener_ = listener_factory_(*thread_io_service_, + address_, + port_, + tls_context_, + idle_timeout_, connection_filter_); // Create the thread pool with immediate start. diff --git a/src/lib/tcp/mt_tcp_listener_mgr.h b/src/lib/tcp/mt_tcp_listener_mgr.h index 41cc1e1e42..fe7780f68f 100644 --- a/src/lib/tcp/mt_tcp_listener_mgr.h +++ b/src/lib/tcp/mt_tcp_listener_mgr.h @@ -18,7 +18,7 @@ namespace isc { namespace tcp { /// @brief Default connection idle timeout in milliseconds. -const long TCP_IDLE_CONNECTION_TIMEOUT = 10000; +const long TCP_IDLE_CONNECTION_TIMEOUT = 300 * 1000; /// @brief Defines a factory function for creating TcpListeners. typedef std::function< @@ -158,6 +158,17 @@ public: return(tcp_listener_); } + /// @brief Sets the idle time per connection. + /// + /// @param timeout Amount of time in milliseconds + void setIdleTimeout(long milliseconds) { + idle_timeout_ = TcpListener::IdleTimeout(milliseconds); + } + + long getIdleTimeout() { + return (idle_timeout_.value_); + } + private: /// @brief Factory for creating TcpListener instances. TcpListenerFactory listener_factory_; @@ -185,6 +196,10 @@ private: /// @brief Callback the listener may use to reject connections during acceptance. TcpConnectionFilterCallback connection_filter_; + + /// @brief Time in milliseconds that a connection can remain idle before + /// it is closed. + TcpListener::IdleTimeout idle_timeout_; }; /// @brief Defines a shared pointer to MtTcpListenerMgr. diff --git a/src/lib/tcp/tcp_listener.h b/src/lib/tcp/tcp_listener.h index 67daf41e9f..20d88762ff 100644 --- a/src/lib/tcp/tcp_listener.h +++ b/src/lib/tcp/tcp_listener.h @@ -90,6 +90,11 @@ public: /// @brief Returns local port on which server is listening. uint16_t getLocalPort() const; + /// @brief Returns the idle timeout (in milliseconds). + long getIdleTimeout() const { + return (idle_timeout_); + } + protected: /// @brief Creates @ref TcpConnection instance and adds it to the diff --git a/src/lib/tcp/tests/mt_tcp_listener_mgr_unittests.cc b/src/lib/tcp/tests/mt_tcp_listener_mgr_unittests.cc index 4613ed2859..9e7f8e85c8 100644 --- a/src/lib/tcp/tests/mt_tcp_listener_mgr_unittests.cc +++ b/src/lib/tcp/tests/mt_tcp_listener_mgr_unittests.cc @@ -959,4 +959,27 @@ TEST_F(MtTcpListenerMgrTest, tls) { EXPECT_TRUE(mt_listener_mgr_->isStopped()); } +/// Verifies that idle timeout can be passed down to the internal listener. +TEST_F(MtTcpListenerMgrTest, idleTimeout) { + // Create an MtTcpListenerMgr. + createMtTcpListenerMgr(1, std::bind(&MtTcpListenerMgrTest::synchronizedCommandHandler, + this, ph::_1)); + // Verify the defualt timeout value. + EXPECT_EQ(TCP_IDLE_CONNECTION_TIMEOUT, mt_listener_mgr_->getIdleTimeout()); + + // Set a new timeout value. + mt_listener_mgr_->setIdleTimeout(200); + EXPECT_EQ(200, mt_listener_mgr_->getIdleTimeout()); + + // Start the listener, which should instantiate the internal listener. + ASSERT_NO_THROW_LOG(mt_listener_mgr_->start()); + ASSERT_TRUE(mt_listener_mgr_->isRunning()); + + // Verify the internal listener's timeout value. + auto tcp_listener = mt_listener_mgr_->getTcpListener(); + ASSERT_TRUE(tcp_listener); + EXPECT_EQ(200, tcp_listener->getIdleTimeout()); +} + + } // end of anonymous namespace