launchdarkly / node-server-sdk

LaunchDarkly Server-side SDK for Node
Other
79 stars 65 forks source link

Automatically handle initialization on the API client side #252

Closed vvo closed 1 year ago

vvo commented 2 years ago

Is your feature request related to a problem? Please describe. Hi there. I find the documentation on the subject of "you have to wait for the client to be initialized" to be confusing. I am still unsure if I need to wait for the client to do variation() calls. I believe I need to do it. This is not practical as it requires to either:

As I see it, initialization is an implementation detail from the user's point of view (me). I should not have to deal with client initialization. Ideally, you have an internal queue that you use so that all variation() calls are being queued until the client is ready.

Asking every user to wait for launchdarkly before accepting requests, before using the client, is not providing a great developer experience.

Describe the solution you'd like

const ldClient = ld(process.env.LD_API_KEY);
server.listen((req, res) => {
  const useYellowDashboard = ldClient.variation('yellow-dashboard', { id: req.user.id }, false);
  if (useYellowDashboard) ...
});

Everything else is an implementation detail and should be hidden. Ideally, there's a timeout handling inside the client (configurable) too. And the default value of the flag is used on timeout.

kinyoklion commented 2 years ago

Hello @vvo,

When, and how, to wait for initialization varies based on the type of application and how it is using the SDK. For instance it may not be appropriate for the SDK to block on initialization when it is instantiated, that would be a major inconvenience for many applications. It may also not be desirable to introduce the possibility of additional blocking in the variation method. As there is IO involved in initialization it is also typical to do this asynchronously.

We do provide asynchronous ways to know when the initialization is complete. This can either be using async/await, or using callbacks.

For the simplest case, where you want initialization overall to be blocked you can do something like:

await client.waitForInitialization();

//Further initialization.

You could also put further initialization code inside a .then callback or using the ready event supported by the SDK.

In some situations variation methods may not be needed until further in initialization, or they may not be needed at all until specific routes or functionality is used.

If you use variation methods before initialization is complete, then those methods will log a warning to inform you they were used before initialization was complete. Depending on the configuration of your SDK you will either get the default variation, or operate on stale data.

If you only care about initialization at specific points, then you can conditionally access them, risk the possibility of a default value and a warning, or await/chain the initialization and variation methods at that point in time.

The variation methods themselves are async, because specific configurations of the SDK may involve IO during the course of a variation call. We do try to limit the amount of code, and the opportunity for asynchronous access, in these methods. As a result they do not await initialization themselves, and we leave that choice to the developer.

Thanks, Ryan