71
71
//
72
72
// http::Session is the primary class that is
73
73
// 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.
77
75
//
78
76
// An http::Session takes an http::Request and,
79
77
// only after initiating a new request, access
@@ -1314,28 +1312,17 @@ class Session final : public ProtocolHandlerInterface
1314
1312
}
1315
1313
1316
1314
// / 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.
1321
1316
void asyncRequest (const Request& req, const std::shared_ptr<SocketPoll>& poll)
1322
1317
{
1323
1318
LOG_TRC (" new asyncRequest: " << req.getVerb () << ' ' << host () << ' :' << port () << ' '
1324
1319
<< req.getUrl ());
1325
1320
1326
1321
newRequest (req);
1327
1322
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);
1339
1326
1340
1327
LOG_DBG (" starting asyncRequest: " << req.getVerb () << ' ' << host () << ' :' << port () << ' '
1341
1328
<< req.getUrl ());
@@ -1441,7 +1428,8 @@ class Session final : public ProtocolHandlerInterface
1441
1428
1442
1429
assert (!!_response && " Response must be set!" );
1443
1430
1444
- if (!isConnected ())
1431
+ assert (!isConnected () && _socket.expired ());
1432
+
1445
1433
{
1446
1434
std::shared_ptr<StreamSocket> socket = connect ();
1447
1435
if (!socket)
@@ -1722,7 +1710,7 @@ class Session final : public ProtocolHandlerInterface
1722
1710
1723
1711
void asyncConnectFailed (net::AsyncConnectResult result)
1724
1712
{
1725
- assert (! _socket.use_count ());
1713
+ assert (_socket.expired ());
1726
1714
_result = result;
1727
1715
1728
1716
LOG_ERR (" Failed to connect to " << _host << ' :' << _port);
@@ -1743,7 +1731,7 @@ class Session final : public ProtocolHandlerInterface
1743
1731
1744
1732
void asyncConnect (const std::weak_ptr<SocketPoll>& poll)
1745
1733
{
1746
- _socket.reset (); // Reset to make sure we are disconnected.
1734
+ assert ( _socket.expired ());
1747
1735
1748
1736
auto pushConnectCompleteToPoll =
1749
1737
[this , poll](std::shared_ptr<StreamSocket> socket, net::AsyncConnectResult result)
0 commit comments