Skip to content

Commit 74af8fa

Browse files
committed
http:Sessions are not reused in practice
so we could drop the support for that and enable simplifying e.g. always do asyncShutdown automatically and remove the need for callers to do that. Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com> Change-Id: Ic1c66efce5f6e68e3c375ce30e0440eb24e0e01e
1 parent 0d8917f commit 74af8fa

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

net/HttpRequest.hpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@
7171
//
7272
// http::Session is the primary class that is
7373
// used for all requests. It is created for a
74-
// specific host. Because http::Session is
75-
// designed to be reusable, the connection itself
76-
// can also be reused, improving efficiency.
74+
// specific host.
7775
//
7876
// An http::Session takes an http::Request and,
7977
// only after initiating a new request, access
@@ -1314,28 +1312,17 @@ class Session final : public ProtocolHandlerInterface
13141312
}
13151313

13161314
/// Start an asynchronous request on the given SocketPoll.
1317-
/// Return true when it dispatches the socket to the SocketPoll.
1318-
/// Note: when reusing this Session, it is assumed that the socket
1319-
/// is already added to the SocketPoll on a previous call (do not
1320-
/// use multiple SocketPoll instances on the same Session).
1315+
/// Note: Sessions are not reusable, create a new one per request.
13211316
void asyncRequest(const Request& req, const std::shared_ptr<SocketPoll>& poll)
13221317
{
13231318
LOG_TRC("new asyncRequest: " << req.getVerb() << ' ' << host() << ':' << port() << ' '
13241319
<< req.getUrl());
13251320

13261321
newRequest(req);
13271322

1328-
if (!isConnected())
1329-
{
1330-
asyncConnect(poll);
1331-
}
1332-
else
1333-
{
1334-
// Technically, there is a race here. The socket can
1335-
// get disconnected and removed right after isConnected.
1336-
// In that case, we will timeout and no request will be sent.
1337-
poll->wakeup();
1338-
}
1323+
assert(!isConnected() && _socket.expired());
1324+
1325+
asyncConnect(poll);
13391326

13401327
LOG_DBG("starting asyncRequest: " << req.getVerb() << ' ' << host() << ':' << port() << ' '
13411328
<< req.getUrl());
@@ -1441,7 +1428,8 @@ class Session final : public ProtocolHandlerInterface
14411428

14421429
assert(!!_response && "Response must be set!");
14431430

1444-
if (!isConnected())
1431+
assert(!isConnected() && _socket.expired());
1432+
14451433
{
14461434
std::shared_ptr<StreamSocket> socket = connect();
14471435
if (!socket)
@@ -1722,7 +1710,7 @@ class Session final : public ProtocolHandlerInterface
17221710

17231711
void asyncConnectFailed(net::AsyncConnectResult result)
17241712
{
1725-
assert(!_socket.use_count());
1713+
assert(_socket.expired());
17261714
_result = result;
17271715

17281716
LOG_ERR("Failed to connect to " << _host << ':' << _port);
@@ -1743,7 +1731,7 @@ class Session final : public ProtocolHandlerInterface
17431731

17441732
void asyncConnect(const std::weak_ptr<SocketPoll>& poll)
17451733
{
1746-
_socket.reset(); // Reset to make sure we are disconnected.
1734+
assert(_socket.expired());
17471735

17481736
auto pushConnectCompleteToPoll =
17491737
[this, poll](std::shared_ptr<StreamSocket> socket, net::AsyncConnectResult result)

0 commit comments

Comments
 (0)