Skip to content

Commit 7f08d90

Browse files
author
Razvan Becheriu
committed
[#3602] use weak ptr
1 parent d8c9dca commit 7f08d90

9 files changed

+34
-16
lines changed

src/bin/dhcp4/tests/http_control_socket_unittest.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ class BaseCtrlChannelDhcpv4Test : public ::testing::Test {
149149
this, true),
150150
TEST_TIMEOUT, IntervalTimer::ONE_SHOT);
151151
// Run until the client stops the service or an error occurs.
152-
io_service->run();
152+
try {
153+
io_service->run();
154+
} catch (...) {
155+
}
153156
test_timer.cancel();
154157
if (io_service->stopped()) {
155158
io_service->restart();

src/bin/dhcp6/tests/http_control_socket_unittest.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ class BaseCtrlChannelDhcpv6Test : public HttpCtrlDhcpv6Test {
187187
this, true),
188188
TEST_TIMEOUT, IntervalTimer::ONE_SHOT);
189189
// Run until the client stops the service or an error occurs.
190-
io_service->run();
190+
try {
191+
io_service->run();
192+
} catch (...) {
193+
}
191194
test_timer.cancel();
192195
if (io_service->stopped()) {
193196
io_service->restart();

src/lib/http/connection.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ SocketCallback::operator()(boost::system::error_code ec, size_t length) {
6868
HttpConnection::HttpConnection(const asiolink::IOServicePtr& io_service,
6969
const HttpAcceptorPtr& acceptor,
7070
const TlsContextPtr& tls_context,
71-
HttpConnectionPool& connection_pool,
71+
HttpConnectionPoolPtr connection_pool,
7272
const HttpResponseCreatorPtr& response_creator,
7373
const HttpAcceptorCallback& callback,
7474
const long request_timeout,
@@ -234,23 +234,33 @@ HttpConnection::close() {
234234

235235
void
236236
HttpConnection::shutdownConnection() {
237+
auto connection_pool = connection_pool_.lock();
237238
try {
238239
LOG_DEBUG(http_logger, isc::log::DBGLVL_TRACE_BASIC,
239240
HTTP_CONNECTION_SHUTDOWN)
240241
.arg(getRemoteEndpointAddressAsText());
241-
connection_pool_.shutdown(shared_from_this());
242+
if (connection_pool) {
243+
connection_pool->shutdown(shared_from_this());
244+
} else {
245+
shutdown();
246+
}
242247
} catch (...) {
243248
LOG_ERROR(http_logger, HTTP_CONNECTION_SHUTDOWN_FAILED);
244249
}
245250
}
246251

247252
void
248253
HttpConnection::stopThisConnection() {
254+
auto connection_pool = connection_pool_.lock();
249255
try {
250256
LOG_DEBUG(http_logger, isc::log::DBGLVL_TRACE_BASIC,
251257
HTTP_CONNECTION_STOP)
252258
.arg(getRemoteEndpointAddressAsText());
253-
connection_pool_.stop(shared_from_this());
259+
if (connection_pool) {
260+
connection_pool->stop(shared_from_this());
261+
} else {
262+
close();
263+
}
254264
} catch (...) {
255265
LOG_ERROR(http_logger, HTTP_CONNECTION_STOP_FAILED);
256266
}

src/lib/http/connection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class HttpConnection : public boost::enable_shared_from_this<HttpConnection> {
245245
HttpConnection(const asiolink::IOServicePtr& io_service,
246246
const HttpAcceptorPtr& acceptor,
247247
const asiolink::TlsContextPtr& tls_context,
248-
HttpConnectionPool& connection_pool,
248+
std::shared_ptr<HttpConnectionPool> connection_pool,
249249
const HttpResponseCreatorPtr& response_creator,
250250
const HttpAcceptorCallback& callback,
251251
const long request_timeout,
@@ -441,7 +441,7 @@ class HttpConnection : public boost::enable_shared_from_this<HttpConnection> {
441441
HttpAcceptorPtr acceptor_;
442442

443443
/// @brief Connection pool holding this connection.
444-
HttpConnectionPool& connection_pool_;
444+
std::weak_ptr<HttpConnectionPool> connection_pool_;
445445

446446
/// @brief Pointer to the @ref HttpResponseCreator object used to create
447447
/// HTTP responses.

src/lib/http/connection_pool.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class HttpConnectionPool {
7171
std::mutex mutex_;
7272
};
7373

74+
/// @brief Pointer to the @ref HttpConnection.
75+
typedef std::shared_ptr<HttpConnectionPool> HttpConnectionPoolPtr;
76+
7477
}
7578
}
7679

src/lib/http/listener_impl.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ HttpListenerImpl::HttpListenerImpl(const IOServicePtr& io_service,
2626
const long request_timeout,
2727
const long idle_timeout)
2828
: io_service_(io_service), tls_context_(tls_context), acceptor_(),
29-
endpoint_(), connections_(),
29+
endpoint_(), connections_(new HttpConnectionPool()),
3030
creator_factory_(creator_factory),
3131
request_timeout_(request_timeout), idle_timeout_(idle_timeout),
3232
use_external_(false) {
@@ -102,7 +102,7 @@ HttpListenerImpl::start() {
102102

103103
void
104104
HttpListenerImpl::stop() {
105-
connections_.stopAll();
105+
connections_->stopAll();
106106
if (use_external_) {
107107
IfaceMgr::instance().deleteExternalSocket(acceptor_->getNative());
108108
}
@@ -124,7 +124,7 @@ HttpListenerImpl::accept() {
124124
conn->addExternalSockets(true);
125125
}
126126
// Add this new connection to the pool.
127-
connections_.start(conn);
127+
connections_->start(conn);
128128
}
129129

130130
void

src/lib/http/listener_impl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class HttpListenerImpl : public boost::enable_shared_from_this<HttpListenerImpl>
128128
boost::scoped_ptr<asiolink::TCPEndpoint> endpoint_;
129129

130130
/// @brief Pool of active connections.
131-
HttpConnectionPool connections_;
131+
HttpConnectionPoolPtr connections_;
132132

133133
/// @brief Pointer to the @ref HttpResponseCreatorFactory.
134134
HttpResponseCreatorFactoryPtr creator_factory_;
@@ -144,7 +144,6 @@ class HttpListenerImpl : public boost::enable_shared_from_this<HttpListenerImpl>
144144
bool use_external_;
145145
};
146146

147-
148147
} // end of namespace isc::http
149148
} // end of namespace isc
150149

src/lib/http/tests/connection_pool_unittests.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class HttpConnectionPoolTest : public ::testing::Test {
107107
HttpConnectionPoolTest()
108108
: io_service_(new IOService()),
109109
acceptor_(new HttpAcceptor(io_service_)),
110-
connection_pool_(),
110+
connection_pool_(new HttpConnectionPool()),
111111
response_creator_(new TestHttpResponseCreator()) {
112112
MultiThreadingMgr::instance().setMode(false);
113113
}
@@ -219,7 +219,7 @@ class HttpConnectionPoolTest : public ::testing::Test {
219219

220220
IOServicePtr io_service_; ///< IO service.
221221
HttpAcceptorPtr acceptor_; ///< Test acceptor.
222-
HttpConnectionPool connection_pool_; ///< Test connection pool.
222+
HttpConnectionPoolPtr connection_pool_; ///< Test connection pool.
223223
HttpResponseCreatorPtr response_creator_; ///< Test response creator.
224224

225225
};

src/lib/http/tests/http_server_test.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class HttpConnectionLongWriteBuffer : public HttpConnection {
125125
HttpConnectionLongWriteBuffer(const IOServicePtr& io_service,
126126
const HttpAcceptorPtr& acceptor,
127127
const TlsContextPtr& tls_context,
128-
HttpConnectionPool& connection_pool,
128+
HttpConnectionPoolPtr connection_pool,
129129
const HttpResponseCreatorPtr& response_creator,
130130
const HttpAcceptorCallback& callback,
131131
const long request_timeout,
@@ -172,7 +172,7 @@ class HttpConnectionTransactionChange : public HttpConnection {
172172
HttpConnectionTransactionChange(const IOServicePtr& io_service,
173173
const HttpAcceptorPtr& acceptor,
174174
const TlsContextPtr& tls_context,
175-
HttpConnectionPool& connection_pool,
175+
HttpConnectionPoolPtr connection_pool,
176176
const HttpResponseCreatorPtr& response_creator,
177177
const HttpAcceptorCallback& callback,
178178
const long request_timeout,

0 commit comments

Comments
 (0)