@@ -64,6 +64,10 @@ extern "C" std::size_t CurlRequestOnReadData(char* ptr, std::size_t size,
6464 return v->OnRead (ptr, size, nitems);
6565}
6666
67+ CurlRequest::~CurlRequest () {
68+ if (factory_) factory_->CleanupHandle (std::move (handle_));
69+ }
70+
6771StatusOr<HttpResponse> CurlRequest::MakeRequest (std::string const & payload) {
6872 handle_.SetOption (CURLOPT_UPLOAD, 0L );
6973 if (!payload.empty ()) {
@@ -90,6 +94,14 @@ StatusOr<HttpResponse> CurlRequest::MakeUploadRequest(
9094 return MakeRequestImpl ();
9195}
9296
97+ Status CurlRequest::OnError (Status status) {
98+ // When there is a transfer error the handle is suspect. It could be pointing
99+ // to an invalid host, a host that is slow and trickling data, or otherwise in
100+ // a bad state. Release the handle, but do not return it to the pool.
101+ auto handle = std::move (handle_);
102+ return status;
103+ }
104+
93105StatusOr<HttpResponse> CurlRequest::MakeRequestImpl () {
94106 // We get better performance using a slightly larger buffer (128KiB) than the
95107 // default buffer size set by libcurl (16KiB)
@@ -111,7 +123,7 @@ StatusOr<HttpResponse> CurlRequest::MakeRequestImpl() {
111123 handle_.SetOption (CURLOPT_HEADERFUNCTION, &CurlRequestOnHeaderData);
112124 handle_.SetOption (CURLOPT_HEADERDATA, this );
113125 auto status = handle_.EasyPerform ();
114- if (!status.ok ()) return status;
126+ if (!status.ok ()) return OnError ( std::move ( status)) ;
115127
116128 if (logging_enabled_) handle_.FlushDebug (__func__);
117129 auto code = handle_.GetResponseCode ();
0 commit comments