]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3602] use weak ptr
authorRazvan Becheriu <razvan@isc.org>
Tue, 24 Sep 2024 04:36:51 +0000 (07:36 +0300)
committerRazvan Becheriu <razvan@isc.org>
Mon, 28 Oct 2024 07:37:04 +0000 (09:37 +0200)
src/bin/dhcp4/tests/http_control_socket_unittest.cc
src/bin/dhcp6/tests/http_control_socket_unittest.cc
src/lib/http/connection.cc
src/lib/http/connection.h
src/lib/http/connection_pool.h
src/lib/http/listener_impl.cc
src/lib/http/listener_impl.h
src/lib/http/tests/connection_pool_unittests.cc
src/lib/http/tests/http_server_test.h

index d53df7f130ac514a25671aeb7e3e843963fd0003..38880683c27dc539e02b22cf65383823a12b285c 100644 (file)
@@ -149,7 +149,10 @@ public:
                                    this, true),
                          TEST_TIMEOUT, IntervalTimer::ONE_SHOT);
         // Run until the client stops the service or an error occurs.
-        io_service->run();
+        try {
+           io_service->run();
+        } catch (...) {
+        }
         test_timer.cancel();
         if (io_service->stopped()) {
             io_service->restart();
index 4487d645ce034b71db9cae0c17f4d48b62013551..0c72acc840e04596ee84a4901b57ddf72a1e6032 100644 (file)
@@ -187,7 +187,10 @@ public:
                                    this, true),
                          TEST_TIMEOUT, IntervalTimer::ONE_SHOT);
         // Run until the client stops the service or an error occurs.
