Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
RateLimit.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2021 Reactive Markets Limited
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef TOOLBOX_NET_RATELIMIT_HPP
17#define TOOLBOX_NET_RATELIMIT_HPP
18
19#include <toolbox/sys/Time.hpp>
20
21#include <boost/container/small_vector.hpp>
22
23namespace toolbox {
24inline namespace net {
25
29 public:
30 template <typename intervalT = Seconds>
31 constexpr RateLimit(std::size_t limit, intervalT interval) noexcept
32 : limit_{limit}
33 , interval_{std::chrono::duration_cast<Decis>(interval)}
34 {
35 assert(interval_.count());
36 }
37 ~RateLimit() = default;
38
39 // Copy.
40 constexpr RateLimit(const RateLimit&) noexcept = default;
41 constexpr RateLimit& operator=(const RateLimit&) noexcept = default;
42
43 // Move.
44 constexpr RateLimit(RateLimit&&) noexcept = default;
45 constexpr RateLimit& operator=(RateLimit&&) noexcept = default;
46
47 constexpr auto limit() const noexcept { return limit_; }
48 constexpr auto interval() const noexcept { return interval_; }
49
50 private:
51 std::size_t limit_;
52 Decis interval_;
53};
54
55TOOLBOX_API RateLimit parse_rate_limit(const std::string& s);
56TOOLBOX_API std::istream& operator>>(std::istream& is, RateLimit& rl);
57TOOLBOX_API std::ostream& operator<<(std::ostream& os, RateLimit rl);
58
61 public:
62 template <typename intervalT = Seconds>
63 explicit RateWindow(intervalT interval)
64 : buckets_(std::chrono::duration_cast<Decis>(interval).count())
65 {
66 assert(buckets_.size());
67 }
69
70 // Copy.
73
74 // Move.
76 RateWindow& operator=(RateWindow&&) noexcept;
77
78 std::size_t count() const noexcept { return count_; }
81 void add(MonoTime time, std::size_t count = 1) noexcept;
82
83 private:
84 std::size_t& at(std::time_t t) noexcept { return buckets_[t % buckets_.size()]; }
85 std::size_t count_{};
86 Decis last_time_{};
87 boost::container::small_vector<std::size_t, 10> buckets_;
88};
89} // namespace net
90
91inline namespace util {
92template <>
93struct TypeTraits<net::RateLimit> {
94 static auto from_string(std::string_view sv) { return net::parse_rate_limit(std::string{sv}); }
95 static auto from_string(const std::string& s) { return net::parse_rate_limit(s); }
96};
97} // namespace util
98} // namespace toolbox
99
100#endif // TOOLBOX_NET_RATELIMIT_HPP
#define TOOLBOX_API
Definition Config.h:39
constexpr RateLimit(const RateLimit &) noexcept=default
constexpr auto interval() const noexcept
Definition RateLimit.hpp:48
constexpr RateLimit & operator=(const RateLimit &) noexcept=default
constexpr RateLimit(std::size_t limit, intervalT interval) noexcept
Definition RateLimit.hpp:31
constexpr RateLimit(RateLimit &&) noexcept=default
RateWindow maintains a sliding window of second time buckets for the specified interval.
Definition RateLimit.hpp:60
RateWindow(RateWindow &&) noexcept
RateWindow & operator=(const RateWindow &)
RateWindow(intervalT interval)
Definition RateLimit.hpp:63
RateWindow(const RateWindow &)
STL namespace.
ostream & operator<<(ostream &os, const pair< T, U > &p)
Definition Parser.ut.cpp:29
istream & operator>>(istream &is, DgramEndpoint &ep)
Definition Endpoint.cpp:100
RateLimit parse_rate_limit(const string &s)
Definition RateLimit.cpp:24
MonoClock::time_point MonoTime
Definition Time.hpp:110
std::chrono::duration< int64_t, std::deci > Decis
Definition Time.hpp:34
std::string_view sv
Definition Tokeniser.hpp:26
constexpr auto bind() noexcept
Definition Slot.hpp:92
static constexpr auto from_string(StringT &&s) noexcept(noexcept(ValueT{s}))