4#include <aegis2d/net/endpoint.hpp>
12#include <unordered_map>
30[[nodiscard]] aegis2d::net::Endpoint
address_of(
const aegis2d::net::Endpoint& endpoint)
noexcept;
39 using Clock = std::chrono::steady_clock;
42 static constexpr std::chrono::seconds
window{1};
52 [[nodiscard]]
bool allow(
const aegis2d::net::Endpoint& source, Clock::time_point now);
57 void prune(Clock::time_point now);
61 [[nodiscard]] std::size_t
addresses() const noexcept;
65 Clock::time_point start;
66 std::uint32_t count = 0;
69 std::uint32_t per_second_;
70 std::unordered_map<aegis2d::net::Endpoint, Window> windows_;
79 using Clock = std::chrono::steady_clock;
81 static constexpr std::uint32_t
burst = 5;
83 static constexpr std::chrono::seconds
refill{1};
88 [[nodiscard]]
bool allow(Clock::time_point now)
noexcept;
91 std::uint32_t tokens_ =
burst;
93 std::optional<Clock::time_point> since_;
102 using Clock = std::chrono::steady_clock;
113 bool count(
const aegis2d::net::Endpoint& source, std::string_view reason,
114 Clock::time_point now);
121 [[nodiscard]] std::vector<std::string>
take_due(Clock::time_point now);
125 Clock::time_point since;
126 std::map<std::string_view, std::uint64_t> reasons;
129 std::unordered_map<aegis2d::net::Endpoint, Source> sources_;
The chat rate of one session (RFC section 3.8): a token bucket.
Definition rate_limit.hpp:76
std::chrono::steady_clock Clock
Clock of the refills.
Definition rate_limit.hpp:79
static constexpr std::uint32_t burst
Messages a session may send at once.
Definition rate_limit.hpp:81
static constexpr std::chrono::seconds refill
Time for one message to come back.
Definition rate_limit.hpp:83
bool allow(Clock::time_point now) noexcept
Counts a message and tells whether it may be relayed.
Counts the datagrams dropped from each address, by reason, for a summary in the log at most every rep...
Definition rate_limit.hpp:99
bool count(const aegis2d::net::Endpoint &source, std::string_view reason, Clock::time_point now)
Counts a dropped datagram.
static constexpr std::chrono::seconds report_period
Shortest time between two summaries of one address.
Definition rate_limit.hpp:105
std::chrono::steady_clock Clock
Clock of the reports.
Definition rate_limit.hpp:102
std::vector< std::string > take_due(Clock::time_point now)
Takes the summaries due: one line per address whose first drop since its last summary is at least rep...
Counts the datagrams of each address in windows of one second.
Definition rate_limit.hpp:36
void prune(Clock::time_point now)
Forgets the addresses whose window ended, to bound the memory a flood of forged addresses can take.
static constexpr std::chrono::seconds window
Length of a window.
Definition rate_limit.hpp:42
std::size_t addresses() const noexcept
Number of addresses being counted.
RateLimiter(std::uint32_t per_second) noexcept
Creates a limiter with no address yet.
std::chrono::steady_clock Clock
Clock of the windows.
Definition rate_limit.hpp:39
bool allow(const aegis2d::net::Endpoint &source, Clock::time_point now)
Counts a datagram and tells whether it may be handled.
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
aegis2d::net::Endpoint address_of(const aegis2d::net::Endpoint &endpoint) noexcept
The address of an endpoint, its port set to 0: the key of the limits.
Limits on the traffic of one IP address.
Definition rate_limit.hpp:18
std::uint32_t connections_per_address
TCP connections open at once from one address.
Definition rate_limit.hpp:24
std::uint32_t datagrams_per_second
Datagrams accepted from one address per second, its ports together.
Definition rate_limit.hpp:22