How to handle multi-threaded usage and libCURL
masterThread Safety
Each ppconsul::Consul object maintains a pool of HTTP(S) clients. It is safe to call endpoint objects (like Kv, Agent, etc.) or the Consul object itself from multiple threads simultaneously.
Aborting Blocking Queries
To gracefully shut down and abort ongoing blocking queries, use Consul::stop().
- Warning: This call is irreversible. Once called, the
Consulobject enters a stopped state forever and no further requests can be made. - Requirement: You must enable this feature at construction time using
kw::enable_stop = true.
Consul consul(kw::enable_stop = true);
// ... perform work ...
consul.stop(); // Aborts pending requestslibCURL Initialization
Ppconsul calls curl_global_init automatically when makeDefaultHttpClientFactory() is first called (usually during Consul construction).
If you need manual control over libCURL initialization (e.g., to ensure it happens before any threads are spawned), you have two options:
- Manual Global Init: Call
curl_global_initandcurl_global_cleanupyourself in yourmain()function. - Skip Ppconsul Auto-Init: Create a factory explicitly with
makeDefaultHttpClientFactory(false)and pass it to theConsulconstructor.
// Option 2: Skip auto-init
Consul consul(makeDefaultHttpClientFactory(false), ...);// Enable stopping and abort queries
Consul consul(kw::enable_stop = true);
consul.stop();
// Skip automatic libCURL initialization
Consul consul(makeDefaultHttpClientFactory(false), ...);