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

Report accurate value for max_concurrent_requests #201

Merged
merged 6 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ polars = { workspace = true }
rand = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thousands = { workspace = true }
tokio = { workspace = true }
65 changes: 64 additions & 1 deletion crates/cli/src/parse/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) async fn parse_source(args: &Args) -> Result<Source, ParseError> {
rpc_url,
provider,
labels: SourceLabels {
max_concurrent_requests: args.requests_per_second.map(|x| x as u64),
max_concurrent_requests: args.max_concurrent_requests,
max_requests_per_second: args.requests_per_second.map(|x| x as u64),
max_retries: Some(args.max_retries),
initial_backoff: Some(args.initial_backoff),
Expand Down Expand Up @@ -113,3 +113,66 @@ pub(crate) fn parse_rpc_url(args: &Args) -> Result<String, ParseError> {
Ok(url)
}
}

#[cfg(test)]
mod tests {
use super::*;
use thousands::Separable;

async fn setup_source(max_concurrent_requests: u64) -> Source {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is taken from parse::timestamps except I set the max_concurrent_requests to the input.

let rpc_url = match crate::parse::source::parse_rpc_url(&Args::default()) {
Ok(url) => url,
Err(_) => std::process::exit(0),
};
let max_retry = 5;
let initial_backoff = 500;
let max_concurrent_requests = max_concurrent_requests;
let provider =
Provider::<RetryClient<Http>>::new_client(&rpc_url, max_retry, initial_backoff)
.map_err(|_e| ParseError::ParseError("could not connect to provider".to_string()))
.unwrap();

let quota = Quota::per_second(NonZeroU32::new(15).unwrap())
.allow_burst(NonZeroU32::new(1).unwrap());
let rate_limiter = Some(RateLimiter::direct(quota));
let semaphore = tokio::sync::Semaphore::new(max_concurrent_requests as usize);

Source {
provider: provider.into(),
semaphore: Arc::new(Some(semaphore)),
rate_limiter: Arc::new(rate_limiter),
chain_id: 1,
inner_request_size: 1,
max_concurrent_chunks: None,
rpc_url: "".to_string(),
labels: SourceLabels {
max_concurrent_requests: Some(max_concurrent_requests),
..SourceLabels::default()
},
}
}

// modified freeze::types::summaries::print_bullet_indent(), returned as a string and removed formatting dependencies.
fn return_bullet_indent_no_formatting<A: AsRef<str>, B: AsRef<str>>(key: A, value: B, indent: usize) -> String {
let bullet_str = "- ";
let key_str = key.as_ref();
let value_str = value.as_ref();
let colon_str = ": ";
format!("{}{}{}{}{}", " ".repeat(indent), bullet_str, key_str, colon_str, value_str)
}

#[tokio::test]
async fn test_max_concurrent_requests_printing_some() {
let source: Source = setup_source(1337).await;

let output = return_bullet_indent_no_formatting(
"max concurrent requests",
source.labels.max_concurrent_requests.unwrap().separate_with_commas(),
4,
);

let expected = format!(" - max concurrent requests: 1,337");

assert_eq!(output, expected);
}
}