pub struct StatsigClient { /* private fields */ }Expand description
A high-performance, async client for Statsig feature flags and dynamic configs.
§Architecture
The client uses a multi-layered architecture:
- API Layer: Handles HTTP communication with Statsig servers
- Cache Layer: Provides intelligent caching with TTL support
- Batch Layer: Optimizes multiple requests into single API calls
§Performance Characteristics
- Cache hit latency: ~1ms
- API call latency: ~100ms (network dependent)
- Batch processing: Reduces API calls by up to 90%
Implementations§
Source§impl StatsigClient
impl StatsigClient
Sourcepub async fn with_config(config: StatsigClientConfig) -> Result<Self>
pub async fn with_config(config: StatsigClientConfig) -> Result<Self>
pub async fn log_event( &self, event_name: impl Into<String>, user: &User, ) -> Result<bool>
pub async fn log_events( &self, events: Vec<StatsigEvent>, user: &User, ) -> Result<LogEventResponse>
Sourcepub async fn check_gate(
&self,
gate_name: impl Into<String>,
user: &User,
) -> Result<bool>
pub async fn check_gate( &self, gate_name: impl Into<String>, user: &User, ) -> Result<bool>
Check if a single feature gate passes for a user
This method first checks the cache for a recent evaluation, falling back to the Statsig API if needed. Results are automatically cached for the configured TTL duration.
§Arguments
gate_name- The name of the feature gate to check (2-100 characters)user- The user to evaluate the gate for
§Returns
Ok(true) if the gate passes, Ok(false) if it doesn’t, or an error
if the evaluation fails.
§Errors
StatsigError::Validationif the gate name or user is invalidStatsigError::Networkif the API request failsStatsigError::Apiif the server returns an error response
§Performance
- Cache hit: ~1ms
- Cache miss: ~100ms (network dependent)
Sourcepub async fn check_gates(
&self,
gate_names: Vec<String>,
user: &User,
) -> Result<HashMap<String, bool>>
pub async fn check_gates( &self, gate_names: Vec<String>, user: &User, ) -> Result<HashMap<String, bool>>
Check multiple feature gates for a user
This method efficiently checks multiple gates in a single API call when cache misses occur, significantly reducing network overhead.
§Arguments
gate_names- List of gate names to checkuser- The user to evaluate the gates for
§Returns
A HashMap mapping gate names to their boolean results
§Errors
Same as check_gate
Sourcepub async fn get_config(
&self,
config_name: impl Into<String>,
user: &User,
) -> Result<Value>
pub async fn get_config( &self, config_name: impl Into<String>, user: &User, ) -> Result<Value>
Get a single dynamic config for a user
Retrieves a dynamic config (or experiment) value for the given user, with caching for improved performance. Statsig uses the same endpoint for both dynamic configs and experiments; the backend determines which based on the name.
§Arguments
config_name- The name of the config to retrieveuser- The user to get the config for
§Returns
The config value as a JSON Value, or null if not found
§Errors
Similar to check_gate, with validation and network errors
Sourcepub async fn get_config_evaluation(
&self,
config_name: impl Into<String>,
user: &User,
) -> Result<ConfigEvaluationResult>
pub async fn get_config_evaluation( &self, config_name: impl Into<String>, user: &User, ) -> Result<ConfigEvaluationResult>
Get a single dynamic config (or experiment) evaluation for a user
Returns the full evaluation payload including rule_id, group_name, and group.
Sourcepub async fn get_configs(
&self,
config_names: Vec<String>,
user: &User,
) -> Result<HashMap<String, Value>>
pub async fn get_configs( &self, config_names: Vec<String>, user: &User, ) -> Result<HashMap<String, Value>>
Get multiple dynamic configs for a user
Efficiently retrieves multiple configuration objects (or experiments) in parallel when cache misses occur.
§Arguments
config_names- List of config names to retrieveuser- The user to get configs for
§Returns
A HashMap mapping config names to their JSON values
§Errors
Similar to check_gate
Sourcepub async fn get_config_evaluations(
&self,
config_names: Vec<String>,
user: &User,
) -> Result<HashMap<String, ConfigEvaluationResult>>
pub async fn get_config_evaluations( &self, config_names: Vec<String>, user: &User, ) -> Result<HashMap<String, ConfigEvaluationResult>>
Get multiple dynamic config (or experiment) evaluations for a user
Returns full evaluation payloads including rule_id, group_name, and group.
Sourcepub fn cache_metrics(&self) -> CacheMetricsSummary
pub fn cache_metrics(&self) -> CacheMetricsSummary
Get cache performance metrics
Returns a snapshot of cache performance metrics including hit ratio, total requests, and other useful statistics for monitoring.
§Returns
A summary of cache metrics
Sourcepub fn reset_cache_metrics(&self)
pub fn reset_cache_metrics(&self)
Reset cache metrics
Resets all cache performance counters to zero. Useful for periodic monitoring or testing scenarios.