Skip to content

Commit 4137629

Browse files
convert from reqwest blocking to non-blocking to conform to wasm target (#71)
* convert from reqwest blocking to non-blocking to conform to wasm target * bump core to 4.1.1 * handle and update_result * bump dependencies to 4.1.1
1 parent 9d491dc commit 4137629

File tree

7 files changed

+33
-20
lines changed

7 files changed

+33
-20
lines changed

eppo_core/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "eppo_core"
3-
version = "4.1.0"
3+
version = "4.1.1"
44
edition = "2021"
55
description = "Eppo SDK core library"
66
repository = "https://github.com/Eppo-exp/rust-sdk"
@@ -26,11 +26,12 @@ log = { version = "0.4.21", features = ["kv", "kv_serde"] }
2626
md5 = "0.7.0"
2727
rand = "0.8.5"
2828
regex = "1.10.4"
29-
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
29+
reqwest = { version = "0.12.4", features = ["json"] }
3030
semver = { version = "1.0.22", features = ["serde"] }
3131
serde = { version = "1.0.198", features = ["derive", "rc"] }
3232
serde_json = "1.0.116"
3333
thiserror = "1.0.60"
34+
tokio = { version = "1.34.0", features = ["rt", "time"] }
3435
url = "2.5.0"
3536

3637
# pyo3 dependencies

eppo_core/src/configuration_fetcher.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const BANDIT_ENDPOINT: &'static str = "/flag-config/v1/bandits";
2020
/// A client that fetches Eppo configuration from the server.
2121
pub struct ConfigurationFetcher {
2222
// Client holds a connection pool internally, so we're reusing the client between requests.
23-
client: reqwest::blocking::Client,
23+
client: reqwest::Client,
2424
config: ConfigurationFetcherConfig,
2525
/// If we receive a 401 Unauthorized error during a request, it means the API key is not
2626
/// valid. We cache this error so we don't issue additional requests to the server.
@@ -29,7 +29,7 @@ pub struct ConfigurationFetcher {
2929

3030
impl ConfigurationFetcher {
3131
pub fn new(config: ConfigurationFetcherConfig) -> ConfigurationFetcher {
32-
let client = reqwest::blocking::Client::new();
32+
let client = reqwest::Client::new();
3333

3434
ConfigurationFetcher {
3535
client,
@@ -38,24 +38,24 @@ impl ConfigurationFetcher {
3838
}
3939
}
4040

41-
pub fn fetch_configuration(&mut self) -> Result<Configuration> {
41+
pub async fn fetch_configuration(&mut self) -> Result<Configuration> {
4242
if self.unauthorized {
4343
return Err(Error::Unauthorized);
4444
}
4545

46-
let ufc = self.fetch_ufc_configuration()?;
46+
let ufc = self.fetch_ufc_configuration().await?;
4747

4848
let bandits = if ufc.compiled.flag_to_bandit_associations.is_empty() {
4949
// We don't need bandits configuration if there are no bandits.
5050
None
5151
} else {
52-
Some(self.fetch_bandits_configuration()?)
52+
Some(self.fetch_bandits_configuration().await?)
5353
};
5454

5555
Ok(Configuration::from_server_response(ufc, bandits))
5656
}
5757

58-
fn fetch_ufc_configuration(&mut self) -> Result<UniversalFlagConfig> {
58+
async fn fetch_ufc_configuration(&mut self) -> Result<UniversalFlagConfig> {
5959
let url = Url::parse_with_params(
6060
&format!("{}{}", self.config.base_url, UFC_ENDPOINT),
6161
&[
@@ -68,7 +68,7 @@ impl ConfigurationFetcher {
6868
.map_err(|err| Error::InvalidBaseUrl(err))?;
6969

7070
log::debug!(target: "eppo", "fetching UFC flags configuration");
71-
let response = self.client.get(url).send()?;
71+
let response = self.client.get(url).send().await?;
7272

7373
let response = response.error_for_status().map_err(|err| {
7474
if err.status() == Some(StatusCode::UNAUTHORIZED) {
@@ -82,15 +82,17 @@ impl ConfigurationFetcher {
8282
}
8383
})?;
8484

85-
let configuration =
86-
UniversalFlagConfig::from_json(self.config.sdk_metadata, response.bytes()?.into())?;
85+
let configuration = UniversalFlagConfig::from_json(
86+
self.config.sdk_metadata,
87+
response.bytes().await?.into(),
88+
)?;
8789

8890
log::debug!(target: "eppo", "successfully fetched UFC flags configuration");
8991

9092
Ok(configuration)
9193
}
9294

93-
fn fetch_bandits_configuration(&mut self) -> Result<BanditResponse> {
95+
async fn fetch_bandits_configuration(&mut self) -> Result<BanditResponse> {
9496
let url = Url::parse_with_params(
9597
&format!("{}{}", self.config.base_url, BANDIT_ENDPOINT),
9698
&[
@@ -103,7 +105,7 @@ impl ConfigurationFetcher {
103105
.map_err(|err| Error::InvalidBaseUrl(err))?;
104106

105107
log::debug!(target: "eppo", "fetching UFC bandits configuration");
106-
let response = self.client.get(url).send()?;
108+
let response = self.client.get(url).send().await?;
107109

108110
let response = response.error_for_status().map_err(|err| {
109111
if err.status() == Some(StatusCode::UNAUTHORIZED) {
@@ -117,7 +119,7 @@ impl ConfigurationFetcher {
117119
}
118120
})?;
119121

120-
let configuration = response.json()?;
122+
let configuration = response.json().await?;
121123

122124
log::debug!(target: "eppo", "successfully fetched UFC bandits configuration");
123125

eppo_core/src/poller_thread.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,19 @@ impl PollerThread {
131131
std::thread::Builder::new()
132132
.name("eppo-poller".to_owned())
133133
.spawn(move || {
134+
let runtime = match tokio::runtime::Builder::new_current_thread()
135+
.enable_all()
136+
.build()
137+
{
138+
Ok(runtime) => runtime,
139+
Err(err) => {
140+
update_result(Err(Error::from(err)));
141+
return;
142+
}
143+
};
134144
loop {
135145
log::debug!(target: "eppo", "fetching new configuration");
136-
let result = fetcher.fetch_configuration();
146+
let result = runtime.block_on(fetcher.fetch_configuration());
137147
match result {
138148
Ok(configuration) => {
139149
store.set_configuration(Arc::new(configuration));

python-sdk/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[package]
22
name = "eppo_py"
3-
version = "4.1.0"
3+
version = "4.1.1"
44
edition = "2021"
55
publish = false
66

77
[lib]
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
eppo_core = { version = "4.1.0", path = "../eppo_core", features = ["pyo3", "vendored"] }
11+
eppo_core = { version = "4.1.1", path = "../eppo_core", features = ["pyo3", "vendored"] }
1212
log = "0.4.22"
1313
pyo3 = { version = "0.22.0" }
1414
pyo3-log = "0.11.0"

ruby-sdk/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ruby-sdk/ext/eppo_client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crate-type = ["cdylib"]
1212

1313
[dependencies]
1414
env_logger = { version = "0.11.3", features = ["unstable-kv"] }
15-
eppo_core = { version = "4.1.0", features = ["vendored"] }
15+
eppo_core = { version = "4.1.1", features = ["vendored"] }
1616
log = { version = "0.4.21", features = ["kv_serde"] }
1717
magnus = { version = "0.6.4" }
1818
serde = { version = "1.0.203", features = ["derive"] }

rust-sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ categories = ["config"]
1111
rust-version = "1.71.1"
1212

1313
[dependencies]
14-
eppo_core = { version = "=4.1.0", path = "../eppo_core" }
14+
eppo_core = { version = "=4.1.1", path = "../eppo_core" }
1515
log = { version = "0.4.21", features = ["kv", "kv_serde"] }
1616
serde_json = "1.0.116"
1717

0 commit comments

Comments
 (0)