1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/compat.h"
6 #include <boost/utility/string_ref.hpp>
10 #include <curl/multi.h>
12 #include "rgw_common.h"
13 #include "rgw_http_client.h"
14 #include "rgw_http_errors.h"
15 #include "common/RefCountedObj.h"
17 #include "rgw_coroutine.h"
21 #define dout_context g_ceph_context
22 #define dout_subsys ceph_subsys_rgw
24 struct rgw_http_req_data
: public RefCountedObject
{
29 std::atomic
<bool> done
= { false };
30 RGWHTTPClient
*client
;
34 char error_buf
[CURL_ERROR_SIZE
];
39 rgw_http_req_data() : easy_handle(NULL
), h(NULL
), id(-1), ret(0),
40 client(nullptr), user_info(nullptr), registered(false),
41 mgr(NULL
), lock("rgw_http_req_data::lock") {
42 memset(error_buf
, 0, sizeof(error_buf
));
46 Mutex::Locker
l(lock
);
53 Mutex::Locker
l(lock
);
56 curl_easy_cleanup(easy_handle
);
59 curl_slist_free_all(h
);
72 Mutex::Locker
l(lock
);
76 RGWHTTPManager
*get_manager() {
77 Mutex::Locker
l(lock
);
82 struct RGWCurlHandle
{
87 RGWCurlHandle(CURL
* h
) : uses(0), h(h
) {};
94 class RGWCurlHandles
: public Thread
{
97 std::vector
<RGWCurlHandle
*>saved_curl
;
102 cleaner_lock
{"RGWCurlHandles::cleaner_lock"},
103 cleaner_shutdown
{0} {
106 RGWCurlHandle
* get_curl_handle();
107 void release_curl_handle_now(RGWCurlHandle
* curl
);
108 void release_curl_handle(RGWCurlHandle
* curl
);
109 void flush_curl_handles();
114 RGWCurlHandle
* RGWCurlHandles::get_curl_handle() {
115 RGWCurlHandle
* curl
= 0;
118 Mutex::Locker
lock(cleaner_lock
);
119 if (!saved_curl
.empty()) {
120 curl
= *saved_curl
.begin();
121 saved_curl
.erase(saved_curl
.begin());
125 } else if ((h
= curl_easy_init())) {
126 curl
= new RGWCurlHandle
{h
};
133 void RGWCurlHandles::release_curl_handle_now(RGWCurlHandle
* curl
)
135 curl_easy_cleanup(**curl
);
139 void RGWCurlHandles::release_curl_handle(RGWCurlHandle
* curl
)
141 if (cleaner_shutdown
) {
142 release_curl_handle_now(curl
);
144 curl_easy_reset(**curl
);
145 Mutex::Locker
lock(cleaner_lock
);
146 curl
->lastuse
= mono_clock::now();
147 saved_curl
.insert(saved_curl
.begin(), 1, curl
);
151 void* RGWCurlHandles::entry()
154 Mutex::Locker
lock(cleaner_lock
);
157 if (cleaner_shutdown
) {
158 if (saved_curl
.empty())
161 utime_t release
= ceph_clock_now() + utime_t(MAXIDLE
,0);
162 cleaner_cond
.WaitUntil(cleaner_lock
, release
);
164 mono_time now
= mono_clock::now();
165 while (!saved_curl
.empty()) {
166 auto cend
= saved_curl
.end();
169 if (!cleaner_shutdown
&& now
- curl
->lastuse
< std::chrono::seconds(MAXIDLE
))
171 saved_curl
.erase(cend
);
172 release_curl_handle_now(curl
);
178 void RGWCurlHandles::stop()
180 Mutex::Locker
lock(cleaner_lock
);
181 cleaner_shutdown
= 1;
182 cleaner_cond
.Signal();
185 void RGWCurlHandles::flush_curl_handles()
189 if (!saved_curl
.empty()) {
190 dout(0) << "ERROR: " << __func__
<< " failed final cleanup" << dendl
;
192 saved_curl
.shrink_to_fit();
195 static RGWCurlHandles
*handles
;
196 // XXX make this part of the token cache? (but that's swift-only;
197 // and this especially needs to integrates with s3...)
199 void rgw_setup_saved_curl_handles()
201 handles
= new RGWCurlHandles();
202 handles
->create("rgw_curl");
205 void rgw_release_all_curl_handles()
207 handles
->flush_curl_handles();
212 * the simple set of callbacks will be called on RGWHTTPClient::process()
214 /* Static methods - callbacks for libcurl. */
215 size_t RGWHTTPClient::simple_receive_http_header(void * const ptr
,
220 RGWHTTPClient
*client
= static_cast<RGWHTTPClient
*>(_info
);
221 const size_t len
= size
* nmemb
;
222 int ret
= client
->receive_header(ptr
, size
* nmemb
);
224 dout(0) << "WARNING: client->receive_header() returned ret="
231 size_t RGWHTTPClient::simple_receive_http_data(void * const ptr
,
236 RGWHTTPClient
*client
= static_cast<RGWHTTPClient
*>(_info
);
237 const size_t len
= size
* nmemb
;
238 int ret
= client
->receive_data(ptr
, size
* nmemb
);
240 dout(0) << "WARNING: client->receive_data() returned ret="
247 size_t RGWHTTPClient::simple_send_http_data(void * const ptr
,
252 RGWHTTPClient
*client
= static_cast<RGWHTTPClient
*>(_info
);
253 int ret
= client
->send_data(ptr
, size
* nmemb
);
255 dout(0) << "WARNING: client->send_data() returned ret="
263 * the following set of callbacks will be called either on RGWHTTPManager::process(),
264 * or via the RGWHTTPManager async processing.
266 size_t RGWHTTPClient::receive_http_header(void * const ptr
,
271 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
272 size_t len
= size
* nmemb
;
274 Mutex::Locker
l(req_data
->lock
);
276 if (!req_data
->registered
) {
280 int ret
= req_data
->client
->receive_header(ptr
, size
* nmemb
);
282 dout(0) << "WARNING: client->receive_header() returned ret=" << ret
<< dendl
;
288 size_t RGWHTTPClient::receive_http_data(void * const ptr
,
293 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
294 size_t len
= size
* nmemb
;
296 Mutex::Locker
l(req_data
->lock
);
298 if (!req_data
->registered
) {
302 int ret
= req_data
->client
->receive_data(ptr
, size
* nmemb
);
304 dout(0) << "WARNING: client->receive_data() returned ret=" << ret
<< dendl
;
310 size_t RGWHTTPClient::send_http_data(void * const ptr
,
315 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
317 Mutex::Locker
l(req_data
->lock
);
319 if (!req_data
->registered
) {
323 int ret
= req_data
->client
->send_data(ptr
, size
* nmemb
);
325 dout(0) << "WARNING: client->receive_data() returned ret=" << ret
<< dendl
;
331 static curl_slist
*headers_to_slist(param_vec_t
& headers
)
333 curl_slist
*h
= NULL
;
335 param_vec_t::iterator iter
;
336 for (iter
= headers
.begin(); iter
!= headers
.end(); ++iter
) {
337 pair
<string
, string
>& p
= *iter
;
338 string val
= p
.first
;
340 if (strncmp(val
.c_str(), "HTTP_", 5) == 0) {
344 /* we need to convert all underscores into dashes as some web servers forbid them
345 * in the http header field names
347 for (size_t i
= 0; i
< val
.size(); i
++) {
353 // curl won't send headers with empty values unless it ends with a ; instead
354 if (p
.second
.empty()) {
358 val
.append(p
.second
);
360 h
= curl_slist_append(h
, val
.c_str());
366 static bool is_upload_request(const char *method
)
368 if (method
== nullptr) {
371 return strcmp(method
, "POST") == 0 || strcmp(method
, "PUT") == 0;
375 * process a single simple one off request, not going through RGWHTTPManager. Not using
378 int RGWHTTPClient::process(const char *method
, const char *url
)
383 char error_buf
[CURL_ERROR_SIZE
];
385 last_method
= (method
? method
: "");
386 last_url
= (url
? url
: "");
388 auto ca
= handles
->get_curl_handle();
391 dout(20) << "sending request to " << url
<< dendl
;
393 curl_slist
*h
= headers_to_slist(headers
);
395 curl_easy_setopt(curl_handle
, CURLOPT_CUSTOMREQUEST
, method
);
396 curl_easy_setopt(curl_handle
, CURLOPT_URL
, url
);
397 curl_easy_setopt(curl_handle
, CURLOPT_NOPROGRESS
, 1L);
398 curl_easy_setopt(curl_handle
, CURLOPT_NOSIGNAL
, 1L);
399 curl_easy_setopt(curl_handle
, CURLOPT_HEADERFUNCTION
, simple_receive_http_header
);
400 curl_easy_setopt(curl_handle
, CURLOPT_WRITEHEADER
, (void *)this);
401 curl_easy_setopt(curl_handle
, CURLOPT_WRITEFUNCTION
, simple_receive_http_data
);
402 curl_easy_setopt(curl_handle
, CURLOPT_WRITEDATA
, (void *)this);
403 curl_easy_setopt(curl_handle
, CURLOPT_ERRORBUFFER
, (void *)error_buf
);
404 curl_easy_setopt(curl_handle
, CURLOPT_LOW_SPEED_TIME
, cct
->_conf
->rgw_curl_low_speed_time
);
405 curl_easy_setopt(curl_handle
, CURLOPT_LOW_SPEED_LIMIT
, cct
->_conf
->rgw_curl_low_speed_limit
);
407 curl_easy_setopt(curl_handle
, CURLOPT_HTTPHEADER
, (void *)h
);
409 curl_easy_setopt(curl_handle
, CURLOPT_READFUNCTION
, simple_send_http_data
);
410 curl_easy_setopt(curl_handle
, CURLOPT_READDATA
, (void *)this);
411 if (is_upload_request(method
)) {
412 curl_easy_setopt(curl_handle
, CURLOPT_UPLOAD
, 1L);
415 curl_easy_setopt(curl_handle
, CURLOPT_INFILESIZE
, (void *)send_len
);
418 curl_easy_setopt(curl_handle
, CURLOPT_SSL_VERIFYPEER
, 0L);
419 curl_easy_setopt(curl_handle
, CURLOPT_SSL_VERIFYHOST
, 0L);
420 dout(20) << "ssl verification is set to off" << dendl
;
423 CURLcode status
= curl_easy_perform(curl_handle
);
425 dout(0) << "curl_easy_perform returned status " << status
<< " error: " << error_buf
<< dendl
;
428 curl_easy_getinfo(curl_handle
, CURLINFO_RESPONSE_CODE
, &http_status
);
429 handles
->release_curl_handle(ca
);
430 curl_slist_free_all(h
);
435 string
RGWHTTPClient::to_str()
437 string method_str
= (last_method
.empty() ? "<no-method>" : last_method
);
438 string url_str
= (last_url
.empty() ? "<no-url>" : last_url
);
439 return method_str
+ " " + url_str
;
442 int RGWHTTPClient::get_req_retcode()
448 return req_data
->get_retcode();
452 * init request, will be used later with RGWHTTPManager
454 int RGWHTTPClient::init_request(const char *method
, const char *url
, rgw_http_req_data
*_req_data
, bool send_data_hint
)
458 req_data
= _req_data
;
462 easy_handle
= curl_easy_init();
464 req_data
->easy_handle
= easy_handle
;
466 dout(20) << "sending request to " << url
<< dendl
;
468 curl_slist
*h
= headers_to_slist(headers
);
472 last_method
= (method
? method
: "");
473 last_url
= (url
? url
: "");
475 curl_easy_setopt(easy_handle
, CURLOPT_CUSTOMREQUEST
, method
);
476 curl_easy_setopt(easy_handle
, CURLOPT_URL
, url
);
477 curl_easy_setopt(easy_handle
, CURLOPT_NOPROGRESS
, 1L);
478 curl_easy_setopt(easy_handle
, CURLOPT_NOSIGNAL
, 1L);
479 curl_easy_setopt(easy_handle
, CURLOPT_HEADERFUNCTION
, receive_http_header
);
480 curl_easy_setopt(easy_handle
, CURLOPT_WRITEHEADER
, (void *)req_data
);
481 curl_easy_setopt(easy_handle
, CURLOPT_WRITEFUNCTION
, receive_http_data
);
482 curl_easy_setopt(easy_handle
, CURLOPT_WRITEDATA
, (void *)req_data
);
483 curl_easy_setopt(easy_handle
, CURLOPT_ERRORBUFFER
, (void *)req_data
->error_buf
);
484 curl_easy_setopt(easy_handle
, CURLOPT_LOW_SPEED_TIME
, cct
->_conf
->rgw_curl_low_speed_time
);
485 curl_easy_setopt(easy_handle
, CURLOPT_LOW_SPEED_LIMIT
, cct
->_conf
->rgw_curl_low_speed_limit
);
487 curl_easy_setopt(easy_handle
, CURLOPT_HTTPHEADER
, (void *)h
);
489 curl_easy_setopt(easy_handle
, CURLOPT_READFUNCTION
, send_http_data
);
490 curl_easy_setopt(easy_handle
, CURLOPT_READDATA
, (void *)req_data
);
491 if (send_data_hint
|| is_upload_request(method
)) {
492 curl_easy_setopt(easy_handle
, CURLOPT_UPLOAD
, 1L);
495 curl_easy_setopt(easy_handle
, CURLOPT_INFILESIZE
, (void *)send_len
);
498 curl_easy_setopt(easy_handle
, CURLOPT_SSL_VERIFYPEER
, 0L);
499 curl_easy_setopt(easy_handle
, CURLOPT_SSL_VERIFYHOST
, 0L);
500 dout(20) << "ssl verification is set to off" << dendl
;
502 curl_easy_setopt(easy_handle
, CURLOPT_PRIVATE
, (void *)req_data
);
508 * wait for async request to complete
510 int RGWHTTPClient::wait()
512 if (!req_data
->is_done()) {
513 return req_data
->wait();
516 return req_data
->ret
;
519 RGWHTTPClient::~RGWHTTPClient()
522 RGWHTTPManager
*http_manager
= req_data
->get_manager();
524 http_manager
->remove_request(this);
532 int RGWHTTPHeadersCollector::receive_header(void * const ptr
, const size_t len
)
534 const boost::string_ref
header_line(static_cast<const char * const>(ptr
), len
);
536 /* We're tokening the line that way due to backward compatibility. */
537 const size_t sep_loc
= header_line
.find_first_of(" \t:");
539 if (boost::string_ref::npos
== sep_loc
) {
540 /* Wrongly formatted header? Just skip it. */
544 header_name_t
name(header_line
.substr(0, sep_loc
));
545 if (0 == relevant_headers
.count(name
)) {
546 /* Not interested in this particular header. */
550 const auto value_part
= header_line
.substr(sep_loc
+ 1);
552 /* Skip spaces and tabs after the separator. */
553 const size_t val_loc_s
= value_part
.find_first_not_of(' ');
554 const size_t val_loc_e
= value_part
.find_first_of("\r\n");
556 if (boost::string_ref::npos
== val_loc_s
||
557 boost::string_ref::npos
== val_loc_e
) {
558 /* Empty value case. */
559 found_headers
.emplace(name
, header_value_t());
561 found_headers
.emplace(name
, header_value_t(
562 value_part
.substr(val_loc_s
, val_loc_e
- val_loc_s
)));
568 int RGWHTTPTransceiver::send_data(void* ptr
, size_t len
)
570 int length_to_copy
= 0;
571 if (post_data_index
< post_data
.length()) {
572 length_to_copy
= min(post_data
.length() - post_data_index
, len
);
573 memcpy(ptr
, post_data
.data() + post_data_index
, length_to_copy
);
574 post_data_index
+= length_to_copy
;
576 return length_to_copy
;
580 static int clear_signal(int fd
)
582 // since we're in non-blocking mode, we can try to read a lot more than
583 // one signal from signal_thread() to avoid later wakeups. non-blocking reads
584 // are also required to support the curl_multi_wait bug workaround
585 std::array
<char, 256> buf
;
586 int ret
= ::read(fd
, (void *)buf
.data(), buf
.size());
589 return ret
== -EAGAIN
? 0 : ret
; // clear EAGAIN
594 #if HAVE_CURL_MULTI_WAIT
596 static std::once_flag detect_flag
;
597 static bool curl_multi_wait_bug_present
= false;
599 static int detect_curl_multi_wait_bug(CephContext
*cct
, CURLM
*handle
,
600 int write_fd
, int read_fd
)
604 // write to write_fd so that read_fd becomes readable
606 ret
= ::write(write_fd
, &buf
, sizeof(buf
));
609 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): write() returned " << ret
<< dendl
;
613 // pass read_fd in extra_fds for curl_multi_wait()
615 struct curl_waitfd wait_fd
;
617 wait_fd
.fd
= read_fd
;
618 wait_fd
.events
= CURL_WAIT_POLLIN
;
621 ret
= curl_multi_wait(handle
, &wait_fd
, 1, 0, &num_fds
);
622 if (ret
!= CURLM_OK
) {
623 ldout(cct
, 0) << "ERROR: curl_multi_wait() returned " << ret
<< dendl
;
627 // curl_multi_wait should flag revents when extra_fd is readable. if it
628 // doesn't, the bug is present and we can't rely on revents
629 if (wait_fd
.revents
== 0) {
630 curl_multi_wait_bug_present
= true;
631 ldout(cct
, 0) << "WARNING: detected a version of libcurl which contains a "
632 "bug in curl_multi_wait(). enabling a workaround that may degrade "
633 "performance slightly." << dendl
;
636 return clear_signal(read_fd
);
639 static bool is_signaled(const curl_waitfd
& wait_fd
)
641 if (wait_fd
.fd
< 0) {
646 if (curl_multi_wait_bug_present
) {
647 // we can't rely on revents, so we always return true if a wait_fd is given.
648 // this means we'll be trying a non-blocking read on this fd every time that
649 // curl_multi_wait() wakes up
653 return wait_fd
.revents
> 0;
656 static int do_curl_wait(CephContext
*cct
, CURLM
*handle
, int signal_fd
)
659 struct curl_waitfd wait_fd
;
661 wait_fd
.fd
= signal_fd
;
662 wait_fd
.events
= CURL_WAIT_POLLIN
;
665 int ret
= curl_multi_wait(handle
, &wait_fd
, 1, cct
->_conf
->rgw_curl_wait_timeout_ms
, &num_fds
);
667 ldout(cct
, 0) << "ERROR: curl_multi_wait() returned " << ret
<< dendl
;
671 if (is_signaled(wait_fd
)) {
672 ret
= clear_signal(signal_fd
);
674 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): read() returned " << ret
<< dendl
;
683 static int do_curl_wait(CephContext
*cct
, CURLM
*handle
, int signal_fd
)
694 /* get file descriptors from the transfers */
695 int ret
= curl_multi_fdset(handle
, &fdread
, &fdwrite
, &fdexcep
, &maxfd
);
697 ldout(cct
, 0) << "ERROR: curl_multi_fdset returned " << ret
<< dendl
;
702 FD_SET(signal_fd
, &fdread
);
703 if (signal_fd
>= maxfd
) {
704 maxfd
= signal_fd
+ 1;
708 /* forcing a strict timeout, as the returned fdsets might not reference all fds we wait on */
709 uint64_t to
= cct
->_conf
->rgw_curl_wait_timeout_ms
;
710 #define RGW_CURL_TIMEOUT 1000
712 to
= RGW_CURL_TIMEOUT
;
713 struct timeval timeout
;
714 timeout
.tv_sec
= to
/ 1000;
715 timeout
.tv_usec
= to
% 1000;
717 ret
= select(maxfd
+1, &fdread
, &fdwrite
, &fdexcep
, &timeout
);
720 ldout(cct
, 0) << "ERROR: select returned " << ret
<< dendl
;
724 if (signal_fd
> 0 && FD_ISSET(signal_fd
, &fdread
)) {
725 ret
= clear_signal(signal_fd
);
727 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): read() returned " << ret
<< dendl
;
737 void *RGWHTTPManager::ReqsThread::entry()
739 manager
->reqs_thread_entry();
744 * RGWHTTPManager has two modes of operation: threaded and non-threaded.
746 RGWHTTPManager::RGWHTTPManager(CephContext
*_cct
, RGWCompletionManager
*_cm
) : cct(_cct
),
747 completion_mgr(_cm
), is_threaded(false),
748 reqs_lock("RGWHTTPManager::reqs_lock"), num_reqs(0), max_threaded_req(0),
751 multi_handle
= (void *)curl_multi_init();
756 RGWHTTPManager::~RGWHTTPManager() {
759 curl_multi_cleanup((CURLM
*)multi_handle
);
762 void RGWHTTPManager::register_request(rgw_http_req_data
*req_data
)
764 RWLock::WLocker
rl(reqs_lock
);
765 req_data
->id
= num_reqs
;
766 req_data
->registered
= true;
767 reqs
[num_reqs
] = req_data
;
769 ldout(cct
, 20) << __func__
<< " mgr=" << this << " req_data->id=" << req_data
->id
<< ", easy_handle=" << req_data
->easy_handle
<< dendl
;
772 void RGWHTTPManager::unregister_request(rgw_http_req_data
*req_data
)
774 RWLock::WLocker
rl(reqs_lock
);
776 req_data
->registered
= false;
777 unregistered_reqs
.push_back(req_data
);
778 ldout(cct
, 20) << __func__
<< " mgr=" << this << " req_data->id=" << req_data
->id
<< ", easy_handle=" << req_data
->easy_handle
<< dendl
;
781 void RGWHTTPManager::complete_request(rgw_http_req_data
*req_data
)
783 RWLock::WLocker
rl(reqs_lock
);
784 _complete_request(req_data
);
787 void RGWHTTPManager::_complete_request(rgw_http_req_data
*req_data
)
789 map
<uint64_t, rgw_http_req_data
*>::iterator iter
= reqs
.find(req_data
->id
);
790 if (iter
!= reqs
.end()) {
794 Mutex::Locker
l(req_data
->lock
);
795 req_data
->mgr
= nullptr;
797 if (completion_mgr
) {
798 completion_mgr
->complete(NULL
, req_data
->user_info
);
804 void RGWHTTPManager::finish_request(rgw_http_req_data
*req_data
, int ret
)
806 req_data
->finish(ret
);
807 complete_request(req_data
);
810 void RGWHTTPManager::_finish_request(rgw_http_req_data
*req_data
, int ret
)
812 req_data
->finish(ret
);
813 _complete_request(req_data
);
817 * hook request to the curl multi handle
819 int RGWHTTPManager::link_request(rgw_http_req_data
*req_data
)
821 ldout(cct
, 20) << __func__
<< " req_data=" << req_data
<< " req_data->id=" << req_data
->id
<< ", easy_handle=" << req_data
->easy_handle
<< dendl
;
822 CURLMcode mstatus
= curl_multi_add_handle((CURLM
*)multi_handle
, req_data
->easy_handle
);
824 dout(0) << "ERROR: failed on curl_multi_add_handle, status=" << mstatus
<< dendl
;
831 * unhook request from the curl multi handle, and finish request if it wasn't finished yet as
832 * there will be no more processing on this request
834 void RGWHTTPManager::_unlink_request(rgw_http_req_data
*req_data
)
836 if (req_data
->easy_handle
) {
837 curl_multi_remove_handle((CURLM
*)multi_handle
, req_data
->easy_handle
);
839 if (!req_data
->is_done()) {
840 _finish_request(req_data
, -ECANCELED
);
844 void RGWHTTPManager::unlink_request(rgw_http_req_data
*req_data
)
846 RWLock::WLocker
wl(reqs_lock
);
847 _unlink_request(req_data
);
850 void RGWHTTPManager::manage_pending_requests()
852 reqs_lock
.get_read();
853 if (max_threaded_req
== num_reqs
&& unregistered_reqs
.empty()) {
859 RWLock::WLocker
wl(reqs_lock
);
861 if (!unregistered_reqs
.empty()) {
862 for (auto& r
: unregistered_reqs
) {
867 unregistered_reqs
.clear();
870 map
<uint64_t, rgw_http_req_data
*>::iterator iter
= reqs
.find(max_threaded_req
);
872 list
<std::pair
<rgw_http_req_data
*, int> > remove_reqs
;
874 for (; iter
!= reqs
.end(); ++iter
) {
875 rgw_http_req_data
*req_data
= iter
->second
;
876 int r
= link_request(req_data
);
878 ldout(cct
, 0) << "ERROR: failed to link http request" << dendl
;
879 remove_reqs
.push_back(std::make_pair(iter
->second
, r
));
881 max_threaded_req
= iter
->first
+ 1;
885 for (auto piter
: remove_reqs
) {
886 rgw_http_req_data
*req_data
= piter
.first
;
887 int r
= piter
.second
;
889 _finish_request(req_data
, r
);
893 int RGWHTTPManager::add_request(RGWHTTPClient
*client
, const char *method
, const char *url
, bool send_data_hint
)
895 rgw_http_req_data
*req_data
= new rgw_http_req_data
;
897 int ret
= client
->init_request(method
, url
, req_data
, send_data_hint
);
904 req_data
->mgr
= this;
905 req_data
->client
= client
;
906 req_data
->user_info
= client
->get_user_info();
908 register_request(req_data
);
911 ret
= link_request(req_data
);
918 ret
= signal_thread();
920 finish_request(req_data
, ret
);
926 int RGWHTTPManager::remove_request(RGWHTTPClient
*client
)
928 rgw_http_req_data
*req_data
= client
->get_req_data();
931 unlink_request(req_data
);
934 unregister_request(req_data
);
935 int ret
= signal_thread();
944 * the synchronous, non-threaded request processing method.
946 int RGWHTTPManager::process_requests(bool wait_for_data
, bool *done
)
948 assert(!is_threaded
);
955 int ret
= do_curl_wait(cct
, (CURLM
*)multi_handle
, -1);
961 mstatus
= curl_multi_perform((CURLM
*)multi_handle
, &still_running
);
964 case CURLM_CALL_MULTI_PERFORM
:
967 dout(20) << "curl_multi_perform returned: " << mstatus
<< dendl
;
972 while ((msg
= curl_multi_info_read((CURLM
*)multi_handle
, &msgs_left
))) {
973 if (msg
->msg
== CURLMSG_DONE
) {
974 CURL
*e
= msg
->easy_handle
;
975 rgw_http_req_data
*req_data
;
976 curl_easy_getinfo(e
, CURLINFO_PRIVATE
, (void **)&req_data
);
979 curl_easy_getinfo(e
, CURLINFO_RESPONSE_CODE
, (void **)&http_status
);
981 int status
= rgw_http_error_to_errno(http_status
);
982 int result
= msg
->data
.result
;
983 finish_request(req_data
, status
);
988 dout(20) << "ERROR: msg->data.result=" << result
<< dendl
;
993 } while (mstatus
== CURLM_CALL_MULTI_PERFORM
);
995 *done
= (still_running
== 0);
1001 * the synchronous, non-threaded request processing completion method.
1003 int RGWHTTPManager::complete_requests()
1008 ret
= process_requests(true, &done
);
1009 } while (!done
&& !ret
);
1014 int RGWHTTPManager::set_threaded()
1016 int r
= pipe(thread_pipe
);
1019 ldout(cct
, 0) << "ERROR: pipe() returned errno=" << r
<< dendl
;
1023 // enable non-blocking reads
1024 r
= ::fcntl(thread_pipe
[0], F_SETFL
, O_NONBLOCK
);
1027 ldout(cct
, 0) << "ERROR: fcntl() returned errno=" << r
<< dendl
;
1028 TEMP_FAILURE_RETRY(::close(thread_pipe
[0]));
1029 TEMP_FAILURE_RETRY(::close(thread_pipe
[1]));
1033 #ifdef HAVE_CURL_MULTI_WAIT
1034 // on first initialization, use this pipe to detect whether we're using a
1035 // buggy version of libcurl
1036 std::call_once(detect_flag
, detect_curl_multi_wait_bug
, cct
,
1037 static_cast<CURLM
*>(multi_handle
),
1038 thread_pipe
[1], thread_pipe
[0]);
1042 reqs_thread
= new ReqsThread(this);
1043 reqs_thread
->create("http_manager");
1047 void RGWHTTPManager::stop()
1058 reqs_thread
->join();
1060 TEMP_FAILURE_RETRY(::close(thread_pipe
[1]));
1061 TEMP_FAILURE_RETRY(::close(thread_pipe
[0]));
1065 int RGWHTTPManager::signal_thread()
1068 int ret
= write(thread_pipe
[1], (void *)&buf
, sizeof(buf
));
1071 ldout(cct
, 0) << "ERROR: " << __func__
<< ": write() returned ret=" << ret
<< dendl
;
1077 void *RGWHTTPManager::reqs_thread_entry()
1082 ldout(cct
, 20) << __func__
<< ": start" << dendl
;
1084 while (!going_down
) {
1085 int ret
= do_curl_wait(cct
, (CURLM
*)multi_handle
, thread_pipe
[0]);
1087 dout(0) << "ERROR: do_curl_wait() returned: " << ret
<< dendl
;
1091 manage_pending_requests();
1093 mstatus
= curl_multi_perform((CURLM
*)multi_handle
, &still_running
);
1096 case CURLM_CALL_MULTI_PERFORM
:
1099 dout(10) << "curl_multi_perform returned: " << mstatus
<< dendl
;
1104 while ((msg
= curl_multi_info_read((CURLM
*)multi_handle
, &msgs_left
))) {
1105 if (msg
->msg
== CURLMSG_DONE
) {
1106 int result
= msg
->data
.result
;
1107 CURL
*e
= msg
->easy_handle
;
1108 rgw_http_req_data
*req_data
;
1109 curl_easy_getinfo(e
, CURLINFO_PRIVATE
, (void **)&req_data
);
1110 curl_multi_remove_handle((CURLM
*)multi_handle
, e
);
1113 curl_easy_getinfo(e
, CURLINFO_RESPONSE_CODE
, (void **)&http_status
);
1115 int status
= rgw_http_error_to_errno(http_status
);
1116 if (result
!= CURLE_OK
&& http_status
== 0) {
1119 int id
= req_data
->id
;
1120 finish_request(req_data
, status
);
1124 case CURLE_OPERATION_TIMEDOUT
:
1125 dout(0) << "WARNING: curl operation timed out, network average transfer speed less than "
1126 << cct
->_conf
->rgw_curl_low_speed_limit
<< " Bytes per second during " << cct
->_conf
->rgw_curl_low_speed_time
<< " seconds." << dendl
;
1128 dout(20) << "ERROR: msg->data.result=" << result
<< " req_data->id=" << id
<< " http_status=" << http_status
<< dendl
;
1136 RWLock::WLocker
rl(reqs_lock
);
1137 for (auto r
: unregistered_reqs
) {
1138 _finish_request(r
, -ECANCELED
);
1141 unregistered_reqs
.clear();
1143 auto all_reqs
= std::move(reqs
);
1144 for (auto iter
: all_reqs
) {
1145 _finish_request(iter
.second
, -ECANCELED
);
1150 if (completion_mgr
) {
1151 completion_mgr
->go_down();