Skip to content

Commit

Permalink
updated to use throttled provider
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKitsune committed Nov 8, 2023
1 parent efcdf1e commit bad679c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ thiserror = "1.0.49"
tokio = { version = "1.32.0", features = ["sync", "macros"] }
toml = "0.8.8"
tracing = "0.1.37"
governor = "0.6.0"
url = "2.4.1"


[dev-dependencies]
reqwest = { version = "0.11.22", features = ["json"] }
Expand Down
29 changes: 22 additions & 7 deletions bin/tree_availability_service.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use std::sync::Arc;
use std::time::Duration;

use clap::Parser;
use common::metrics::init_statsd_exporter;
use common::shutdown_tracer_provider;
use common::tracing::{init_datadog_subscriber, init_subscriber};
use ethers::providers::{Http, Provider};
use ethers::types::H160;
use ethers_throttle::ThrottledProvider;
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use governor::Jitter;
use tracing::Level;
use url::Url;
use world_tree::tree::service::TreeAvailabilityService;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -52,8 +56,14 @@ struct Opts {
port: u16,
#[clap(long, help = "Enable datadog backend for instrumentation")]
datadog: bool,
#[clap(long, help = "Request per minute limit for rpc endpoint")]
throttle: bool,

#[clap(
short,
long,
help = "Request per minute limit for rpc endpoint",
default_value = "0"
)]
throttle: u32,
}

const SERVICE_NAME: &str = "tree-availability-service";
Expand All @@ -71,11 +81,16 @@ pub async fn main() -> eyre::Result<()> {
init_subscriber(Level::INFO);
}

let middleware = if opts.throttle {
Arc::new(Provider::<Http>::try_from(opts.rpc_endpoint)?)
} else {
Arc::new(Provider::<Http>::try_from(opts.rpc_endpoint)?)
};
let http_provider = Http::new(Url::parse(&opts.rpc_endpoint)?);
let throttled_http_provider = ThrottledProvider::new(
http_provider,
opts.throttle,
Some(Jitter::new(
Duration::from_millis(10),
Duration::from_millis(100),
)),
);
let middleware = Arc::new(Provider::new(throttled_http_provider));

let handles = TreeAvailabilityService::new(
opts.tree_depth,
Expand Down
6 changes: 3 additions & 3 deletions crates/ethers-throttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ impl<P: JsonRpcClient> ThrottledProvider<P> {
provider: P,
requests_per_second: u32,
jitter: Option<Jitter>,
) -> Result<Self, ProviderError> {
) -> Self {
let throttle = Arc::new(RateLimiter::direct(Quota::per_second(
NonZeroU32::new(requests_per_second)
.expect("Could not initialize NonZeroU32"),
)));

Ok(ThrottledProvider {
ThrottledProvider {
throttle,
jitter,
inner: provider,
})
}
}
}

Expand Down

0 comments on commit bad679c

Please sign in to comment.