Skip to content

Commit

Permalink
Refactorings of internal API (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence authored Jul 2, 2022
1 parent 596cf5a commit 123fe54
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
15 changes: 3 additions & 12 deletions src/hyper/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,22 +249,13 @@ impl<H: 'static + Send + Sync + Clone + connect::Connect> SlackClientHyperConnec
{
match (self.tokio_rate_controller.as_ref(), rate_control_params) {
(Some(rate_controller), maybe_method_rate_params) => {
if let Some(duration) = rate_controller
.calc_throttle_delay(
rate_controller
.throttle_delay(
maybe_method_rate_params,
token.and_then(|t| t.team_id.clone()),
delayed,
)
.await
{
if !duration.is_zero() {
debug!("Slack throttler postponed request for {:?}", duration);
let mut interval = tokio::time::interval(duration);

interval.tick().await;
interval.tick().await;
}
}
.await;

self.retry_request_if_needed(
rate_controller.clone(),
Expand Down
23 changes: 22 additions & 1 deletion src/hyper/src/ratectl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use slack_morphism::prelude::*;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tracing::*;

#[derive(Clone, Debug)]
pub struct SlackTokioRateController {
Expand All @@ -17,7 +18,7 @@ impl SlackTokioRateController {
}
}

pub async fn calc_throttle_delay(
async fn calc_throttle_delay(
&self,
method_rate_ctl: Option<&SlackApiMethodRateControlConfig>,
team_id: Option<SlackTeamId>,
Expand All @@ -41,4 +42,24 @@ impl SlackTokioRateController {
delayed
}
}

pub async fn throttle_delay(
&self,
method_rate_ctl: Option<&SlackApiMethodRateControlConfig>,
team_id: Option<SlackTeamId>,
delayed: Option<Duration>,
) {
if let Some(duration) = self
.calc_throttle_delay(method_rate_ctl, team_id, delayed)
.await
{
if !duration.is_zero() {
debug!("Slack throttler postponed request for {:?}", duration);
let mut interval = tokio::time::interval(duration);

interval.tick().await;
interval.tick().await;
}
}
}
}

0 comments on commit 123fe54

Please # to comment.