-        io_service->run();
+        try {
+           io_service->run();
+        } catch (...) {
+        }
         test_timer.cancel();
         if (io_service->stopped()) {
             io_service->restart();
index fdf0f370284c2b80980969a4185e39c3c894f0ea..4c83d1b0b6eb60b46c3df460469aa39bb42033f6 100644 (file)
@@ -68,7 +68,7 @@ SocketCallback::operator()(boost::system::error_code ec, size_t length) {
 HttpConnection::HttpConnection(const asiolink::IOServicePtr& io_service,
                                const HttpAcceptorPtr& acceptor,
                                const TlsContextPtr& tls_context,
-                               HttpConnectionPool& connection_pool,
+                               HttpConnectionPoolPtr connection_pool,
                                const HttpResponseCreatorPtr& response_creator,
                                const HttpAcceptorCallback& callback,
                                const long request_timeout,
@@ -234,11 +234,16 @@ HttpConnection::close() {
 
 void
 HttpConnection::shutdownConnection() {
+    auto connection_pool = connection_pool_.lock();
     try {
         LOG_DEBUG(http_logger, isc::log::DBGLVL_TRACE_BASIC,
                   HTTP_CONNECTION_SHUTDOWN)
             .arg(getRemoteEndpointAddressAsText());
-        connection_pool_.shutdown(shared_from_this());
+        if (connection_pool) {
+            connection_pool->shutdown(shared_from_this());
+        } else {
+            shutdown();
+        }
     } catch (...) {
         LOG_ERROR(http_logger, HTTP_CONNECTION_SHUTDOWN_FAILED);
     }
@@ -246,11 +251,16 @@ HttpConnection::shutdownConnection() {
 
 void
 HttpConnection::stopThisConnection() {
+    auto connection_pool = connection_pool_.lock();
     try {
         LOG_DEBUG(http_logger, isc::log::DBGLVL_TRACE_BASIC,
                   HTTP_CONNECTION_STOP)
             .arg(getRemoteEndpointAddressAsText());
-        connection_pool_.stop(shared_from_this());
+        if (connection_pool) {
+            connection_pool->stop(shared_from_this());
+        } else {
+            close();
+        }
     } catch (...) {
         LOG_ERROR(http_logger, HTTP_CONNECTION_STOP_FAILED);
     }
index eec18d319c797e32e9113bc3559351a4e5ffba46..11b90799ba171c39442444a363d66424ed1dc277 100644 (file)
@@ -245,7 +245,7 @@ public:
     HttpConnection(const asiolink::IOServicePtr& io_service,
                    const HttpAcceptorPtr& acceptor,
                    const asiolink::TlsContextPtr& tls_context,
-                   HttpConnectionPool& connection_pool,
+                   std::shared_ptr<HttpConnectionPool> connection_pool,
                    const HttpResponseCreatorPtr& response_creator,
                    const HttpAcceptorCallback& callback,
                    const long request_timeout,
@@ -441,7 +441,7 @@ protected:
     HttpAcceptorPtr acceptor_;
 
     /// @brief Connection pool holding this connection.
-    HttpConnectionPool& connection_pool_;
+    std::weak_ptr<HttpConnectionPool> connection_pool_;
 
     /// @brief Pointer to the @ref HttpResponseCreator object used to create
     /// HTTP responses.
index ab6a7cfde6516e3eed8f84154a81612830d2307e..d5a995bc312e0d608ce259f0682417afdb3e9edb 100644 (file)
@@ -71,6 +71,9 @@ protected:
     std::mutex mutex_;
 };
 
+/// @brief Pointer to the @ref HttpConnection.
+typedef std::shared_ptr<HttpConnectionPool> HttpConnectionPoolPtr;
+
 }
 }
 
index a5df194c1649cf3df916194d5e4d60c6c36db0a2..70535ef7e56e7236906e3e5d5d76b6a1151900a8 100644 (file)
@@ -26,7 +26,7 @@ HttpListenerImpl::HttpListenerImpl(const IOServicePtr& io_service,
                                    const long request_timeout,
                                    const long idle_timeout)
     : io_service_(io_service), tls_context_(tls_context), acceptor_(),
-      endpoint_(), connections_(),
+      endpoint_(), connections_(new HttpConnectionPool()),
       creator_factory_(creator_factory),
       request_timeout_(request_timeout), idle_timeout_(idle_timeout),
       use_external_(false) {
@@ -102,7 +102,7 @@ HttpListenerImpl::start() {
 
 void
 HttpListenerImpl::stop() {
-    connections_.stopAll();
+    connections_->stopAll();
     if (use_external_) {
         IfaceMgr::instance().deleteExternalSocket(acceptor_->getNative());
     }
@@ -124,7 +124,7 @@ HttpListenerImpl::accept() {
         conn->addExternalSockets(true);
     }
     // Add this new connection to the pool.
-    connections_.start(conn);
+    connections_->start(conn);
 }
 
 void
index f65cd3fa6dbf979bdbf51c999fdb8f0fb7944990..73f70dc50764dbb0ccb8174384aaaa7034d20903 100644 (file)
@@ -128,7 +128,7 @@ protected:
     boost::scoped_ptr<asiolink::TCPEndpoint> endpoint_;
 
     /// @brief Pool of active connections.
-    HttpConnectionPool connections_;
+    HttpConnectionPoolPtr connections_;
 
     /// @brief Pointer to the @ref HttpResponseCreatorFactory.
     HttpResponseCreatorFactoryPtr creator_factory_;
@@ -144,7 +144,6 @@ protected:
     bool use_external_;
 };
 
-
 } // end of namespace isc::http
 } // end of namespace isc
 
index c6e72585419b786ffffbd321a4c03891e8f5bc7a..bbbdd7e15f54de98382cd77ad83b96735f6b529e 100644 (file)
@@ -107,7 +107,7 @@ public:
     HttpConnectionPoolTest()
         : io_service_(new IOService()),
           acceptor_(new HttpAcceptor(io_service_)),
-          connection_pool_(),
+          connection_pool_(new HttpConnectionPool()),
           response_creator_(new TestHttpResponseCreator()) {
         MultiThreadingMgr::instance().setMode(false);
     }
@@ -219,7 +219,7 @@ public:
 
     IOServicePtr io_service_;                   ///< IO service.
     HttpAcceptorPtr acceptor_;                  ///< Test acceptor.
-    HttpConnectionPool connection_pool_;        ///< Test connection pool.
+    HttpConnectionPoolPtr connection_pool_;     ///< Test connection pool.
     HttpResponseCreatorPtr response_creator_;   ///< Test response creator.
 
 };
index a87c411a1a46110869f30613125bcaafc6f8511f..48c0671400a92610f83d1ce84fabf485f7496124 100644 (file)
@@ -125,7 +125,7 @@ public:
     HttpConnectionLongWriteBuffer(const IOServicePtr& io_service,
                                   const HttpAcceptorPtr& acceptor,
                                   const TlsContextPtr& tls_context,
-                                  HttpConnectionPool& connection_pool,
+                                  HttpConnectionPoolPtr connection_pool,
                                   const HttpResponseCreatorPtr& response_creator,
                                   const HttpAcceptorCallback& callback,
                                   const long request_timeout,
@@ -172,7 +172,7 @@ public:
     HttpConnectionTransactionChange(const IOServicePtr& io_service,
                                     const HttpAcceptorPtr& acceptor,
                                     const TlsContextPtr& tls_context,
-                                    HttpConnectionPool& connection_pool,
+                                    HttpConnectionPoolPtr connection_pool,
                                     const HttpResponseCreatorPtr& response_creator,
                                     const HttpAcceptorCallback& callback,
                                     const long request_timeout,