From 99e02927b72376562470321d9da1e3cdafd387be Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Tue, 19 Mar 2019 15:34:54 +0800 Subject: [PATCH] Reconnect and retry the request if server has closed the reused connection. --- webcc/http_client_session.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/webcc/http_client_session.cc b/webcc/http_client_session.cc index a1bb8d0..f279633 100644 --- a/webcc/http_client_session.cc +++ b/webcc/http_client_session.cc @@ -94,7 +94,21 @@ HttpResponsePtr HttpClientSession::Request(HttpRequestArgs&& args) { client->set_timeout(timeout_); - if (!client->Request(request, !reuse)) { + bool ok = client->Request(request, !reuse); + + if (!ok) { + if (reuse && client->error() == kSocketWriteError) { + LOG_WARN("Cannot send request with the reused connection. " + "The server must have closed it, reconnect and try again."); + ok = client->Request(request, true); + } + } + + if (!ok) { + if (reuse) { + // Remove the failed connection from pool. + pool_.Remove(key); + } throw Exception(client->error(), client->timed_out()); }