Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-42628: A connection pool in the sync client API based on libcurl #828

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/http/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ add_library(http SHARED)
target_sources(http PRIVATE
AsyncReq.cc
Client.cc
ClientConnPool.cc
ClientConfig.cc
ClientConnPool.cc
Exceptions.cc
MetaModule.cc
Method.cc
Expand Down
234 changes: 133 additions & 101 deletions src/http/Client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,37 @@
#include "http/Client.h"

// Qserv headers
#include "http/ClientConnPool.h"
#include "http/Exceptions.h"

// Standard headers
#include <algorithm>
#include <cassert>
#include <iostream>
#include <stdexcept>
#include <vector>

using namespace std;
using json = nlohmann::json;

namespace lsst::qserv::http {

string const ClientConfig::category = "worker-http-file-reader";

string const ClientConfig::sslVerifyHostKey = "SSL_VERIFYHOST";
string const ClientConfig::sslVerifyPeerKey = "SSL_VERIFYPEER";
string const ClientConfig::caPathKey = "CAPATH";
string const ClientConfig::caInfoKey = "CAINFO";
string const ClientConfig::caInfoValKey = "CAINFO_VAL";

string const ClientConfig::proxySslVerifyHostKey = "PROXY_SSL_VERIFYHOST";
string const ClientConfig::proxySslVerifyPeerKey = "PROXY_SSL_VERIFYPEER";
string const ClientConfig::proxyCaPathKey = "PROXY_CAPATH";
string const ClientConfig::proxyCaInfoKey = "PROXY_CAINFO";
string const ClientConfig::proxyCaInfoValKey = "PROXY_CAINFO_VAL";

string const ClientConfig::proxyKey = "CURLOPT_PROXY";
string const ClientConfig::noProxyKey = "CURLOPT_NOPROXY";
string const ClientConfig::httpProxyTunnelKey = "CURLOPT_HTTPPROXYTUNNEL";

string const ClientConfig::connectTimeoutKey = "CONNECTTIMEOUT";
string const ClientConfig::timeoutKey = "TIMEOUT";
string const ClientConfig::lowSpeedLimitKey = "LOW_SPEED_LIMIT";
string const ClientConfig::lowSpeedTimeKey = "LOW_SPEED_TIME";

string const ClientConfig::asyncProcLimitKey = "ASYNC_PROC_LIMIT";

size_t forwardToClient(char* ptr, size_t size, size_t nmemb, void* userdata) {
size_t forwardToClient(char* ptr, size_t size, size_t nmemb, void* client) {
size_t const nchars = size * nmemb;
Client* reader = reinterpret_cast<Client*>(userdata);
reader->_store(ptr, nchars);
reinterpret_cast<Client*>(client)->_store(ptr, nchars);
return nchars;
}

Client::Client(http::Method method, string const& url, string const& data, vector<string> const& headers,
ClientConfig const& clientConfig)
: _method(method), _url(url), _data(data), _headers(headers), _clientConfig(clientConfig) {
ClientConfig const& clientConfig, shared_ptr<ClientConnPool> const& connPool)
: _method(method),
_url(url),
_data(data),
_headers(headers),
_clientConfig(clientConfig),
_connPool(connPool) {
_hcurl = curl_easy_init();
assert(_hcurl != nullptr); // curl_easy_init() failed to allocate memory, etc.
assert(_hcurl != nullptr);
}

Client::~Client() {
Expand All @@ -81,110 +62,161 @@ Client::~Client() {
}

void Client::read(CallbackType const& onDataRead) {
assert(onDataRead != nullptr); // no callback function provided
string const context = "Client::" + string(__func__) + " ";
if (onDataRead == nullptr) {
throw invalid_argument("http::Client::" + string(__func__) + ": no callback provided");
}
_onDataRead = onDataRead;
_errorChecked("curl_easy_setopt(CURLOPT_URL)", curl_easy_setopt(_hcurl, CURLOPT_URL, _url.c_str()));
_errorChecked("curl_easy_setopt(CURLOPT_CUSTOMREQUEST)",
curl_easy_setopt(_hcurl, CURLOPT_CUSTOMREQUEST, nullptr));

_setConnOptions();
_setSslCertOptions();
_setProxyOptions();

_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_URL)",
curl_easy_setopt(_hcurl, CURLOPT_URL, _url.c_str()));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_CUSTOMREQUEST)",
curl_easy_setopt(_hcurl, CURLOPT_CUSTOMREQUEST, nullptr));
if (_method == http::Method::GET) {
_errorChecked("curl_easy_setopt(CURLOPT_HTTPGET)", curl_easy_setopt(_hcurl, CURLOPT_HTTPGET, 1L));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_HTTPGET)",
curl_easy_setopt(_hcurl, CURLOPT_HTTPGET, 1L));
} else if (_method == http::Method::POST) {
_errorChecked("curl_easy_setopt(CURLOPT_POST)", curl_easy_setopt(_hcurl, CURLOPT_POST, 1L));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_POST)", curl_easy_setopt(_hcurl, CURLOPT_POST, 1L));
} else {
_errorChecked("curl_easy_setopt(CURLOPT_CUSTOMREQUEST)",
curl_easy_setopt(_hcurl, CURLOPT_CUSTOMREQUEST, http::method2string(_method).data()));
_curlEasyErrorChecked(
"curl_easy_setopt(CURLOPT_CUSTOMREQUEST)",
curl_easy_setopt(_hcurl, CURLOPT_CUSTOMREQUEST, http::method2string(_method).data()));
}
if (!_data.empty()) {
_errorChecked("curl_easy_setopt(CURLOPT_POSTFIELDS)",
curl_easy_setopt(_hcurl, CURLOPT_POSTFIELDS, _data.c_str()));
_errorChecked("curl_easy_setopt(CURLOPT_POSTFIELDSIZE)",
curl_easy_setopt(_hcurl, CURLOPT_POSTFIELDSIZE, _data.size()));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_POSTFIELDS)",
curl_easy_setopt(_hcurl, CURLOPT_POSTFIELDS, _data.c_str()));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_POSTFIELDSIZE)",
curl_easy_setopt(_hcurl, CURLOPT_POSTFIELDSIZE, _data.size()));
}
curl_slist_free_all(_hlist);
_hlist = nullptr;
for (auto& header : _headers) {
_hlist = curl_slist_append(_hlist, header.c_str());
}
_errorChecked("curl_easy_setopt(CURLOPT_HTTPHEADER)",
curl_easy_setopt(_hcurl, CURLOPT_HTTPHEADER, _hlist));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_HTTPHEADER)",
curl_easy_setopt(_hcurl, CURLOPT_HTTPHEADER, _hlist));

