fix: wrong use of Instant sub, just after booting (#8470)

* fix: wrong use of Instant sub, just after booting

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix: ThrottledInterval, first next tick

Signed-off-by: fufesou <linlong1266@gmail.com>

---------

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou 2024-06-25 20:33:38 +08:00 committed by GitHub
parent c1c2d26ec7
commit f0dcc91907
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1434,7 +1434,7 @@ pub fn using_public_server() -> bool {
pub struct ThrottledInterval {
interval: Interval,
last_tick: Instant,
next_tick: Instant,
min_interval: Duration,
}
@ -1443,7 +1443,7 @@ impl ThrottledInterval {
let period = i.period();
ThrottledInterval {
interval: i,
last_tick: Instant::now() - period * 2,
next_tick: Instant::now(),
min_interval: Duration::from_secs_f64(period.as_secs_f64() * 0.9),
}
}
@ -1456,8 +1456,9 @@ impl ThrottledInterval {
pub fn poll_tick(&mut self, cx: &mut std::task::Context<'_>) -> Poll<Instant> {
match self.interval.poll_tick(cx) {
Poll::Ready(instant) => {
if self.last_tick.elapsed() >= self.min_interval {
self.last_tick = Instant::now();
let now = Instant::now();
if self.next_tick <= now {
self.next_tick = now + self.min_interval;
Poll::Ready(instant)
} else {
// This call is required since tokio 1.27