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:
parent
c1c2d26ec7
commit
f0dcc91907
@ -1434,7 +1434,7 @@ pub fn using_public_server() -> bool {
|
|||||||
|
|
||||||
pub struct ThrottledInterval {
|
pub struct ThrottledInterval {
|
||||||
interval: Interval,
|
interval: Interval,
|
||||||
last_tick: Instant,
|
next_tick: Instant,
|
||||||
min_interval: Duration,
|
min_interval: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1443,7 +1443,7 @@ impl ThrottledInterval {
|
|||||||
let period = i.period();
|
let period = i.period();
|
||||||
ThrottledInterval {
|
ThrottledInterval {
|
||||||
interval: i,
|
interval: i,
|
||||||
last_tick: Instant::now() - period * 2,
|
next_tick: Instant::now(),
|
||||||
min_interval: Duration::from_secs_f64(period.as_secs_f64() * 0.9),
|
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> {
|
pub fn poll_tick(&mut self, cx: &mut std::task::Context<'_>) -> Poll<Instant> {
|
||||||
match self.interval.poll_tick(cx) {
|
match self.interval.poll_tick(cx) {
|
||||||
Poll::Ready(instant) => {
|
Poll::Ready(instant) => {
|
||||||
if self.last_tick.elapsed() >= self.min_interval {
|
let now = Instant::now();
|
||||||
self.last_tick = Instant::now();
|
if self.next_tick <= now {
|
||||||
|
self.next_tick = now + self.min_interval;
|
||||||
Poll::Ready(instant)
|
Poll::Ready(instant)
|
||||||
} else {
|
} else {
|
||||||
// This call is required since tokio 1.27
|
// This call is required since tokio 1.27
|
||||||
|
Loading…
x
Reference in New Issue
Block a user