throttle_machines 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +231 -0
- data/Cargo.lock +307 -0
- data/Cargo.toml +31 -0
- data/README.md +89 -14
- data/ext/throttle_machines_native/core/CHANGELOG.md +77 -0
- data/ext/throttle_machines_native/core/Cargo.toml +25 -0
- data/ext/throttle_machines_native/core/README.md +100 -0
- data/ext/throttle_machines_native/core/src/fixed_window.rs +172 -0
- data/ext/throttle_machines_native/core/src/gate.rs +82 -0
- data/ext/throttle_machines_native/core/src/gcra.rs +219 -0
- data/ext/throttle_machines_native/core/src/keyed.rs +325 -0
- data/ext/throttle_machines_native/core/src/lib.rs +50 -0
- data/ext/throttle_machines_native/core/src/refusal.rs +30 -0
- data/ext/throttle_machines_native/core/src/token_bucket.rs +191 -0
- data/ext/throttle_machines_native/extconf.rb +2 -0
- data/ext/throttle_machines_native/ffi/CHANGELOG.md +59 -0
- data/ext/throttle_machines_native/ffi/Cargo.toml +22 -0
- data/ext/throttle_machines_native/ffi/extconf.rb +14 -0
- data/ext/throttle_machines_native/ffi/src/init.rs +63 -0
- data/ext/throttle_machines_native/ffi/src/lib.rs +255 -0
- data/ext/throttle_machines_native/ffi/src/values.rs +194 -0
- data/lib/throttle_machines/async_breaker.rb +5 -0
- data/lib/throttle_machines/async_support.rb +16 -21
- data/lib/throttle_machines/breakers.rb +46 -0
- data/lib/throttle_machines/control.rb +25 -46
- data/lib/throttle_machines/controller_helpers.rb +11 -10
- data/lib/throttle_machines/hedged_breaker.rb +4 -6
- data/lib/throttle_machines/hedged_request.rb +29 -42
- data/lib/throttle_machines/instrumentation.rb +10 -75
- data/lib/throttle_machines/limiter.rb +71 -136
- data/lib/throttle_machines/middleware.rb +6 -6
- data/lib/throttle_machines/rack_middleware/allow2_ban.rb +1 -8
- data/lib/throttle_machines/rack_middleware/ban_filter.rb +3 -2
- data/lib/throttle_machines/rack_middleware/configuration.rb +4 -2
- data/lib/throttle_machines/rack_middleware/fail2_ban.rb +5 -21
- data/lib/throttle_machines/rack_middleware/throttle.rb +2 -1
- data/lib/throttle_machines/rack_middleware.rb +3 -1
- data/lib/throttle_machines/storage/base.rb +49 -48
- data/lib/throttle_machines/storage/cache.rb +12 -6
- data/lib/throttle_machines/storage/memory.rb +68 -96
- data/lib/throttle_machines/storage/null.rb +18 -28
- data/lib/throttle_machines/storage/redis/gcra.lua +21 -20
- data/lib/throttle_machines/storage/redis/increment_counter.lua +4 -4
- data/lib/throttle_machines/storage/redis/peek_gcra.lua +16 -16
- data/lib/throttle_machines/storage/redis/peek_token_bucket.lua +22 -18
- data/lib/throttle_machines/storage/redis/token_bucket.lua +31 -19
- data/lib/throttle_machines/storage/redis.rb +63 -69
- data/lib/throttle_machines/throttled_error.rb +4 -1
- data/lib/throttle_machines/version.rb +1 -1
- data/lib/throttle_machines.rb +130 -87
- data/sig/throttle_machines.rbs +555 -0
- metadata +55 -71
- data/lib/throttle_machines/async_limiter.rb +0 -24
- data/lib/throttle_machines/dependency_error.rb +0 -6
- data/lib/throttle_machines/native_speedup.rb +0 -110
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
# Every instant and span is Integer nanoseconds (`now`, periods, windows,
|
|
2
|
+
# intervals, TTLs, waits). Arguments are in 0..2**64-1, except a native
|
|
3
|
+
# gate's `now`, `tat`, `refilled_at` and `window_start`, which take any
|
|
4
|
+
# instant a Duration holds (0..(2**64 * 10**9) - 1).
|
|
5
|
+
#
|
|
6
|
+
# Every Data value validates when built, by the gem or by hand: TypeError for
|
|
7
|
+
# the wrong type, RangeError for a value the core rejects.
|
|
8
|
+
module ThrottleMachines
|
|
9
|
+
VERSION: String
|
|
10
|
+
NS_PER_SECOND: Integer
|
|
11
|
+
|
|
12
|
+
# Refusals: the call was not honoured and changed nothing. The native gates
|
|
13
|
+
# answer :throttled, and Gcra :overflow when an admission would push its
|
|
14
|
+
# TAT past the largest instant; a store that cannot track another key
|
|
15
|
+
# answers :full.
|
|
16
|
+
type refusal = :throttled | :full | :overflow
|
|
17
|
+
type answer = Integer | refusal
|
|
18
|
+
type algorithm = :fixed_window | :gcra | :token_bucket
|
|
19
|
+
|
|
20
|
+
class GcraPolicy < Data
|
|
21
|
+
attr_reader emission_interval: Integer
|
|
22
|
+
attr_reader delay_tolerance: Integer
|
|
23
|
+
def self.new: (Integer emission_interval, Integer delay_tolerance) -> GcraPolicy
|
|
24
|
+
| (emission_interval: Integer, delay_tolerance: Integer) -> GcraPolicy
|
|
25
|
+
def self.per: (Integer period, Integer limit, Integer burst) -> GcraPolicy
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class TokenBucketPolicy < Data
|
|
29
|
+
attr_reader capacity: Integer
|
|
30
|
+
attr_reader refill_every: Integer
|
|
31
|
+
def self.new: (Integer capacity, Integer refill_every) -> TokenBucketPolicy
|
|
32
|
+
| (capacity: Integer, refill_every: Integer) -> TokenBucketPolicy
|
|
33
|
+
def self.per: (Integer period, Integer limit) -> TokenBucketPolicy
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class FixedWindowPolicy < Data
|
|
37
|
+
attr_reader window: Integer
|
|
38
|
+
attr_reader limit: Integer
|
|
39
|
+
def self.new: (Integer window, Integer limit) -> FixedWindowPolicy
|
|
40
|
+
| (window: Integer, limit: Integer) -> FixedWindowPolicy
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The native gates. `check` admits one call and answers how many more would
|
|
44
|
+
# be admitted at the same instant; `peek` answers how many would be, at
|
|
45
|
+
# least one; both answer a refusal otherwise. `retry_after` answers the
|
|
46
|
+
# largest instant when no instant admits. A frozen gate raises
|
|
47
|
+
# FrozenError on `check` and `refund`.
|
|
48
|
+
class Gcra
|
|
49
|
+
def self.new: (GcraPolicy policy) -> Gcra
|
|
50
|
+
def self.restored: (GcraPolicy policy, Integer tat) -> Gcra
|
|
51
|
+
def check: (Integer now) -> answer
|
|
52
|
+
def peek: (Integer now) -> answer
|
|
53
|
+
def retry_after: (Integer now) -> Integer
|
|
54
|
+
def refund: () -> nil
|
|
55
|
+
def policy: () -> GcraPolicy
|
|
56
|
+
def tat: () -> Integer
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class TokenBucket
|
|
60
|
+
def self.new: (TokenBucketPolicy policy) -> TokenBucket
|
|
61
|
+
def self.restored: (TokenBucketPolicy policy, Integer tokens, Integer refilled_at) -> TokenBucket
|
|
62
|
+
def check: (Integer now) -> answer
|
|
63
|
+
def peek: (Integer now) -> answer
|
|
64
|
+
def retry_after: (Integer now) -> Integer
|
|
65
|
+
def refund: () -> nil
|
|
66
|
+
def policy: () -> TokenBucketPolicy
|
|
67
|
+
def tokens: () -> Integer
|
|
68
|
+
def refilled_at: () -> Integer
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
class FixedWindow
|
|
72
|
+
def self.new: (FixedWindowPolicy policy) -> FixedWindow
|
|
73
|
+
def self.restored: (FixedWindowPolicy policy, Integer count, Integer window_start) -> FixedWindow
|
|
74
|
+
def check: (Integer now) -> answer
|
|
75
|
+
def peek: (Integer now) -> answer
|
|
76
|
+
def retry_after: (Integer now) -> Integer
|
|
77
|
+
def refund: () -> nil
|
|
78
|
+
def policy: () -> FixedWindowPolicy
|
|
79
|
+
def count: () -> Integer
|
|
80
|
+
def window_start: () -> Integer
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
class Configuration
|
|
84
|
+
attr_accessor default_storage: untyped
|
|
85
|
+
attr_accessor default_algorithm: algorithm
|
|
86
|
+
attr_accessor instrumentation_enabled: bool
|
|
87
|
+
attr_accessor instrumentation_backend: untyped
|
|
88
|
+
attr_accessor _storage_instance: Storage::Base?
|
|
89
|
+
attr_reader fiber_safe: bool
|
|
90
|
+
def initialize: () -> void
|
|
91
|
+
def fiber_safe=: (untyped value) -> untyped
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def self.config: () -> Configuration
|
|
95
|
+
def self.monotonic_time: () -> Integer
|
|
96
|
+
def self.configure: () ?{ (Configuration) -> void } -> void
|
|
97
|
+
def self.storage: () -> Storage::Base
|
|
98
|
+
def self.storage=: (Symbol | Class | Storage::Base value) -> Storage::Base
|
|
99
|
+
def self.reset!: (?String? key) -> void
|
|
100
|
+
# The block runs instance_eval'd on the Control.
|
|
101
|
+
def self.control: (String key) ?{ (Control) [self: Control] -> void } -> Control
|
|
102
|
+
def self.limit: (String key, limit: Integer, period: Integer, ?algorithm: algorithm?) ?{ () -> untyped } -> untyped
|
|
103
|
+
def self.break_circuit: (String key, failures: Integer, within: Integer, ?timeout: Integer) { () -> untyped } -> untyped
|
|
104
|
+
def self.retry_with: (untyped strategy, Integer? max_attempts, **untyped runtime) { () -> untyped } -> untyped
|
|
105
|
+
def self.limiter: (String key, limit: Integer, period: Integer, ?algorithm: algorithm?) -> Limiter
|
|
106
|
+
def self.async_breaker: (*untyped, **untyped) -> untyped
|
|
107
|
+
def self.load_async_runtime!: () -> true
|
|
108
|
+
def self.hedged_request: (**untyped) { (Integer attempt) -> untyped } -> untyped
|
|
109
|
+
|
|
110
|
+
private def self.create_storage: (Symbol | Class | Storage::Base storage) -> Storage::Base
|
|
111
|
+
private def self.create_storage_from_symbol: (Symbol symbol) -> Storage::Base
|
|
112
|
+
|
|
113
|
+
# Native validators and constructors behind the Data values above
|
|
114
|
+
# (ext/throttle_machines_native/ffi/src/values.rs): each raises TypeError
|
|
115
|
+
# or RangeError for what the core rejects.
|
|
116
|
+
private def self.__gcra_policy: (Integer emission_interval, Integer delay_tolerance) -> nil
|
|
117
|
+
private def self.__token_bucket_policy: (Integer capacity, Integer refill_every) -> nil
|
|
118
|
+
private def self.__fixed_window_policy: (Integer window, Integer limit) -> nil
|
|
119
|
+
# [emission_interval, delay_tolerance]
|
|
120
|
+
private def self.__gcra_per: (Integer period, Integer limit, Integer burst) -> [Integer, Integer]
|
|
121
|
+
# [capacity, refill_every]
|
|
122
|
+
private def self.__token_bucket_per: (Integer period, Integer limit) -> [Integer, Integer]
|
|
123
|
+
|
|
124
|
+
CircuitOpenError: untyped
|
|
125
|
+
RetryExhaustedError: untyped
|
|
126
|
+
|
|
127
|
+
# The seam to BreakerMachines circuits: Policy shaping and spans read off a
|
|
128
|
+
# circuit's clock, all Integer nanoseconds.
|
|
129
|
+
module Breakers
|
|
130
|
+
module Failure
|
|
131
|
+
def self.===: (untyped error) -> bool
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# module_function: public on the module, private when included.
|
|
135
|
+
def self?.policy: (Integer failures, Integer window, Integer cooldown) -> untyped
|
|
136
|
+
def self?.dynamic: (String name, untyped owner, Integer failures, Integer window, Integer cooldown, **untyped runtime) -> untyped
|
|
137
|
+
def self?.since: (untyped circuit) -> Integer?
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
class Limiter
|
|
141
|
+
include AsyncSupport
|
|
142
|
+
|
|
143
|
+
ALGORITHMS: Array[algorithm]
|
|
144
|
+
attr_reader key: String
|
|
145
|
+
attr_reader limit: Integer
|
|
146
|
+
attr_reader period: Integer
|
|
147
|
+
attr_reader algorithm: algorithm
|
|
148
|
+
attr_reader storage: Storage::Base
|
|
149
|
+
attr_reader policy: GcraPolicy | TokenBucketPolicy | FixedWindowPolicy
|
|
150
|
+
def initialize: (String key, limit: Integer, period: Integer, ?algorithm: algorithm, ?storage: Storage::Base?) -> void
|
|
151
|
+
def allow?: () -> bool
|
|
152
|
+
def throttle!: () ?{ () -> untyped } -> untyped
|
|
153
|
+
def reset!: () -> untyped
|
|
154
|
+
def remaining: () -> Integer
|
|
155
|
+
def retry_after: () -> Integer
|
|
156
|
+
def to_h: () -> Hash[Symbol, untyped]
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
# Calls left after this admission, or a refusal.
|
|
161
|
+
def check: () -> answer
|
|
162
|
+
# Calls admitted now, at least one, or a refusal.
|
|
163
|
+
def peek: () -> answer
|
|
164
|
+
def build_policy: () -> (GcraPolicy | TokenBucketPolicy | FixedWindowPolicy)
|
|
165
|
+
def positive: (Integer value, String name) -> Integer
|
|
166
|
+
def validate_algorithm!: () -> void
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
module AsyncSupport
|
|
170
|
+
def allowed_async?: () -> bool
|
|
171
|
+
def check_async: () ?{ () -> untyped } -> bool
|
|
172
|
+
def throttle_async: (?max_wait: Integer?) ?{ () -> untyped } -> untyped
|
|
173
|
+
|
|
174
|
+
private
|
|
175
|
+
|
|
176
|
+
def sleep_for: (Integer nanoseconds) -> void
|
|
177
|
+
# The current Async::Task, or nil outside one (or without async).
|
|
178
|
+
def current_async_task: () -> untyped
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
class Control
|
|
182
|
+
attr_reader key: String
|
|
183
|
+
attr_reader limiter: Limiter?
|
|
184
|
+
attr_reader breaker: untyped
|
|
185
|
+
attr_reader retrier: untyped
|
|
186
|
+
def initialize: (String key) -> void
|
|
187
|
+
def limit: (rate: Integer, per: Integer, ?algorithm: algorithm) -> self
|
|
188
|
+
def break_on: (failures: Integer, within: Integer, ?timeout: Integer?) -> self
|
|
189
|
+
def retry_on_failure: (untyped strategy, Integer? max_attempts, **untyped runtime) -> self
|
|
190
|
+
def call: () { () -> untyped } -> untyped
|
|
191
|
+
|
|
192
|
+
private
|
|
193
|
+
|
|
194
|
+
def setup_components: () -> void
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
class ThrottledError < StandardError
|
|
198
|
+
attr_reader limiter: Limiter
|
|
199
|
+
def initialize: (Limiter limiter) -> void
|
|
200
|
+
def retry_after: () -> Integer
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
class UnsupportedOperationError < StandardError
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
class AsyncBreaker
|
|
207
|
+
def self.new: (String name, untyped policy, **untyped runtime) -> untyped
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
class HedgedRequest
|
|
211
|
+
attr_reader delay: Integer
|
|
212
|
+
attr_reader max_attempts: Integer
|
|
213
|
+
attr_reader timeout: Integer?
|
|
214
|
+
def initialize: (?delay: Integer, ?max_attempts: Integer, ?timeout: Integer?) -> void
|
|
215
|
+
def run: () { (Integer attempt) -> untyped } -> untyped
|
|
216
|
+
def run_async: () { (Integer attempt) -> untyped } -> untyped
|
|
217
|
+
def shutdown: () -> untyped
|
|
218
|
+
|
|
219
|
+
private
|
|
220
|
+
|
|
221
|
+
def span: (Integer value, String name) -> Integer
|
|
222
|
+
def seconds: (Integer nanoseconds) -> Rational
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
class HedgedBreaker
|
|
226
|
+
def initialize: (untyped breakers, ?delay: Integer) -> void
|
|
227
|
+
def run: () { () -> untyped } -> untyped
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
module Instrumentation
|
|
231
|
+
attr_writer self.enabled: bool
|
|
232
|
+
attr_writer self.backend: untyped
|
|
233
|
+
def self.enabled: () -> bool
|
|
234
|
+
def self.backend: () -> untyped
|
|
235
|
+
def self.active?: () -> bool
|
|
236
|
+
def self.instrument: (String event_name, ?Hash[Symbol, untyped] payload) ?{ () -> untyped } -> untyped
|
|
237
|
+
def self.rate_limit_checked: (Limiter limiter, allowed: bool, ?remaining: Integer?) -> untyped
|
|
238
|
+
def self.rate_limit_allowed: (Limiter limiter, ?remaining: Integer?) -> untyped
|
|
239
|
+
def self.rate_limit_throttled: (Limiter limiter, ?retry_after: Integer?) -> untyped
|
|
240
|
+
def self.hedged_request_started: (String request_id, attempts: Integer) -> untyped
|
|
241
|
+
def self.hedged_request_winner: (String request_id, attempt: Integer, duration: Integer) -> untyped
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
class Middleware
|
|
245
|
+
# The block runs instance_eval'd on the middleware.
|
|
246
|
+
def initialize: (untyped app) ?{ (Middleware) [self: Middleware] -> void } -> void
|
|
247
|
+
def call: (Hash[String, untyped] env) -> [Integer, Hash[String, String], Array[String]]
|
|
248
|
+
def throttle: (untyped path, limit: Integer, period: Integer, ?by: untyped, ?algorithm: algorithm) -> untyped
|
|
249
|
+
|
|
250
|
+
private
|
|
251
|
+
|
|
252
|
+
# A String, Regexp or callable path, as a request predicate.
|
|
253
|
+
def build_matcher: (untyped path) -> ^(untyped request) -> untyped
|
|
254
|
+
# :ip, a session Symbol, a callable or a literal, as a request key.
|
|
255
|
+
def build_key_generator: (untyped by) -> ^(untyped request) -> untyped
|
|
256
|
+
def rate_limit_response: (Limiter limiter) -> [Integer, Hash[String, String], Array[String]]
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
module ControllerHelpers
|
|
260
|
+
def throttle!: (?String? key, limit: Integer, period: Integer, ?algorithm: algorithm) -> untyped
|
|
261
|
+
def with_throttle: (?String? key, limit: Integer, period: Integer) { () -> untyped } -> untyped
|
|
262
|
+
def rate_limited?: (?String? key, limit: Integer, period: Integer) -> bool
|
|
263
|
+
def rate_limit_remaining: (?String? key, limit: Integer, period: Integer) -> Integer
|
|
264
|
+
|
|
265
|
+
private
|
|
266
|
+
|
|
267
|
+
def default_throttle_key: () -> String
|
|
268
|
+
def set_rate_limit_headers: (Limiter limiter) -> String
|
|
269
|
+
def render_rate_limited: (Limiter limiter) -> untyped
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
class RackMiddleware
|
|
273
|
+
attr_accessor self.enabled: bool
|
|
274
|
+
attr_accessor self.notifier: untyped
|
|
275
|
+
attr_reader self.configuration: Configuration
|
|
276
|
+
# The block runs instance_eval'd on the configuration.
|
|
277
|
+
def self.configure: () ?{ (Configuration) [self: Configuration] -> void } -> void
|
|
278
|
+
def self.reset!: () -> void
|
|
279
|
+
def self.clear!: () -> Configuration
|
|
280
|
+
def self.instrument: (untyped request) -> untyped
|
|
281
|
+
def self.throttle: (untyped name, ?Hash[Symbol, untyped] options) { (untyped) -> untyped } -> Throttle
|
|
282
|
+
def self.track: (untyped name, ?Hash[Symbol, untyped] options) { (untyped) -> untyped } -> Track
|
|
283
|
+
def self.safelist: (?untyped name) { (untyped) -> untyped } -> untyped
|
|
284
|
+
def self.blocklist: (?untyped name) { (untyped) -> untyped } -> untyped
|
|
285
|
+
def self.blocklist_ip: (String ip_address) -> untyped
|
|
286
|
+
def self.safelist_ip: (String ip_address) -> untyped
|
|
287
|
+
def self.fail2ban: (untyped name, ?Hash[Symbol, untyped] options) { (untyped) -> untyped } -> Fail2Ban
|
|
288
|
+
def self.allow2ban: (untyped name, ?Hash[Symbol, untyped] options) { (untyped) -> untyped } -> Allow2Ban
|
|
289
|
+
def self.throttled_responder: () -> untyped
|
|
290
|
+
def self.throttled_responder=: (untyped responder) -> untyped
|
|
291
|
+
def self.blocklisted_responder: () -> untyped
|
|
292
|
+
def self.blocklisted_responder=: (untyped responder) -> untyped
|
|
293
|
+
def self.throttles: () -> Hash[untyped, Throttle]
|
|
294
|
+
def self.tracks: () -> Hash[untyped, Track]
|
|
295
|
+
def self.safelists: () -> Hash[untyped, Safelist]
|
|
296
|
+
def self.blocklists: () -> Hash[untyped, Blocklist]
|
|
297
|
+
def self.fail2bans: () -> Hash[untyped, Fail2Ban]
|
|
298
|
+
def self.allow2bans: () -> Hash[untyped, Allow2Ban]
|
|
299
|
+
def initialize: (untyped app) -> void
|
|
300
|
+
def call: (Hash[String, untyped] env) -> untyped
|
|
301
|
+
|
|
302
|
+
class Configuration
|
|
303
|
+
DEFAULT_BLOCKLISTED_RESPONDER: untyped
|
|
304
|
+
DEFAULT_THROTTLED_RESPONDER: untyped
|
|
305
|
+
attr_reader throttles: Hash[untyped, Throttle]
|
|
306
|
+
attr_reader tracks: Hash[untyped, Track]
|
|
307
|
+
attr_reader safelists: Hash[untyped, Safelist]
|
|
308
|
+
attr_reader blocklists: Hash[untyped, Blocklist]
|
|
309
|
+
attr_reader fail2bans: Hash[untyped, Fail2Ban]
|
|
310
|
+
attr_reader allow2bans: Hash[untyped, Allow2Ban]
|
|
311
|
+
attr_accessor throttled_responder: untyped
|
|
312
|
+
attr_accessor blocklisted_responder: untyped
|
|
313
|
+
def initialize: () -> void
|
|
314
|
+
def throttle: (untyped name, ?Hash[Symbol, untyped] options) { (untyped) -> untyped } -> Throttle
|
|
315
|
+
def track: (untyped name, ?Hash[Symbol, untyped] options) { (untyped) -> untyped } -> Track
|
|
316
|
+
def safelist: (?untyped name) { (untyped) -> untyped } -> untyped
|
|
317
|
+
def blocklist: (?untyped name) { (untyped) -> untyped } -> untyped
|
|
318
|
+
def safelist_ip: (String ip_address) -> untyped
|
|
319
|
+
def blocklist_ip: (String ip_address) -> untyped
|
|
320
|
+
def fail2ban: (untyped name, ?Hash[Symbol, untyped] options) { (untyped) -> untyped } -> Fail2Ban
|
|
321
|
+
def allow2ban: (untyped name, ?Hash[Symbol, untyped] options) { (untyped) -> untyped } -> Allow2Ban
|
|
322
|
+
def safelisted?: (untyped request) -> bool
|
|
323
|
+
def blocklisted?: (untyped request) -> bool
|
|
324
|
+
def throttled?: (untyped request) -> bool
|
|
325
|
+
def tracked?: (untyped request) -> untyped
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
class Request
|
|
329
|
+
def user_agent: () -> String?
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
class ListFilter
|
|
333
|
+
attr_reader name: untyped
|
|
334
|
+
attr_reader block: untyped
|
|
335
|
+
def initialize: (untyped name) ?{ (untyped) -> untyped } -> void
|
|
336
|
+
def matched_by?: (untyped request) -> bool
|
|
337
|
+
|
|
338
|
+
private
|
|
339
|
+
|
|
340
|
+
def match_type: () -> Symbol
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
class Safelist < ListFilter
|
|
344
|
+
private
|
|
345
|
+
|
|
346
|
+
def match_type: () -> :safelist
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
class Blocklist < ListFilter
|
|
350
|
+
private
|
|
351
|
+
|
|
352
|
+
def match_type: () -> :blocklist
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# `findtime` and `bantime` are Integer nanoseconds.
|
|
356
|
+
class BanFilter
|
|
357
|
+
attr_reader name: untyped
|
|
358
|
+
attr_reader maxretry: Integer
|
|
359
|
+
attr_reader findtime: Integer
|
|
360
|
+
attr_reader bantime: Integer
|
|
361
|
+
attr_reader block: untyped
|
|
362
|
+
def initialize: (untyped name, Hash[Symbol, untyped] options) { (untyped) -> untyped } -> void
|
|
363
|
+
|
|
364
|
+
private
|
|
365
|
+
|
|
366
|
+
def discriminator_for: (untyped request) -> untyped
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
class Fail2Ban < BanFilter
|
|
370
|
+
def banned?: (untyped request) -> bool
|
|
371
|
+
def count: (untyped request) { () -> bool } -> untyped
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
class Allow2Ban < BanFilter
|
|
375
|
+
def matched_by?: (untyped request) -> false
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
class Throttle
|
|
379
|
+
attr_reader name: untyped
|
|
380
|
+
attr_reader limit: untyped
|
|
381
|
+
attr_reader period: untyped
|
|
382
|
+
attr_reader block: untyped
|
|
383
|
+
attr_reader algorithm: algorithm
|
|
384
|
+
def initialize: (untyped name, Hash[Symbol, untyped] options) { (untyped) -> untyped } -> void
|
|
385
|
+
def matched_by?: (untyped request) -> bool
|
|
386
|
+
|
|
387
|
+
private
|
|
388
|
+
|
|
389
|
+
def discriminator_for: (untyped request) -> untyped
|
|
390
|
+
# `limit` and `period` may be callables answering them per request.
|
|
391
|
+
def limit_for: (untyped request) -> Integer
|
|
392
|
+
def period_for: (untyped request) -> Integer
|
|
393
|
+
def annotate_request_with_throttle_data: (untyped request, Hash[Symbol, untyped] data) -> Hash[Symbol, untyped]
|
|
394
|
+
def annotate_request_with_matched_data: (untyped request, Hash[Symbol, untyped] data) -> Hash[Symbol, untyped]
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
class Track
|
|
398
|
+
attr_reader name: untyped
|
|
399
|
+
attr_reader block: untyped
|
|
400
|
+
attr_reader limit: Integer?
|
|
401
|
+
attr_reader period: Integer?
|
|
402
|
+
def initialize: (untyped name, ?Hash[Symbol, untyped] options) { (untyped) -> untyped } -> void
|
|
403
|
+
def matched_by?: (untyped request) -> false
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
module Storage
|
|
408
|
+
class Base
|
|
409
|
+
ALGORITHM_OPERATIONS: Hash[algorithm, Array[Symbol]]
|
|
410
|
+
HEALTH_KEY: String
|
|
411
|
+
UNTRACKED: Integer
|
|
412
|
+
KEY_SEPARATOR: String
|
|
413
|
+
GLOB_WILDCARD: String
|
|
414
|
+
GLOB_SPECIALS: Regexp
|
|
415
|
+
def initialize: (?Hash[Symbol, untyped] options) -> void
|
|
416
|
+
def supports?: (Symbol algorithm) -> bool
|
|
417
|
+
def increment_counter: (String key, Integer window, ?Integer amount) -> Integer
|
|
418
|
+
def get_counter: (String key, Integer window) -> Integer
|
|
419
|
+
def get_counter_ttl: (String key, Integer window) -> Integer
|
|
420
|
+
def reset_counter: (String key, Integer window) -> untyped
|
|
421
|
+
def check_gcra: (String key, GcraPolicy policy, Integer ttl) -> answer
|
|
422
|
+
def peek_gcra: (String key, GcraPolicy policy) -> answer
|
|
423
|
+
def gcra_retry_after: (String key, GcraPolicy policy) -> Integer
|
|
424
|
+
def reset_gcra: (String key) -> untyped
|
|
425
|
+
def check_token_bucket: (String key, TokenBucketPolicy policy, Integer ttl) -> answer
|
|
426
|
+
def peek_token_bucket: (String key, TokenBucketPolicy policy) -> answer
|
|
427
|
+
def token_bucket_retry_after: (String key, TokenBucketPolicy policy) -> Integer
|
|
428
|
+
def reset_token_bucket: (String key) -> untyped
|
|
429
|
+
def clear: (?String? pattern) -> untyped
|
|
430
|
+
def reset_key: (String key) -> untyped
|
|
431
|
+
def healthy?: () -> bool
|
|
432
|
+
# Timeout.timeout yields the seconds it was given.
|
|
433
|
+
def with_timeout: [T] (Integer timeout) { (Rational seconds) -> T } -> T
|
|
434
|
+
|
|
435
|
+
# Protected in Ruby; RBS has no protected, and every caller uses an
|
|
436
|
+
# implicit receiver.
|
|
437
|
+
private def owned_by?: (String stored_key, String key) -> bool
|
|
438
|
+
private def escape_glob: (String literal) -> String
|
|
439
|
+
private def glob_to_regexp: (String glob) -> Regexp
|
|
440
|
+
private def current_time: () -> Integer
|
|
441
|
+
|
|
442
|
+
private def unsupported: (Symbol operation) -> bot
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
class Memory < Base
|
|
446
|
+
DEFAULT_MAX_KEYS: Integer
|
|
447
|
+
CAPACITY_RETRY_AFTER: Integer
|
|
448
|
+
DEFAULT_CLEANUP_INTERVAL: Integer
|
|
449
|
+
DEFAULT_LOCK_POOL_SIZE: Integer
|
|
450
|
+
SHUTDOWN_GRACE: Float
|
|
451
|
+
# A stored counter ({count:, expires_at:}), GCRA state ({tat:,
|
|
452
|
+
# expires_at:}) or bucket ({tokens:, refilled_at:, expires_at:}).
|
|
453
|
+
type entry = Hash[Symbol, Integer]
|
|
454
|
+
|
|
455
|
+
def self.finalizer: (Thread cleanup_thread) -> Proc
|
|
456
|
+
def shutdown: () -> void
|
|
457
|
+
|
|
458
|
+
private
|
|
459
|
+
|
|
460
|
+
def delete_where: () { (String stored_key) -> boolish } -> void
|
|
461
|
+
def store_entry: (Hash[String, entry] store, String key, entry value) -> bool
|
|
462
|
+
def delete_entry: (Hash[String, entry] store, String key) -> entry?
|
|
463
|
+
def capacity_available?: (Hash[String, entry] store, String key) -> bool
|
|
464
|
+
# The key's gate, or nil when it is untracked and the store is full.
|
|
465
|
+
def gcra_gate: (String key, GcraPolicy policy) -> Gcra?
|
|
466
|
+
def bucket_gate: (String key, TokenBucketPolicy policy) -> TokenBucket?
|
|
467
|
+
def restored_bucket: (TokenBucketPolicy policy, entry entry) -> TokenBucket
|
|
468
|
+
def reclaim_capacity: () -> void
|
|
469
|
+
def with_read_lock: [T] (String key) { () -> T } -> T
|
|
470
|
+
def with_write_lock: [T] (String key) { () -> T } -> T
|
|
471
|
+
def clean_expired_entries: () -> void
|
|
472
|
+
|
|
473
|
+
# Every map, lock and counter the cleanup worker sweeps.
|
|
474
|
+
class State
|
|
475
|
+
attr_reader counters: Hash[String, entry]
|
|
476
|
+
attr_reader gcra_states: Hash[String, entry]
|
|
477
|
+
attr_reader token_buckets: Hash[String, entry]
|
|
478
|
+
def initialize: (max_keys: Integer, lock_pool_size: Integer, cleanup_interval: Integer) -> void
|
|
479
|
+
def start_cleanup_thread: () -> Thread
|
|
480
|
+
def shutdown!: () -> true
|
|
481
|
+
def stores: () -> [Hash[String, entry], Hash[String, entry], Hash[String, entry]]
|
|
482
|
+
def stored_keys: () -> Integer
|
|
483
|
+
def saturated?: () -> bool
|
|
484
|
+
def with_read_lock: [T] (String key) { () -> T } -> T
|
|
485
|
+
def with_write_lock: [T] (String key) { () -> T } -> T
|
|
486
|
+
# false when the key is new and the store is at max_keys.
|
|
487
|
+
def store_entry: (Hash[String, entry] store, String key, entry value) -> bool
|
|
488
|
+
def delete_entry: (Hash[String, entry] store, String key) -> entry?
|
|
489
|
+
def capacity_available?: (Hash[String, entry] store, String key) -> bool
|
|
490
|
+
def clear_all: () -> void
|
|
491
|
+
def clean_expired_entries: () -> void
|
|
492
|
+
|
|
493
|
+
private
|
|
494
|
+
|
|
495
|
+
# The key's stripe, a Concurrent::ReadWriteLock.
|
|
496
|
+
def lock_for: (String key) -> untyped
|
|
497
|
+
end
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
class Redis < Base
|
|
501
|
+
NS_PER_MILLISECOND: Integer
|
|
502
|
+
LUA_SCRIPTS_DIR: String
|
|
503
|
+
GCRA_SCRIPT: String
|
|
504
|
+
TOKEN_BUCKET_SCRIPT: String
|
|
505
|
+
PEEK_GCRA_SCRIPT: String
|
|
506
|
+
PEEK_TOKEN_BUCKET_SCRIPT: String
|
|
507
|
+
INCREMENT_COUNTER_SCRIPT: String
|
|
508
|
+
GCRA_SHA: String
|
|
509
|
+
TOKEN_BUCKET_SHA: String
|
|
510
|
+
PEEK_GCRA_SHA: String
|
|
511
|
+
PEEK_TOKEN_BUCKET_SHA: String
|
|
512
|
+
INCREMENT_COUNTER_SHA: String
|
|
513
|
+
SCAN_BATCH: Integer
|
|
514
|
+
|
|
515
|
+
# A peek script's reply: [allowed (1 or 0), calls admitted, nanoseconds
|
|
516
|
+
# to wait]. A refusal formats the wait as a decimal String, past what a
|
|
517
|
+
# Lua integer reply holds exactly.
|
|
518
|
+
type peek_reply = [Integer, Integer, Integer | String]
|
|
519
|
+
|
|
520
|
+
private
|
|
521
|
+
|
|
522
|
+
def delete_state: (String key) -> Integer
|
|
523
|
+
def delete_matching: (String scan_pattern) -> void
|
|
524
|
+
def admitted: (peek_reply reply) -> answer
|
|
525
|
+
def peek_gcra_reply: (String key, GcraPolicy policy) -> peek_reply
|
|
526
|
+
def peek_token_bucket_reply: (String key, TokenBucketPolicy policy) -> peek_reply
|
|
527
|
+
def window_key: (String key, Integer window) -> String
|
|
528
|
+
# Whole milliseconds, rounded up, at least one.
|
|
529
|
+
def milliseconds: (Integer nanoseconds) -> Integer
|
|
530
|
+
def prefixed: (String key) -> String
|
|
531
|
+
def run_script: (String script, String sha, keys: Array[String], argv: Array[Integer]) -> untyped
|
|
532
|
+
def validate_redis_connection!: () -> void
|
|
533
|
+
# Yields the client, or a connection checked out of a pool.
|
|
534
|
+
def with_redis: [T] () { (untyped redis) -> T } -> T
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
class Cache < Base
|
|
538
|
+
PATTERN_SPECIALS: Regexp
|
|
539
|
+
|
|
540
|
+
private
|
|
541
|
+
|
|
542
|
+
def window_key: (String key, Integer window) -> String
|
|
543
|
+
def positive_window: (Integer window) -> Integer
|
|
544
|
+
def delete_matched: (String | Regexp matcher) -> untyped
|
|
545
|
+
# A glob for a Redis-backed cache, a Regexp otherwise.
|
|
546
|
+
def matcher_for: (String glob) -> (String | Regexp)
|
|
547
|
+
def prefix_matcher: (String literal) -> (String | Regexp)
|
|
548
|
+
# Integer nanoseconds since the Unix epoch.
|
|
549
|
+
def wall_time: () -> Integer
|
|
550
|
+
end
|
|
551
|
+
|
|
552
|
+
class Null < Base
|
|
553
|
+
end
|
|
554
|
+
end
|
|
555
|
+
end
|