Skip to content

Commit

Permalink
Revert "fix: update ratelimit crate (iopsystems#20)"
Browse files Browse the repository at this point in the history
This reverts commit c408818.
  • Loading branch information
brayniac committed Jul 30, 2023
1 parent 4d4dff7 commit fe626c4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 48 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protocol-ping = { git = "https://github.com/pelikan-io/pelikan" }
rand = "0.8.5"
rand_distr = "0.4.3"
rand_xoshiro = "0.6.0"
ratelimit = "0.7.0"
ratelimit = "0.5.1"
redis = { version = "0.22.3", features = ["tokio-comp"] }
ringlog = "0.2.0"
serde = "1.0.144"
Expand Down
12 changes: 1 addition & 11 deletions src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,7 @@ mod handlers {
ratelimit: Option<Arc<Ratelimiter>>,
) -> Result<impl warp::Reply, Infallible> {
if let Some(r) = ratelimit {
let amount = (rate as f64 / 1_000_000.0).ceil() as u64;
let interval = Duration::from_micros(1_000_000 / (rate / amount));
let capacity = std::cmp::max(100, amount);

r.set_max_tokens(capacity)
.expect("failed to set max tokens");
r.set_refill_interval(interval)
.expect("failed to set refill interval");
r.set_refill_amount(amount)
.expect("failed to set refill amount");

r.set_rate(rate);
Ok(StatusCode::OK)
} else {
Ok(StatusCode::NOT_FOUND)
Expand Down
43 changes: 11 additions & 32 deletions src/workload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,10 @@ pub struct Generator {

impl Generator {
pub fn new(config: &Config) -> Self {
let ratelimiter = config.workload().ratelimit().map(|rate| {
let amount = (rate.get() as f64 / 1_000_000.0).ceil() as u64;
let interval = Duration::from_micros(1_000_000 / (rate.get() / amount));
let capacity = std::cmp::max(100, amount);

Arc::new(
Ratelimiter::builder(amount, interval)
.max_tokens(capacity)
.build()
.expect("failed to initialize ratelimiter"),
)
});
let ratelimiter = config
.workload()
.ratelimit()
.map(|r| Arc::new(Ratelimiter::new(1000, 1, r.into())));

let mut components = Vec::new();
let mut component_weights = Vec::new();
Expand Down Expand Up @@ -642,18 +634,11 @@ pub async fn reconnect(work_sender: Sender<ClientWorkItem>, config: Config) -> R
return Ok(());
}

let ratelimiter = config.client().unwrap().reconnect_rate().map(|rate| {
let amount = (rate.get() as f64 / 1_000_000.0).ceil() as u64;
let interval = Duration::from_micros(1_000_000 / (rate.get() / amount));
let capacity = std::cmp::max(100, amount);

Arc::new(
Ratelimiter::builder(amount, interval)
.max_tokens(capacity)
.build()
.expect("failed to initialize ratelimiter"),
)
});
let ratelimiter = config
.client()
.unwrap()
.reconnect_rate()
.map(|r| Arc::new(Ratelimiter::new(1000, 1, r.into())));

if ratelimiter.is_none() {
return Ok(());
Expand All @@ -662,14 +647,8 @@ pub async fn reconnect(work_sender: Sender<ClientWorkItem>, config: Config) -> R
let ratelimiter = ratelimiter.unwrap();

while RUNNING.load(Ordering::Relaxed) {
match ratelimiter.try_wait() {
Ok(_) => {
let _ = work_sender.send(ClientWorkItem::Reconnect).await;
}
Err(d) => {
std::thread::sleep(d);
}
}
ratelimiter.wait();
let _ = work_sender.send(ClientWorkItem::Reconnect).await;
}

Ok(())
Expand Down

0 comments on commit fe626c4

Please sign in to comment.