MXS-2208 Add init/finish functions for mxb::http
Init/finish needed, as curl otherwise will leak memory.
This commit is contained in:
@ -24,6 +24,37 @@ namespace maxbase
|
|||||||
namespace http
|
namespace http
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the http library.
|
||||||
|
*
|
||||||
|
* @return True if successful, false otherwise.
|
||||||
|
*/
|
||||||
|
bool init();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finalize the http library.
|
||||||
|
*/
|
||||||
|
void finish();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RAII class for initializing the http functionality.
|
||||||
|
*/
|
||||||
|
class Init
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Init()
|
||||||
|
{
|
||||||
|
if (!mxb::http::init())
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Could not initialize mxb::http.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
~Init()
|
||||||
|
{
|
||||||
|
mxb::http::finish();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
DEFAULT_CONNECT_TIMEOUT = 10, // @see https://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html
|
DEFAULT_CONNECT_TIMEOUT = 10, // @see https://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html
|
||||||
|
@ -33,6 +33,14 @@ using std::vector;
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
|
static struct THIS_UNIT
|
||||||
|
{
|
||||||
|
int nInits;
|
||||||
|
} this_unit =
|
||||||
|
{
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
using namespace mxb;
|
using namespace mxb;
|
||||||
using namespace mxb::http;
|
using namespace mxb::http;
|
||||||
|
|
||||||
@ -421,6 +429,38 @@ namespace maxbase
|
|||||||
namespace http
|
namespace http
|
||||||
{
|
{
|
||||||
|
|
||||||
|
bool init()
|
||||||
|
{
|
||||||
|
bool rv = true;
|
||||||
|
|
||||||
|
if (this_unit.nInits == 0)
|
||||||
|
{
|
||||||
|
CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
|
||||||
|
if (code == CURLE_OK)
|
||||||
|
{
|
||||||
|
this_unit.nInits = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MXB_ERROR("Failed to initialize CURL library: %s", curl_easy_strerror(code));
|
||||||
|
rv = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
void finish()
|
||||||
|
{
|
||||||
|
mxb_assert(this_unit.nInits > 0);
|
||||||
|
|
||||||
|
if (--this_unit.nInits == 0)
|
||||||
|
{
|
||||||
|
curl_global_cleanup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Async::Imp::~Imp()
|
Async::Imp::~Imp()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -160,6 +160,8 @@ int main()
|
|||||||
long start;
|
long start;
|
||||||
long stop;
|
long stop;
|
||||||
|
|
||||||
|
mxb::http::Init init;
|
||||||
|
|
||||||
start = time_since_epoch_ms();
|
start = time_since_epoch_ms();
|
||||||
rv += test_http();
|
rv += test_http();
|
||||||
stop = time_since_epoch_ms();
|
stop = time_since_epoch_ms();
|
||||||
|
Reference in New Issue
Block a user