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

implement exponential backoff #34

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ pg16 = ["pgrx/pg16", "pgrx-tests/pg16", "supabase-wrappers/pg16"]
pg_test = []

[dependencies]
backoff = { version = "0.4.0", features = ["tokio"] }
chrono = "0.4.26"
clerk-rs = "0.3.0"
pgrx = "=0.11.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
supabase-wrappers = { git = "https://github.com/supabase/wrappers.git", default-features = false}
supabase-wrappers = { git = "https://github.com/supabase/wrappers.git", default-features = false }
tokio = { version = "1", features = ["full"] }
reqwest = "0.11"

[dev-dependencies]
pgrx-tests = "=0.11.3"
pgrx-tests = "=0.11.3"
2 changes: 1 addition & 1 deletion Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Postgres Foreign Data Wrapper for Clerk.com Backend API"
homepage = "https://github.com/tembo-io/clerk_fdw"
documentation = "https://github.com/tembo-io/clerk_fdw"
categories = ["connectors"]
version = "0.3.0"
version = "0.3.1"

[build]
postgres_version = "15"
Expand Down
44 changes: 31 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use clerk_rs::{
apis::organizations_api::Organization, apis::users_api::User, clerk::Clerk, ClerkConfiguration,
};

// TODO: will have to incorporate offset at some point
use backoff::future::retry;
use backoff::ExponentialBackoff;

const PAGE_SIZE: usize = 500;

fn body_to_rows(
Expand Down Expand Up @@ -236,15 +238,34 @@ impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
info!("clerk_fdw: received total organizations: {}", total_orgs);
let mut i_org = 0;
for org in org_res.data.iter() {
let membership_resp =
OrganizationMembership::list_organization_memberships(
&self.clerk_client,
&org.id,
Some(PAGE_SIZE as f32),
None,
)
.await;

let membership_resp = retry(ExponentialBackoff::default(), || {
async {
OrganizationMembership::list_organization_memberships(
&self.clerk_client,
&org.id,
Some(PAGE_SIZE as f32),
None,
)
.await
.map_err(|e| match e {
clerk_rs::apis::Error::Reqwest(ref reqwest_error) => {
if let Some(status_code) = reqwest_error.status() {
if status_code == reqwest::StatusCode::TOO_MANY_REQUESTS {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 5xx should also be retried. I have seen 502/503 from clerk as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can add that later though.

info!("clerk_fdw: received too many requests error, backing off");
backoff::Error::transient(e) // Mark 429 errors as transient
} else {
backoff::Error::Permanent(e)
}
} else {
backoff::Error::Permanent(e)
}
}
_ => backoff::Error::Permanent(e),
})
}
})
.await;

match membership_resp {
Ok(mem_res) => {
i_org += 1;
Expand All @@ -264,8 +285,6 @@ impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
continue;
}
}
// Introduce a delay of 0.05 seconds
std::thread::sleep(std::time::Duration::from_millis(50));
}
if org_res.data.len() < PAGE_SIZE {
info!("clerk_fdw: finished fetching all memberships, total={}", result.len());
Expand All @@ -279,7 +298,6 @@ impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
}
}
} else {
// this is where i need to make changes
let mut offset = 0;
loop {
let obj_js =
Expand Down
Loading