R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rate_limit.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/net/endpoint.hpp>
5
6#include <chrono>
7#include <cstdint>
8#include <map>
9#include <optional>
10#include <string>
11#include <string_view>
12#include <unordered_map>
13#include <vector>
14
15namespace rtype {
16
18struct RateLimits {
22 std::uint32_t datagrams_per_second = 300;
24 std::uint32_t connections_per_address = 8;
25};
26
30[[nodiscard]] aegis2d::net::Endpoint address_of(const aegis2d::net::Endpoint& endpoint) noexcept;
31
37public:
39 using Clock = std::chrono::steady_clock;
40
42 static constexpr std::chrono::seconds window{1};
43
46 explicit RateLimiter(std::uint32_t per_second) noexcept;
47
52 [[nodiscard]] bool allow(const aegis2d::net::Endpoint& source, Clock::time_point now);
53
57 void prune(Clock::time_point now);
58
61 [[nodiscard]] std::size_t addresses() const noexcept;
62
63private:
64 struct Window {
65 Clock::time_point start;
66 std::uint32_t count = 0;
67 };
68
69 std::uint32_t per_second_;
70 std::unordered_map<aegis2d::net::Endpoint, Window> windows_;
71};
72
76class ChatRate {
77public:
79 using Clock = std::chrono::steady_clock;
81 static constexpr std::uint32_t burst = 5;
83 static constexpr std::chrono::seconds refill{1};
84
88 [[nodiscard]] bool allow(Clock::time_point now) noexcept;
89
90private:
91 std::uint32_t tokens_ = burst;
92 // When the last message came back, or when the bucket was last full.
93 std::optional<Clock::time_point> since_;
94};
95
100public:
102 using Clock = std::chrono::steady_clock;
103
105 static constexpr std::chrono::seconds report_period{10};
106
113 bool count(const aegis2d::net::Endpoint& source, std::string_view reason,
114 Clock::time_point now);
115
121 [[nodiscard]] std::vector<std::string> take_due(Clock::time_point now);
122
123private:
124 struct Source {
125 Clock::time_point since;
126 std::map<std::string_view, std::uint64_t> reasons;
127 };
128
129 std::unordered_map<aegis2d::net::Endpoint, Source> sources_;
130};
131
132} // namespace rtype
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