Skip to content

Commit 0e1c71c

Browse files
Extract persistent keep-alive timer
1 parent 7facc4e commit 0e1c71c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

boringtun/src/noise/timers.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ impl Timers {
130130
Some(last_data_received_without_reply + KEEPALIVE_TIMEOUT)
131131
}
132132

133+
pub(crate) fn next_persistent_keepalive(&self) -> Option<Instant> {
134+
let keepalive = Duration::from_secs(self.persistent_keepalive as u64);
135+
136+
if keepalive.is_zero() {
137+
return None;
138+
}
139+
140+
Some(self[TimerName::TimePersistentKeepalive] + keepalive)
141+
}
142+
133143
fn is_initiator(&self) -> bool {
134144
self.is_initiator
135145
}
@@ -378,9 +388,10 @@ impl Tunn {
378388
}
379389

380390
// Persistent KEEPALIVE
381-
if persistent_keepalive > 0
382-
&& (now - self.timers[TimePersistentKeepalive]
383-
>= Duration::from_secs(persistent_keepalive as _))
391+
if self
392+
.timers
393+
.next_persistent_keepalive()
394+
.is_some_and(|deadline| now >= deadline)
384395
{
385396
tracing::debug!("KEEPALIVE(PERSISTENT_KEEPALIVE)");
386397
self.timer_tick(TimePersistentKeepalive, now);
@@ -400,7 +411,6 @@ impl Tunn {
400411
existing.is_none(),
401412
"Should never override existing handshake"
402413
);
403-
404414
tracing::debug!(?jitter, "Scheduling new handshake");
405415

406416
return TunnResult::Done;

0 commit comments

Comments
 (0)