Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework async library in CAC client #88

Merged
merged 2 commits into from
Jun 24, 2024
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test_logs
.keep
.vscode
*.session.sql

.cargo
# pre-commit config
.pre-commit-config.yaml
.cargo
33 changes: 22 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ members = [
"crates/service_utils",
"crates/experimentation_client",
"crates/cac_client",
"crates/experimentation_client_integration_example",
"crates/frontend",
"crates/caclang",
"crates/superposition",
"crates/superposition_types",
"examples/experimentation_client_integration_example",
"examples/cac_client_integration_example"
]

[[workspace.metadata.leptos]]
Expand Down
128 changes: 67 additions & 61 deletions crates/cac_client/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ use std::{
};

use crate::{utils::core::MapError, Client, MergeStrategy, CLIENT_FACTORY};
use once_cell::sync::Lazy;
use serde_json::{Map, Value};
use std::{
cell::RefCell,
ffi::{c_int, CString},
time::Duration,
};
use tokio::{runtime::Runtime, task};
use tokio::runtime::Runtime;

thread_local! {
static LAST_ERROR: RefCell<Option<String>> = const { RefCell::new(None) };
}

static CAC_RUNTIME: Lazy<Runtime> =
Lazy::new(|| Runtime::new().expect("The runtime was not intialized"));

macro_rules! null_check {
($client: ident, $err: literal, $return: stmt) => {
if $client.is_null() {
Expand Down Expand Up @@ -104,8 +108,7 @@ pub extern "C" fn cac_new_client(
let hostname = unwrap_safe!(cstring_to_rstring(hostname), return 1);

// println!("Creating cac client thread for tenant {tenant}");
let local = task::LocalSet::new();
local.block_on(&Runtime::new().unwrap(), async move {
CAC_RUNTIME.block_on(async move {
match CLIENT_FACTORY
.create_client(tenant.clone(), duration, hostname)
.await
Expand All @@ -125,12 +128,8 @@ pub extern "C" fn cac_start_polling_update(tenant: *const c_char) {
unsafe {
let client = cac_get_client(tenant);
null_check!(client, "CAC client for tenant not found", return);
let local = task::LocalSet::new();
// println!("in FFI polling");
local.block_on(
&Runtime::new().unwrap(),
(*client).clone().run_polling_updates(),
);
let _handle = CAC_RUNTIME.spawn((*client).clone().run_polling_updates());
}
}

Expand All @@ -148,12 +147,15 @@ pub extern "C" fn cac_free_client(ptr: *mut Arc<Client>) {
pub extern "C" fn cac_get_client(tenant: *const c_char) -> *mut Arc<Client> {
let ten = unwrap_safe!(cstring_to_rstring(tenant), return std::ptr::null_mut());
// println!("fetching cac client thread for tenant {ten}");
unwrap_safe!(
CLIENT_FACTORY
.get_client(ten)
.map(|client| Box::into_raw(Box::new(client))),
std::ptr::null_mut()
)
CAC_RUNTIME.block_on(async move {
unwrap_safe!(
CLIENT_FACTORY
.get_client(ten)
.await
.map(|client| Box::into_raw(Box::new(client))),
std::ptr::null_mut()
)
})
}

#[no_mangle]
Expand All @@ -163,14 +165,12 @@ pub extern "C" fn cac_get_last_modified(client: *mut Arc<Client>) -> *const c_ch
"an invalid null pointer client is being used, please call get_client()",
return std::ptr::null()
);
unwrap_safe!(
CAC_RUNTIME.block_on(async move {
unsafe {
(*client)
.get_last_modified()
.map(|date| rstring_to_cstring(date.to_string()).into_raw())
},
std::ptr::null()
)
let datetime = (*client).get_last_modified().await;
rstring_to_cstring(datetime.to_string()).into_raw()
}
})
}

#[no_mangle]
Expand Down Expand Up @@ -208,18 +208,22 @@ pub extern "C" fn cac_get_config(

Some(prefix_list).filter(|list| !list.is_empty())
};

unwrap_safe!(
CAC_RUNTIME.block_on(async move {
unsafe {
(*client)
.get_full_config_state_with_filter(filters, prefix_list)
.map(|config| {
rstring_to_cstring(serde_json::to_value(config).unwrap().to_string())
unwrap_safe!(
(*client)
.get_full_config_state_with_filter(filters, prefix_list)
.await
.map(|config| {
rstring_to_cstring(
serde_json::to_value(config).unwrap().to_string(),
)
.into_raw()
})
},
std::ptr::null_mut()
)
}),
std::ptr::null_mut()
)
}
})
}

#[no_mangle]
Expand All @@ -238,13 +242,8 @@ pub extern "C" fn cac_get_resolved_config(
let keys: Option<Vec<String>> = if filter_keys.is_null() {
None
} else {
let filter_string = match cstring_to_rstring(filter_keys) {
Ok(s) => s,
Err(err) => {
update_last_error(err);
return std::ptr::null();
}
};
let filter_string =
unwrap_safe!(cstring_to_rstring(filter_keys), return std::ptr::null());
Some(filter_string.split('|').map(str::to_string).collect())
};

Expand All @@ -260,21 +259,27 @@ pub extern "C" fn cac_get_resolved_config(
serde_json::from_str::<Map<String, Value>>(query.as_str()),
return std::ptr::null()
);

unwrap_safe!(
CAC_RUNTIME.block_on(async move {
unsafe {
(*client)
.get_resolved_config(context, keys, MergeStrategy::from(merge_strategem))
.map(|ov| {
unwrap_safe!(
unwrap_safe!(
(*client)
.get_resolved_config(
context,
keys,
MergeStrategy::from(merge_strategem),
)
.await
.map(|ov| {
unwrap_safe!(
serde_json::to_string::<Map<String, Value>>(&ov)
.map(|overrides| rstring_to_cstring(overrides).into_raw()),
std::ptr::null()
)
})
},
std::ptr::null()
)
}),
std::ptr::null()
)
}
})
}

#[no_mangle]
Expand All @@ -294,17 +299,18 @@ pub extern "C" fn cac_get_default_config(
};
Some(filter_string.split('|').map(str::to_string).collect())
};

unwrap_safe!(
unsafe {
(*client).get_default_config(keys).map(|ov| {
unwrap_safe!(
serde_json::to_string::<Map<String, Value>>(&ov)
.map(|overrides| rstring_to_cstring(overrides).into_raw()),
std::ptr::null()
)
})
},
std::ptr::null()
)
CAC_RUNTIME.block_on(async move {
unwrap_safe!(
unsafe {
(*client).get_default_config(keys).await.map(|ov| {
unwrap_safe!(
serde_json::to_string::<Map<String, Value>>(&ov)
.map(|overrides| rstring_to_cstring(overrides).into_raw()),
return std::ptr::null()
)
})
},
return std::ptr::null()
)
})
}
Loading
Loading