Skip to content

Commit

Permalink
convert from reqwest blocking to non-blocking to conform to wasm target
Browse files Browse the repository at this point in the history
  • Loading branch information
leoromanovsky committed Nov 16, 2024
1 parent 5a0e616 commit 59f178e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.compile.nullAnalysis.mode": "automatic"
}
3 changes: 2 additions & 1 deletion eppo_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ log = { version = "0.4.21", features = ["kv", "kv_serde"] }
md5 = "0.7.0"
rand = "0.8.5"
regex = "1.10.4"
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
reqwest = { version = "0.12.4", features = ["json"] }
semver = { version = "1.0.22", features = ["serde"] }
serde = { version = "1.0.198", features = ["derive", "rc"] }
serde_json = "1.0.116"
thiserror = "1.0.60"
tokio = { version = "1.34.0", features = ["rt", "time"] }
url = "2.5.0"

# pyo3 dependencies
Expand Down
26 changes: 14 additions & 12 deletions eppo_core/src/configuration_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const BANDIT_ENDPOINT: &'static str = "/flag-config/v1/bandits";
/// A client that fetches Eppo configuration from the server.
pub struct ConfigurationFetcher {
// Client holds a connection pool internally, so we're reusing the client between requests.
client: reqwest::blocking::Client,
client: reqwest::Client,
config: ConfigurationFetcherConfig,
/// If we receive a 401 Unauthorized error during a request, it means the API key is not
/// valid. We cache this error so we don't issue additional requests to the server.
Expand All @@ -29,7 +29,7 @@ pub struct ConfigurationFetcher {

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

ConfigurationFetcher {
client,
Expand All @@ -38,24 +38,24 @@ impl ConfigurationFetcher {
}
}

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

let ufc = self.fetch_ufc_configuration()?;
let ufc = self.fetch_ufc_configuration().await?;

let bandits = if ufc.compiled.flag_to_bandit_associations.is_empty() {
// We don't need bandits configuration if there are no bandits.
None
} else {
Some(self.fetch_bandits_configuration()?)
Some(self.fetch_bandits_configuration().await?)
};

Ok(Configuration::from_server_response(ufc, bandits))
}

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

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

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

let configuration =
UniversalFlagConfig::from_json(self.config.sdk_metadata, response.bytes()?.into())?;
let configuration = UniversalFlagConfig::from_json(
self.config.sdk_metadata,
response.bytes().await?.into(),
)?;

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

Ok(configuration)
}

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

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

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

let configuration = response.json()?;
let configuration = response.json().await?;

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

Expand Down
6 changes: 5 additions & 1 deletion eppo_core/src/poller_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ impl PollerThread {
std::thread::Builder::new()
.name("eppo-poller".to_owned())
.spawn(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
loop {
log::debug!(target: "eppo", "fetching new configuration");
let result = fetcher.fetch_configuration();
let result = runtime.block_on(fetcher.fetch_configuration());
match result {
Ok(configuration) => {
store.set_configuration(Arc::new(configuration));
Expand Down
2 changes: 1 addition & 1 deletion sdk-test-data
Submodule sdk-test-data updated 55 files
+7 −0 .gitignore
+8 −0 package-testing/.prettierrc
+16 −0 package-testing/eslint.config.EXAMPLE.mjs
+7 −0 package-testing/php-sdk-relay/.env.EXAMPLE
+2 −0 package-testing/php-sdk-relay/.gitignore
+32 −0 package-testing/php-sdk-relay/Dockerfile
+45 −0 package-testing/php-sdk-relay/README.md
+28 −0 package-testing/php-sdk-relay/build-and-run.sh
+35 −0 package-testing/php-sdk-relay/composer.json
+2,813 −0 package-testing/php-sdk-relay/composer.lock
+18 −0 package-testing/php-sdk-relay/docker-run.sh
+5 −0 package-testing/php-sdk-relay/release.sh
+66 −0 package-testing/php-sdk-relay/src/AssignmentHandler.php
+83 −0 package-testing/php-sdk-relay/src/BanditHandler.php
+17 −0 package-testing/php-sdk-relay/src/Config.php
+36 −0 package-testing/php-sdk-relay/src/RelayLogger.php
+18 −0 package-testing/php-sdk-relay/src/eppo_poller.php
+59 −0 package-testing/php-sdk-relay/src/index.php
+14 −0 package-testing/scenarios.json
+3 −0 package-testing/sdk-test-runner/.env.EXAMPLE
+5 −0 package-testing/sdk-test-runner/.gitignore
+16 −0 package-testing/sdk-test-runner/Dockerfile
+214 −0 package-testing/sdk-test-runner/README.md
+16 −0 package-testing/sdk-test-runner/eslint.config.mjs
+39 −0 package-testing/sdk-test-runner/package.json
+8 −0 package-testing/sdk-test-runner/release.sh
+231 −0 package-testing/sdk-test-runner/src/app.ts
+38 −0 package-testing/sdk-test-runner/src/config.ts
+7 −0 package-testing/sdk-test-runner/src/dto/assignmentRequest.ts
+22 −0 package-testing/sdk-test-runner/src/dto/banditActionRequest.ts
+9 −0 package-testing/sdk-test-runner/src/dto/scenario.ts
+6 −0 package-testing/sdk-test-runner/src/dto/testResponse.ts
+14 −0 package-testing/sdk-test-runner/src/logging.ts
+23 −0 package-testing/sdk-test-runner/src/program.ts
+1 −0 package-testing/sdk-test-runner/src/util.ts
+170 −0 package-testing/sdk-test-runner/test-sdk.sh
+12 −0 package-testing/sdk-test-runner/tsconfig.json
+1,394 −0 package-testing/sdk-test-runner/yarn.lock
+3 −0 package-testing/testing-api/.dockerignore
+4 −0 package-testing/testing-api/.env.EXAMPLE
+1 −0 package-testing/testing-api/.gitignore
+17 −0 package-testing/testing-api/Dockerfile
+126 −0 package-testing/testing-api/README.md
+13 −0 package-testing/testing-api/clone-test-data.sh
+5 −0 package-testing/testing-api/copy-test-data.sh
+13 −0 package-testing/testing-api/eslint.config.mjs
+34 −0 package-testing/testing-api/package.json
+9 −0 package-testing/testing-api/release.sh
+53 −0 package-testing/testing-api/src/app.ts
+22 −0 package-testing/testing-api/src/config.ts
+72 −0 package-testing/testing-api/src/routes.ts
+11 −0 package-testing/testing-api/src/server.ts
+39 −0 package-testing/testing-api/src/ufc/data.ts
+12 −0 package-testing/testing-api/tsconfig.json
+1,780 −0 package-testing/testing-api/yarn.lock

0 comments on commit 59f178e

Please sign in to comment.