Demonstrates how to lazily initialize the control client. Stores the
control client as an Arc<Mutex<Option<ScsControlClient<>>>>:
Since the cache client is cloneable, Arc allows it to be cloned
Mutex for exclusive access to lazily initialize only once
Option to track the state from uninitialized to initialized
A helper function get_control_client does
get-or-lazily-initialize. Uses another helper create_control_client
to instantiate it.
In this PR we've refactored the control requests to use this. The data
client refactor would be nearly identical but would take changes to
many request files. So we exclude that from this spike.
We could refactor this into its own component for re-use. It would parallel
the once_cell::sync::Lazy struct (see below).
Alternatives considered
There is a rust library called once_cell which has a Lazy type that takes an
initializer function. This has to be a pure function and cannot capture variables
from the outer scope (which we would need for the credential provider), so
we could not get that to work.
Overview
Demonstrates how to lazily initialize the control client. Stores the control client as an
Arc<Mutex<Option<ScsControlClient<>>>>
:A helper function
get_control_client
does get-or-lazily-initialize. Uses another helpercreate_control_client
to instantiate it.In this PR we've refactored the control requests to use this. The data client refactor would be nearly identical but would take changes to many request files. So we exclude that from this spike.
We could refactor this into its own component for re-use. It would parallel the
once_cell::sync::Lazy
struct (see below).Alternatives considered
There is a rust library called
once_cell
which has aLazy
type that takes an initializer function. This has to be a pure function and cannot capture variables from the outer scope (which we would need for the credential provider), so we could not get that to work.