Skip to content

Commit

Permalink
fix: fixed duplicate snowflake id gen and busy wait on snowflake_gene…
Browse files Browse the repository at this point in the history
…rator mutex
  • Loading branch information
ShubhranshuSanjeev authored and pratikmishra356 committed Apr 26, 2024
1 parent 7724808 commit 71dd5a9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions crates/context_aware_config/src/api/context/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ use service_utils::{bad_argument, result as superposition};
pub fn endpoints() -> Scope {
Scope::new("")
.service(put_handler)
.service(move_handler)
.service(delete_context)
.service(bulk_operations)
.service(list_contexts)
.service(move_handler)
.service(delete_context)
.service(get_context)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ async fn create(
// generating snowflake id for experiment
let mut snowflake_generator = state.snowflake_generator.lock().unwrap();
let experiment_id = snowflake_generator.real_time_generate();
// explicitly dropping snowflake_generator so that lock is released and it can be acquired in bulk-operations handler
drop(snowflake_generator);

//create overrides in CAC, if successfull then create experiment in DB
let mut cac_operations: Vec<ContextAction> = vec![];
Expand Down
3 changes: 2 additions & 1 deletion crates/service_utils/src/service/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
collections::HashSet,
future::{ready, Ready},
str::FromStr,
sync::Arc,
};

use actix_web::{error, web::Data, Error, FromRequest, HttpMessage};
Expand Down Expand Up @@ -37,7 +38,7 @@ pub struct AppState {
pub default_config_validation_schema: JSONSchema,
pub meta_schema: JSONSchema,
pub experimentation_flags: ExperimentationFlags,
pub snowflake_generator: Mutex<SnowflakeIdGenerator>,
pub snowflake_generator: Arc<Mutex<SnowflakeIdGenerator>>,
pub enable_tenant_and_scope: bool,
pub tenant_middleware_exclusion_list: HashSet<String>,
pub service_prefix: String,
Expand Down
5 changes: 4 additions & 1 deletion crates/superposition/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use context_aware_config::helpers::{
};
use dotenv;
use experimentation_platform::api::*;
use std::sync::Arc;
use std::{collections::HashSet, io::Result};
use superposition_types::User;

Expand Down Expand Up @@ -134,6 +135,8 @@ async fn main() -> Result<()> {
return view! { <App app_envs=routes_ui_envs.clone()/> };
});

let snowflake_generator = Arc::new(Mutex::new(SnowflakeIdGenerator::new(1, 1)));

HttpServer::new(move || {
let leptos_options = &conf.leptos_options;
let site_root = &leptos_options.site_root;
Expand Down Expand Up @@ -161,7 +164,7 @@ async fn main() -> Result<()> {
allow_same_keys_non_overlapping_ctx.to_owned(),
},

snowflake_generator: Mutex::new(SnowflakeIdGenerator::new(1,1)),
snowflake_generator: snowflake_generator.clone(),
meta_schema: get_meta_schema(),
app_env: app_env.to_owned(),
enable_tenant_and_scope: enable_tenant_and_scope.to_owned(),
Expand Down

0 comments on commit 71dd5a9

Please sign in to comment.