Struct kvarn::limiting::Manager

source ·
pub struct Manager { /* private fields */ }
Expand description

Data used to limit requests.

One instance of this is used per Listener. It counts using a atomic::AtomicUsize and keeps the count of requests per IpAddr in a Mutex, for fast access times and cheap cloning.

Implementations§

source§

impl LimitManager

source

pub fn new(max_requests: usize, check_every: usize, reset_seconds: f64) -> Self

Creates a new manager.

Use LimitManager::default for sane defaults.

The number of allowed requests per reset is max_requests * check_every. After reset_seconds, all data is cleared.

As the math implies, increasing max_requests and lowering reset_seconds does nothing to the amount of accepted requests. Though, if you have large reset_seconds, it’ll take longer for the limits to clear after the user has reached max_requests.

source

pub fn disable(&mut self) -> &mut Self

Disables limiting of this manager.

source

pub fn set_check_every(&mut self, check_every: usize) -> &mut Self

Sets the number of calls in between checking the request.

This is here to not have to check every request.

source

pub fn set_max_requests(&mut self, max_requests: usize) -> &mut Self

Sets the max requests in the current cycle (which resets after Self::set_reset_seconds). If the amount of requests from a IP address exceeds max_requests in the cycle, the request is denied.

source

pub fn set_reset_seconds(&mut self, reset_seconds: f64) -> &mut Self

Sets the interval to clear all limits.

See Self::new for considerations when making this value large.

source

pub fn register(&self, addr: IpAddr) -> Action

Registers a request from addr.

This is called twice, once when a new connection is established, and once when a new request is made.

Does not always lock the Mutex, only once per check_every. It only atomic::AtomicUsize::fetch_add else, with atomic::Ordering::Relaxed. This is less reliable, but faster. We do not require this to be to be exact.

Trait Implementations§

source§

impl Clone for Manager

source§

fn clone(&self) -> Manager

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Manager

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for LimitManager

Default is Self::new(10, 10, 10).

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more