_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_FAILONERROR)",
curl_easy_setopt(_hcurl, CURLOPT_FAILONERROR, 1L));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_WRITEFUNCTION)",
curl_easy_setopt(_hcurl, CURLOPT_WRITEFUNCTION, forwardToClient));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_WRITEDATA)",
curl_easy_setopt(_hcurl, CURLOPT_WRITEDATA, this));
_curlEasyErrorChecked("curl_easy_perform()", curl_easy_perform(_hcurl));
}

json Client::readAsJson() {
vector<char> data;
this->read([&data](char const* buf, size_t size) { data.insert(data.cend(), buf, buf + size); });
return json::parse(data);
}

void Client::_setConnOptions() {
if (_clientConfig.httpVersion != CURL_HTTP_VERSION_NONE) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_HTTP_VERSION)",
curl_easy_setopt(_hcurl, CURLOPT_HTTP_VERSION, _clientConfig.httpVersion));
}
if (_clientConfig.bufferSize > 0) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_BUFFERSIZE)",
curl_easy_setopt(_hcurl, CURLOPT_BUFFERSIZE, _clientConfig.bufferSize));
}
if (_clientConfig.connectTimeout > 0) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_CONNECTTIMEOUT)",
curl_easy_setopt(_hcurl, CURLOPT_CONNECTTIMEOUT, _clientConfig.connectTimeout));
}
if (_clientConfig.timeout > 0) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_TIMEOUT)",
curl_easy_setopt(_hcurl, CURLOPT_TIMEOUT, _clientConfig.timeout));
}
if (_clientConfig.lowSpeedLimit > 0) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_LOW_SPEED_LIMIT)",
curl_easy_setopt(_hcurl, CURLOPT_LOW_SPEED_LIMIT, _clientConfig.lowSpeedLimit));
}
if (_clientConfig.lowSpeedTime > 0) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_LOW_SPEED_TIME)",
curl_easy_setopt(_hcurl, CURLOPT_LOW_SPEED_TIME, _clientConfig.lowSpeedTime));
}
if (_clientConfig.tcpKeepAlive) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_TCP_KEEPALIVE)",
curl_easy_setopt(_hcurl, CURLOPT_TCP_KEEPALIVE, 1L));
if (_clientConfig.tcpKeepIdle > 0) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_TCP_KEEPIDLE)",
curl_easy_setopt(_hcurl, CURLOPT_TCP_KEEPIDLE, _clientConfig.tcpKeepIdle));
}
if (_clientConfig.tcpKeepIntvl > 0) {
_curlEasyErrorChecked(
"curl_easy_setopt(CURLOPT_TCP_KEEPINTVL)",
curl_easy_setopt(_hcurl, CURLOPT_TCP_KEEPINTVL, _clientConfig.tcpKeepIntvl));
}
}
if (_connPool != nullptr) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_SHARE)",
curl_easy_setopt(_hcurl, CURLOPT_SHARE, _connPool->shareCurl()));
if (_connPool->maxConnections() > 0) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_MAXCONNECTS)",
curl_easy_setopt(_hcurl, CURLOPT_MAXCONNECTS, _connPool->maxConnections()));
}
}
}

