Do global curl init / cleanup only once.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2020-01-05 19:07:07 +01:00
parent 159cd05f5a
commit be2f00faae
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 20 additions and 3 deletions

View File

@ -30,6 +30,8 @@ namespace mastodonpp
using std::string;
using std::string_view;
extern uint16_t curl_inits;
/*!
* @brief The HTTP method.
*
@ -59,7 +61,8 @@ public:
/*!
* @brief Initializes curl and sets up connection.
*
* Calls `curl_global_init`, which is not thread-safe. For more information
* The first time an instance of CURLWrapper is created, it calls
* `curl_global_init`, which is not thread-safe. For more information
* consult [curl_global_init(3)]
* (https://curl.haxx.se/libcurl/c/curl_global_init.html).
*

View File

@ -26,17 +26,31 @@ namespace mastodonpp
using std::uint8_t;
using std::uint16_t;
uint16_t curlwrapper_instances{0};
CURLWrapper::CURLWrapper()
: _curl_buffer_error{}
{
curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
if (curlwrapper_instances == 0)
{
curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
}
++curlwrapper_instances;
debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (+1)\n";
_connection = curl_easy_init();
setup_curl();
}
CURLWrapper::~CURLWrapper() noexcept
{
curl_easy_cleanup(_connection);
curl_global_cleanup();
--curlwrapper_instances;
debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
if (curlwrapper_instances == 0)
{
curl_global_cleanup();
}
}
answer_type CURLWrapper::make_request(const http_method &method,