R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rtype::RateLimiter Class Reference

Counts the datagrams of each address in windows of one second. More...

#include </__w/project-rtype/project-rtype/server/include/rtype/rate_limit.hpp>

Public Types

using Clock = std::chrono::steady_clock
 Clock of the windows.
 

Public Member Functions

 RateLimiter (std::uint32_t per_second) noexcept
 Creates a limiter with no address yet.
 
bool allow (const aegis2d::net::Endpoint &source, Clock::time_point now)
 Counts a datagram and tells whether it may be handled.
 
void prune (Clock::time_point now)
 Forgets the addresses whose window ended, to bound the memory a flood of forged addresses can take.
 
std::size_t addresses () const noexcept
 Number of addresses being counted.
 

Static Public Attributes

static constexpr std::chrono::seconds window {1}
 Length of a window.
 

Detailed Description

Counts the datagrams of each address in windows of one second.

The first datagrams_per_second datagrams of a window pass; the next ones are refused until the window ends. Addresses silent for a whole window are forgotten by prune(). Not thread-safe: use it from the network thread.

Member Typedef Documentation

◆ Clock

using rtype::RateLimiter::Clock = std::chrono::steady_clock

Clock of the windows.

Constructor & Destructor Documentation

◆ RateLimiter()

rtype::RateLimiter::RateLimiter ( std::uint32_t  per_second)
explicitnoexcept

Creates a limiter with no address yet.

Parameters
per_secondDatagrams accepted per address and window; 0 refuses everything.

Member Function Documentation

◆ addresses()

std::size_t rtype::RateLimiter::addresses ( ) const
noexcept

Number of addresses being counted.

Returns
The count.

◆ allow()

bool rtype::RateLimiter::allow ( const aegis2d::net::Endpoint &  source,
Clock::time_point  now 
)

Counts a datagram and tells whether it may be handled.

Parameters
sourceSource of the datagram; only its address counts.
nowCurrent time.
Returns
false if the address already sent its datagrams of the window.

◆ prune()

void rtype::RateLimiter::prune ( Clock::time_point  now)

Forgets the addresses whose window ended, to bound the memory a flood of forged addresses can take.

Parameters
nowCurrent time.

Member Data Documentation

◆ window

constexpr std::chrono::seconds rtype::RateLimiter::window {1}
staticconstexpr

Length of a window.


The documentation for this class was generated from the following file: