Apply suggestions from code review #1002

Co-authored-by: Waffle Maybe <waffle.lapkin@gmail.com>
This commit is contained in:
Сырцев Вадим Игоревич 2024-02-01 10:09:58 +03:00 committed by GitHub
parent 927e0ad044
commit 58f8a35825
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 6 additions and 6 deletions

View file

@ -51,7 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Greatly improved the speed of graceful shutdown (`^C`) ([PR 938](https://github.com/teloxide/teloxide/pull/938))
- Fix typos in docstrings ([PR 953](https://github.com/teloxide/teloxide/pull/953))
- Use `Seconds` instead of `String` in `InlineQueryResultAudio` for `audio_duration` ([PR 994](https://github.com/teloxide/teloxide/pull/994))
- Issue [#780](https://github.com/teloxide/teloxide/issues/780) ([PR 1002](https://github.com/teloxide/teloxide/pull/1002))
- High CPU usage on network errors ([PR 1002](https://github.com/teloxide/teloxide/pull/1002), [Issue 780](https://github.com/teloxide/teloxide/issues/780))
### Changed

View file

@ -1,10 +1,10 @@
use std::time::Duration;
pub type BackoffStrategy = Box<dyn Fn(u32) -> Duration + Send>;
pub type BackoffStrategy = Box<dyn Send + Fn(u32) -> Duration>;
/// Calculates the backoff time in seconds for exponential strategy with base 2
///
/// More at: <https://en.wikipedia.org/wiki/Exponential_backoff#Exponential_backoff_algorithm>
pub fn exponential_backoff_strategy(error_count: u32) -> Duration {
Duration::from_secs(2_u64.pow(error_count))
Duration::from_secs((1_u64 << error_count).min(30 * 60))
}

View file

@ -88,9 +88,9 @@ where
}
/// The backoff strategy that will be used for delay calculation between
/// reconnections
/// reconnections caused by network errors.
///
/// By default, the [`exponential_backoff_strategy`] is used
/// By default, the [`exponential_backoff_strategy`] is used.
pub fn backoff_strategy(self, backoff_strategy: BackoffStrategy) -> Self {
Self { backoff_strategy, ..self }
}
@ -462,7 +462,7 @@ impl<B: Requester> Stream for PollingStream<'_, B> {
}
}
// Poll eepy future until completion, needed for backoff strategy
if let Some(eepy) = this.eepy.as_mut().as_pin_mut() {
else if let Some(eepy) = this.eepy.as_mut().as_pin_mut() {
match eepy.poll(cx) {
Poll::Ready(_) => {
// As soon as delay is waited we increment the counter