Fastly Compute C++ SDK
Loading...
Searching...
No Matches
erl.h
Go to the documentation of this file.
1#ifndef FASTLY_ERL_H
2#define FASTLY_ERL_H
3
4#include <chrono>
5#include <fastly/expected.h>
6
7namespace fastly::erl {
9class ERLError {
10public:
16 explicit ERLError(Code code) : code_(code) {}
17 Code code() const { return code_; }
18
19private:
20 Code code_;
21};
22
26public:
27 explicit PenaltyBox(std::string name) : name_(std::move(name)) {};
30 tl::expected<void, ERLError> add(std::string_view entry,
31 std::chrono::minutes ttl);
33 tl::expected<bool, ERLError> has(std::string_view entry) const;
34 std::string_view name() const { return name_; }
35
36private:
37 std::string name_;
38};
39
41enum class CounterDuration {
42 TenSec = 10,
48};
49
52enum class RateWindow {
53 OneSec = 1,
54 TenSecs = 10,
56};
57
61public:
62 explicit RateCounter(std::string name) : name_(std::move(name)) {}
64 tl::expected<void, ERLError> increment(std::string_view entry,
65 std::uint32_t delta);
67 tl::expected<std::uint32_t, ERLError> lookup_rate(std::string_view entry,
68 RateWindow window) const;
70 tl::expected<std::uint32_t, ERLError>
71 lookup_count(std::string_view entry, CounterDuration duration) const;
72 std::string_view name() const { return name_; }
73
74private:
75 std::string name_;
76};
77
78class ERL {
79public:
81 : rate_counter_(std::move(rate_counter)),
82 penalty_box_(std::move(penalty_box)) {}
83
89 tl::expected<bool, ERLError>
90 check_rate(std::string_view entry, std::uint32_t delta, RateWindow window,
91 std::uint32_t limit, std::chrono::minutes ttl) const;
92
93 const RateCounter &rate_counter() const { return rate_counter_; }
94 const PenaltyBox &penalty_box() const { return penalty_box_; }
95
96private:
97 RateCounter rate_counter_;
98 PenaltyBox penalty_box_;
99};
100} // namespace fastly::erl
101#endif
ERLError(Code code)
Definition erl.h:16
Code
Definition erl.h:11
@ InvalidArgument
Definition erl.h:14
@ Unknown
Definition erl.h:13
@ Unexpected
Definition erl.h:12
Code code() const
Definition erl.h:17
const RateCounter & rate_counter() const
Definition erl.h:93
ERL(RateCounter rate_counter, PenaltyBox penalty_box)
Definition erl.h:80
const PenaltyBox & penalty_box() const
Definition erl.h:94
tl::expected< bool, ERLError > check_rate(std::string_view entry, std::uint32_t delta, RateWindow window, std::uint32_t limit, std::chrono::minutes ttl) const
Definition erl.h:25
PenaltyBox(std::string name)
Definition erl.h:27
std::string_view name() const
Definition erl.h:34
tl::expected< bool, ERLError > has(std::string_view entry) const
Check if entry is in the penaltybox.
tl::expected< void, ERLError > add(std::string_view entry, std::chrono::minutes ttl)
Definition erl.h:60
tl::expected< std::uint32_t, ERLError > lookup_count(std::string_view entry, CounterDuration duration) const
Lookup the current count for entry in the rate counter for a duration.
std::string_view name() const
Definition erl.h:72
tl::expected< std::uint32_t, ERLError > lookup_rate(std::string_view entry, RateWindow window) const
Lookup the current rate for entry in the rate counter for a window.
tl::expected< void, ERLError > increment(std::string_view entry, std::uint32_t delta)
Increment an entry in the ratecounter by delta.
RateCounter(std::string name)
Definition erl.h:62
Definition erl.h:7
RateWindow
Definition erl.h:52
@ TenSecs
Definition erl.h:54
@ OneSec
Definition erl.h:53
CounterDuration
To be used for picking the duration in a rate counter lookup_count call.
Definition erl.h:41
@ TwentySecs
Definition erl.h:43
@ FortySecs
Definition erl.h:45
@ TenSec
Definition erl.h:42
@ FiftySecs
Definition erl.h:46
@ SixtySecs
Definition erl.h:47
@ ThirtySecs
Definition erl.h:44