Skip to content

Commit 09c44ba

Browse files
dhrgitalxiord
authored andcommitted
rate_limiter: always create timer_fd
RateLimiter::timer_fd is now always created by RateLimiter::new(), since rate limiters can now be added after setting up seccomp. Signed-off-by: Dan Horobeanu <[email protected]>
1 parent 5be5e94 commit 09c44ba

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

rate_limiter/src/lib.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ pub struct RateLimiter {
269269
bandwidth: Option<TokenBucket>,
270270
ops: Option<TokenBucket>,
271271

272-
timer_fd: Option<TimerFd>,
272+
timer_fd: TimerFd,
273273
// Internal flag that quickly determines timer state.
274274
timer_active: bool,
275275
}
@@ -411,12 +411,10 @@ impl RateLimiter {
411411
ops_complete_refill_time_ms,
412412
);
413413

414-
// TODO: Self::timer_fd should not be an `Option` anymore; clean that up.
415-
//
416414
// We'll need a timer_fd, even if our current config effectively disables rate limiting,
417415
// because `Self::update_buckets()` might re-enable it later, and we might be
418416
// seccomp-blocked from creating the timer_fd at that time.
419-
let timer_fd = Some(TimerFd::new_custom(ClockId::Monotonic, true, true)?);
417+
let timer_fd = TimerFd::new_custom(ClockId::Monotonic, true, true)?;
420418

421419
Ok(RateLimiter {
422420
bandwidth: bytes_token_bucket,
@@ -449,8 +447,6 @@ impl RateLimiter {
449447
// Register the timer; don't care about its previous state
450448
// safe to unwrap: timer is definitely Some() since we have a bucket.
451449
self.timer_fd
452-
.as_mut()
453-
.expect("Failed to consume rate limiter token due to invalid timer fd")
454450
.set_state(TIMER_REFILL_STATE, SetTimeFlags::Default);
455451
self.timer_active = true;
456452
}
@@ -489,22 +485,14 @@ impl RateLimiter {
489485
///
490486
/// If the rate limiter is disabled or is not blocked, an error is returned.
491487
pub fn event_handler(&mut self) -> Result<(), Error> {
492-
match self.timer_fd.as_mut() {
493-
Some(timer_fd) => {
494-
// Read the timer_fd and report error if there was no event.
495-
match timer_fd.read() {
496-
0 => Err(Error::SpuriousRateLimiterEvent(
497-
"Rate limiter event handler called without a present timer",
498-
)),
499-
_ => {
500-
self.timer_active = false;
501-
Ok(())
502-
}
503-
}
504-
}
505-
None => Err(Error::SpuriousRateLimiterEvent(
488+
match self.timer_fd.read() {
489+
0 => Err(Error::SpuriousRateLimiterEvent(
506490
"Rate limiter event handler called without a present timer",
507491
)),
492+
_ => {
493+
self.timer_active = false;
494+
Ok(())
495+
}
508496
}
509497
}
510498

@@ -543,10 +531,7 @@ impl AsRawFd for RateLimiter {
543531
/// Will return a negative value if rate limiting is disabled on both
544532
/// token types.
545533
fn as_raw_fd(&self) -> RawFd {
546-
match self.timer_fd.as_ref() {
547-
Some(timer_fd) => timer_fd.as_raw_fd(),
548-
None => -1,
549-
}
534+
self.timer_fd.as_raw_fd()
550535
}
551536
}
552537

0 commit comments

Comments
 (0)