launchdarkly / erlang-server-sdk

LaunchDarkly Server-Side SDK for Erlang/Elixir
Other
34 stars 36 forks source link

Question: Supervising the Client #26

Closed aj-foster closed 4 years ago

aj-foster commented 4 years ago

Hello there 👋

Thank you for your work on this client. This might be a naive question, so please accept my apologies if the answer is obvious or already documented.

Is there a recommended way to start this client in a supervision tree?

Presently, we wrap our call to ldclient:start_instance/1 in Elixir's Task.start_link/1, which returns a PID in such a way that the supervisor can restart the client if something happens. We aren't certain that this is necessary, but it provides a layer of protection while allowing us to configure the SDK key at runtime.

When considering an ideal solution (as mentioned in the PR prompt), I imagine that there could be a start_link function available on the ldclient that accepts the SDK key as an argument and returns the {ok, Pid} or {ok, Pid, Info} tuple expected by supervisors.

As a possible extension, the supervised process could return ignore if an SDK key is not provided. This would provide a nice default in the cases of development or testing when an SDK key isn't available.

Thanks again. 🙂

zurab-darkly commented 4 years ago

Hi @aj-foster, thanks for the question.

Currently when you call the ldclient:start_instance/1-3 the dynamically created supervision tree under ldclient_instance_sup (streaming/polling updater, storage, events) is attached to ldclient_sup - the top level simple_one_for_one supervisor for the ldclient OTP application.

Are you suggesting that it would make more sense, or to at least have an option, to supervise the ldclient_instance_sup outside of the ldclient application?

Additionally, can you clarify how you'd expect the SDK to behave when starting an instance without the SDK key being available? Currently, one way to do this would be to start the instance in an offline mode (i.e. set offline => true in options passed to start_instance).

aj-foster commented 4 years ago

Okay, I think I'm starting to understand.

Is it true that ldclient_sup is started / supervised by the ldclient application even if there are no instances running? And once an instance gets started, its supervisor can be restarted by ldclient_sup if something happens?

In this case my supervision of the process that calls ldclient:start_instance/1-3 appears unnecessary. Instead we only need to call the function once during application startup if we have an SDK key available (addressing the second question).

zurab-darkly commented 4 years ago

@aj-foster that's correct. You might also want to start it in offline mode in environments where you have no desire to connect to the service so that you have the same execution path; but note that currently this SDK doesn't yet support reading flags from a file as described in above linked page, so your ldclient:variation/3-4 calls will always return the default values you specify in those calls.