Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "open-feature"
version = "0.2.7"
edition = "2021"
rust-version = "1.77.0" # MSRV
rust-version = "1.80.1" # MSRV
description = "The official OpenFeature Rust SDK."
documentation = "https://docs.rs/open-feature"
readme = "README.md"
Expand All @@ -17,11 +17,10 @@ maintenance = { status = "actively-developed" }

[dependencies]
async-trait = "0.1.80"
lazy_static = "1.5"
mockall = { version = "0.14.0", optional = true }
serde_json = { version = "1.0.116", optional = true }
time = "0.3.36"
tokio = { version = "1.40", features = ["full"] }
tokio = { version = "1.40", features = ["sync"] }
typed-builder = "0.22.0"

log = { package = "log", version = "0.4", optional = true }
Expand All @@ -30,6 +29,7 @@ log = { package = "log", version = "0.4", optional = true }
env_logger = "0.11.5"
structured-logger = "1.0.3"
spec = { path = "spec" }
tokio = { version = "1.40", features = ["sync", "rt-multi-thread", "macros"] }

[features]
default = ["test-util", "dep:log"]
Expand Down
19 changes: 11 additions & 8 deletions src/api/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use lazy_static::lazy_static;
use std::sync::OnceLock;

use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};

use crate::{
Expand All @@ -11,14 +12,16 @@ use super::{
provider_registry::ProviderRegistry,
};

lazy_static! {
/// The singleton instance of [`OpenFeature`] struct.
/// The client should always use this instance to access OpenFeature APIs.
static ref SINGLETON: RwLock<OpenFeature> = RwLock::new(OpenFeature::default());
/// The singleton instance of [`OpenFeature`] struct.
/// The client should always use this instance to access OpenFeature APIs.
static SINGLETON: OnceLock<RwLock<OpenFeature>> = OnceLock::new();

fn get_singleton() -> &'static RwLock<OpenFeature> {
SINGLETON.get_or_init(|| RwLock::new(OpenFeature::default()))
}

/// THE struct of the OpenFeature API.
/// Access it via the [`SINGLETON`] instance.
/// Access it via [`OpenFeature::singleton()`] or [`OpenFeature::singleton_mut()`].
#[derive(Default)]
pub struct OpenFeature {
evaluation_context: GlobalEvaluationContext,
Expand All @@ -30,12 +33,12 @@ pub struct OpenFeature {
impl OpenFeature {
/// Get the singleton of [`OpenFeature`].
pub async fn singleton() -> RwLockReadGuard<'static, Self> {
SINGLETON.read().await
get_singleton().read().await
}

/// Get a mutable singleton of [`OpenFeature`].
pub async fn singleton_mut() -> RwLockWriteGuard<'static, Self> {
SINGLETON.write().await
get_singleton().write().await
}

/// Set the global evaluation context.
Expand Down