// Optional settings for the peer's cert
void Client::_setSslCertOptions() {
if (!_clientConfig.sslVerifyHost) {
_errorChecked("curl_easy_setopt(CURLOPT_SSL_VERIFYHOST)",
curl_easy_setopt(_hcurl, CURLOPT_SSL_VERIFYHOST, 0L));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_SSL_VERIFYHOST)",
curl_easy_setopt(_hcurl, CURLOPT_SSL_VERIFYHOST, 0L));
}
if (_clientConfig.sslVerifyPeer) {
if (!_clientConfig.caPath.empty()) {
_errorChecked("curl_easy_setopt(CURLOPT_CAPATH)",
curl_easy_setopt(_hcurl, CURLOPT_CAPATH, _clientConfig.caPath.c_str()));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_CAPATH)",
curl_easy_setopt(_hcurl, CURLOPT_CAPATH, _clientConfig.caPath.c_str()));
}
if (!_clientConfig.caInfo.empty()) {
_errorChecked("curl_easy_setopt(CURLOPT_CAINFO)",
curl_easy_setopt(_hcurl, CURLOPT_CAINFO, _clientConfig.caInfo.c_str()));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_CAINFO)",
curl_easy_setopt(_hcurl, CURLOPT_CAINFO, _clientConfig.caInfo.c_str()));
}
} else {
_errorChecked("curl_easy_setopt(CURLOPT_SSL_VERIFYPEER)",
curl_easy_setopt(_hcurl, CURLOPT_SSL_VERIFYPEER, 0L));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_SSL_VERIFYPEER)",
curl_easy_setopt(_hcurl, CURLOPT_SSL_VERIFYPEER, 0L));
}
}

