momentohq / client-sdk-rust

Official Rust SDK for Momento Serverless Cache
Apache License 2.0
12 stars 4 forks source link

refactor: lazily initialize control client #391

Open malandis opened 2 months ago

malandis commented 2 months ago

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 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.