pub struct RateLimiter { /* private fields */ }Expand description
Rate Limiter that works on both bandwidth and ops/s limiting.
Bandwidth (bytes/s) and ops/s limiting can be used at the same time or individually.
Implementation uses a single timer through TimerFd to refresh either or both token buckets.
Its internal buckets are ‘passively’ replenished as they’re being used (as
part of consume() operations).
A timer is enabled and used to ‘actively’ replenish the token buckets when
limiting is in effect and consume() operations are disabled.
RateLimiters will generate events on the FDs provided by their AsRawFd trait
implementation. These events are meant to be consumed by the user of this struct.
On each such event, the user must call the event_handler() method.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(
bytes_total_capacity: u64,
bytes_one_time_burst: u64,
bytes_complete_refill_time_ms: u64,
ops_total_capacity: u64,
ops_one_time_burst: u64,
ops_complete_refill_time_ms: u64,
) -> Result<Self>
pub fn new( bytes_total_capacity: u64, bytes_one_time_burst: u64, bytes_complete_refill_time_ms: u64, ops_total_capacity: u64, ops_one_time_burst: u64, ops_complete_refill_time_ms: u64, ) -> Result<Self>
Creates a new Rate Limiter that can limit on both bytes/s and ops/s.
§Arguments
bytes_total_capacity- the total capacity of theTokenType::Bytestoken bucket.bytes_one_time_burst- initial extra credit on top ofbytes_total_capacity, that does not replenish and which can be used for an initial burst of data.bytes_complete_refill_time_ms- number of milliseconds for theTokenType::Bytestoken bucket to go from zero Bytes tobytes_total_capacityBytes.ops_total_capacity- the total capacity of theTokenType::Opstoken bucket.ops_one_time_burst- initial extra credit on top ofops_total_capacity, that does not replenish and which can be used for an initial burst of data.ops_complete_refill_time_ms- number of milliseconds for theTokenType::Opstoken bucket to go from zero Ops toops_total_capacityOps.
If either bytes/ops size or refill_time are zero, the limiter is disabled for that respective token type.
§Errors
If the timerfd creation fails, an error is returned.
Sourcepub fn consume(&mut self, tokens: u64, token_type: TokenType) -> bool
pub fn consume(&mut self, tokens: u64, token_type: TokenType) -> bool
Attempts to consume tokens and returns whether that is possible.
If rate limiting is disabled on provided token_type, this function will always succeed.
Sourcepub fn manual_replenish(&mut self, tokens: u64, token_type: TokenType)
pub fn manual_replenish(&mut self, tokens: u64, token_type: TokenType)
Adds tokens of token_type to their respective bucket.
Can be used to manually add tokens to a bucket. Useful for reverting a
consume() if needed.
Sourcepub fn is_blocked(&self) -> bool
pub fn is_blocked(&self) -> bool
Returns whether this rate limiter is blocked.
The limiter ‘blocks’ when a consume() operation fails because there was not enough
budget for it.
An event will be generated on the exported FD when the limiter ‘unblocks’.
Sourcepub fn event_handler(&mut self) -> Result<(), RateLimiterError>
pub fn event_handler(&mut self) -> Result<(), RateLimiterError>
This function needs to be called every time there is an event on the
FD provided by this object’s AsRawFd trait implementation.
§Errors
If the rate limiter is disabled or is not blocked, an error is returned.
Sourcepub fn update_buckets(&mut self, bytes: BucketUpdate, ops: BucketUpdate)
pub fn update_buckets(&mut self, bytes: BucketUpdate, ops: BucketUpdate)
Updates the parameters of the token buckets associated with this RateLimiter.
Sourcepub fn bandwidth(&self) -> Option<&TokenBucket>
pub fn bandwidth(&self) -> Option<&TokenBucket>
Returns an immutable view of the inner bandwidth token bucket.
Sourcepub fn ops(&self) -> Option<&TokenBucket>
pub fn ops(&self) -> Option<&TokenBucket>
Returns an immutable view of the inner ops token bucket.
Trait Implementations§
Source§impl AsRawFd for RateLimiter
impl AsRawFd for RateLimiter
Source§impl Debug for RateLimiter
impl Debug for RateLimiter
Source§impl Default for RateLimiter
impl Default for RateLimiter
Source§impl From<&RateLimiter> for RateLimiterConfig
impl From<&RateLimiter> for RateLimiterConfig
Source§fn from(rl: &RateLimiter) -> Self
fn from(rl: &RateLimiter) -> Self
Source§impl PartialEq for RateLimiter
impl PartialEq for RateLimiter
Source§impl Persist<'_> for RateLimiter
impl Persist<'_> for RateLimiter
Source§type State = RateLimiterState
type State = RateLimiterState
Source§type ConstructorArgs = ()
type ConstructorArgs = ()
Source§impl TryInto<RateLimiter> for RateLimiterConfig
impl TryInto<RateLimiter> for RateLimiterConfig
Auto Trait Implementations§
impl Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnwindSafe for RateLimiter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.