launchdarkly / cpp-sdks

C++ Client/Server SDKs
Other
5 stars 3 forks source link

Does client-side SDK supports multiple client objects connecting to different projects/environments #469

Closed TouHouPolice closed 3 days ago

TouHouPolice commented 3 days ago

Is this a support request? No.

Describe the bug This is more of a question than a bug report. Reading your documentations and coming from deprecated SDK 2.x, my understanding is that the launchdarkly::client_side::Client object is internally a global object/singleton. However, I've experimented with the latest SDK and created multiple launchdarkly::client_side::Client objects initialised with different Mobile Keys and they seem to evaluate flags just fine. I want to know what I'm missing here. My concern is that what if an application needs to access feature flags from multiple LaunchDarkly projects/environments? I want to understand if that's possible, what problems would I have.

To reproduce It's not a bug, but it seems multiple clients actually work? Basically I'm doing this twice:

...
auto client_1= std::make_unique<launchdarkly::client_side::Client>(*config, context);
auto start_result_1 = client_1->StartAsync();
...
auto client_2= std::make_unique<launchdarkly::client_side::Client>(*config, context);
auto start_result_2 = client_2->StartAsync();
...
//Evaluate flags using both client_1 and client_2

Expected behavior Would it be undefined behaviour internally?

Logs N/A

SDK version v3.x

Language version, developer tools C++ 17

OS/platform Apple M3 Pro

Additional context The context here is that I'm working on a tool and I need to let my users understand what's possible and what's prohibited before adopting my tool. Please advice. I'd really appreciate if you can provide some related information for the deprecated v2.x SDK as well. Thank you.

cwaldren-ld commented 3 days ago

Hi @TouHouPolice,

Yes, multiple clients with different mobile keys should work independently. It would also work if they shared the same key, but it's not recommended. To answer your question - what you are doing in the code sample should be correct.

This is what the docs right above this anchor link are mentioning:

Client must be a singleton It's important to make Client a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.

If you have multiple LaunchDarkly projects, you can create one Client for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.

The reason this is so strongly worded (must be a singleton..) is not for safety but because of the resource consumption. Many customers will do something like instantiate an SDK every time a feature flag needs to be evaluated, which is highly inefficient.

If you encounter any bugs related to using multiple clients in the same app, please let us know.

As some more background, the deprecated v2.x C Client SDK was meant to be used as a singleton because it used global state. It was capable of handling multiple mobile keys at once. Additionally, it used up to 3 threads during operation.

In contrast, the C++ SDK handles a single key and all IO is performed asynchronously on a single thread.

TouHouPolice commented 3 days ago

@cwaldren-ld Thank you for your prompt response, this has perfectly answered my question. Much appreciated.