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
20#include <toolbox/sys/Time.hpp>
21
22#include <boost/container/small_vector.hpp>
23
24namespace toolbox {
25inline namespace net {
26
30 public:
31 template <typename intervalT = Seconds>
32 constexpr RateLimit(std::size_t limit, intervalT interval) noexcept
33 : limit_{limit}
34 , interval_{std::chrono::duration_cast<Decis>(interval)}
35 {
36 assert(interval_.count());
37 }
38 ~RateLimit() = default;
39
40 // Copy.
41 constexpr RateLimit(const RateLimit&) noexcept = default;
42 constexpr RateLimit& operator=(const RateLimit&) noexcept = default;
43
44 // Move.
45 constexpr RateLimit(RateLimit&&) noexcept = default;
46 constexpr RateLimit& operator=(RateLimit&&) noexcept = default;
47
48 constexpr auto limit() const noexcept { return limit_; }
49 constexpr auto interval() const noexcept { return interval_; }
50
51 private:
52 std::size_t limit_;
53 Decis interval_;
54};
55
56TOOLBOX_API RateLimit parse_rate_limit(const std::string& s);
57TOOLBOX_API std::istream& operator>>(std::istream& is, RateLimit& rl);
58
59template <typename StreamT>
60 requires Streamable<StreamT>
62{
63 os << rl.limit() << '/' << rl.interval().count();
64 return os;
65}
66
69 public:
70 template <typename intervalT = Seconds>
71 explicit RateWindow(intervalT interval)
72 : buckets_(std::chrono::duration_cast<Decis>(interval).count())
73 {
74 assert(buckets_.size());
75 }
77
78 // Copy.
81
82 // Move.
84 RateWindow& operator=(RateWindow&&) noexcept;
85
86 std::size_t count() const noexcept { return count_; }
89 void add(MonoTime time, std::size_t count = 1) noexcept;
90
91 private:
92 std::size_t& at(std::time_t t) noexcept { return buckets_[t % buckets_.size()]; }
93 std::size_t count_{};
94 Decis last_time_{};
95 boost::container::small_vector<std::size_t, 10> buckets_;
96};
97} // namespace net
98
99inline namespace util {
100template <>
101struct TypeTraits<net::RateLimit> {
102 static auto from_string(std::string_view sv) { return net::parse_rate_limit(std::string{sv}); }
103 static auto from_string(const std::string& s) { return net::parse_rate_limit(s); }
104};
105} // namespace util
106} // namespace toolbox
107
108#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:49
constexpr RateLimit & operator=(const RateLimit &) noexcept=default
constexpr RateLimit(std::size_t limit, intervalT interval) noexcept
Definition RateLimit.hpp:32
constexpr RateLimit(RateLimit &&) noexcept=default
RateWindow maintains a sliding window of second time buckets for the specified interval.
Definition RateLimit.hpp:68
RateWindow(RateWindow &&) noexcept
RateWindow & operator=(const RateWindow &)
RateWindow(intervalT interval)
Definition RateLimit.hpp:71
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}))