// Optional settings for the proxy's cert
void Client::_setProxyOptions() {
if (!_clientConfig.proxy.empty()) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_PROXY)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY, _clientConfig.proxy.c_str()));
if (_clientConfig.httpProxyTunnel != 0) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_HTTPPROXYTUNNEL)",
curl_easy_setopt(_hcurl, CURLOPT_HTTPPROXYTUNNEL, 1L));
}
}
if (!_clientConfig.noProxy.empty()) {
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_NOPROXY)",
curl_easy_setopt(_hcurl, CURLOPT_NOPROXY, _clientConfig.noProxy.c_str()));
}
if (!_clientConfig.proxySslVerifyHost) {
_errorChecked("curl_easy_setopt(CURLOPT_PROXY_SSL_VERIFYHOST)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY_SSL_VERIFYHOST, 0L));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_PROXY_SSL_VERIFYHOST)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY_SSL_VERIFYHOST, 0L));
}
if (_clientConfig.proxySslVerifyPeer) {
if (!_clientConfig.proxyCaPath.empty()) {
_errorChecked("curl_easy_setopt(CURLOPT_PROXY_CAPATH)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY_CAPATH, _clientConfig.proxyCaPath.c_str()));
_curlEasyErrorChecked(
"curl_easy_setopt(CURLOPT_PROXY_CAPATH)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY_CAPATH, _clientConfig.proxyCaPath.c_str()));
}
if (!_clientConfig.proxyCaInfo.empty()) {
_errorChecked("curl_easy_setopt(CURLOPT_PROXY_CAINFO)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY_CAINFO, _clientConfig.proxyCaInfo.c_str()));
_curlEasyErrorChecked(
"curl_easy_setopt(CURLOPT_PROXY_CAINFO)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY_CAINFO, _clientConfig.proxyCaInfo.c_str()));
}
} else {
_errorChecked("curl_easy_setopt(CURLOPT_PROXY_SSL_VERIFYPEER)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY_SSL_VERIFYPEER, 0L));
_curlEasyErrorChecked("curl_easy_setopt(CURLOPT_PROXY_SSL_VERIFYPEER)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY_SSL_VERIFYPEER, 0L));
}

// Optional settings for proxies
if (!_clientConfig.proxy.empty()) {
_errorChecked("curl_easy_setopt(CURLOPT_PROXY)",
curl_easy_setopt(_hcurl, CURLOPT_PROXY, _clientConfig.proxy.c_str()));
if (_clientConfig.httpProxyTunnel != 0) {
_errorChecked("curl_easy_setopt(CURLOPT_HTTPPROXYTUNNEL)",
curl_easy_setopt(_hcurl, CURLOPT_HTTPPROXYTUNNEL, 1L));
}
}
if (!_clientConfig.noProxy.empty()) {
_errorChecked("curl_easy_setopt(CURLOPT_NOPROXY)",
curl_easy_setopt(_hcurl, CURLOPT_NOPROXY, _clientConfig.noProxy.c_str()));
}

// Optional settings for timing and performance of the transfer
_errorChecked("curl_easy_setopt(CURLOPT_CONNECTTIMEOUT)",
curl_easy_setopt(_hcurl, CURLOPT_CONNECTTIMEOUT, _clientConfig.connectTimeout));
_errorChecked("curl_easy_setopt(CURLOPT_TIMEOUT)",
curl_easy_setopt(_hcurl, CURLOPT_TIMEOUT, _clientConfig.timeout));
_errorChecked("curl_easy_setopt(CURLOPT_LOW_SPEED_LIMIT)",
curl_easy_setopt(_hcurl, CURLOPT_LOW_SPEED_LIMIT, _clientConfig.lowSpeedLimit));
_errorChecked("curl_easy_setopt(CURLOPT_LOW_SPEED_TIME)",
curl_easy_setopt(_hcurl, CURLOPT_LOW_SPEED_TIME, _clientConfig.lowSpeedTime));

_errorChecked("curl_easy_setopt(CURLOPT_FAILONERROR)", curl_easy_setopt(_hcurl, CURLOPT_FAILONERROR, 1L));
_errorChecked("curl_easy_setopt(CURLOPT_WRITEFUNCTION)",
curl_easy_setopt(_hcurl, CURLOPT_WRITEFUNCTION, forwardToClient));
_errorChecked("curl_easy_setopt(CURLOPT_WRITEDATA)", curl_easy_setopt(_hcurl, CURLOPT_WRITEDATA, this));
_errorChecked("curl_easy_perform()", curl_easy_perform(_hcurl));
}

json Client::readAsJson() {
vector<char> data;
this->read([&data](char const* buf, size_t size) { data.insert(data.cend(), buf, buf + size); });
return json::parse(data);
}

void Client::_errorChecked(string const& scope, CURLcode errnum) {
void Client::_curlEasyErrorChecked(string const& scope, CURLcode errnum) {
if (errnum != CURLE_OK) {
string errorStr = curl_easy_strerror(errnum);
long httpResponseCode = 0;
Expand Down
Loading
Loading