throttle_machines 0.4.2 → 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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +231 -0
  3. data/Cargo.lock +307 -0
  4. data/Cargo.toml +31 -0
  5. data/README.md +89 -14
  6. data/ext/throttle_machines_native/core/CHANGELOG.md +77 -0
  7. data/ext/throttle_machines_native/core/Cargo.toml +25 -0
  8. data/ext/throttle_machines_native/core/README.md +100 -0
  9. data/ext/throttle_machines_native/core/src/fixed_window.rs +172 -0
  10. data/ext/throttle_machines_native/core/src/gate.rs +82 -0
  11. data/ext/throttle_machines_native/core/src/gcra.rs +219 -0
  12. data/ext/throttle_machines_native/core/src/keyed.rs +325 -0
  13. data/ext/throttle_machines_native/core/src/lib.rs +50 -0
  14. data/ext/throttle_machines_native/core/src/refusal.rs +30 -0
  15. data/ext/throttle_machines_native/core/src/token_bucket.rs +191 -0
  16. data/ext/throttle_machines_native/extconf.rb +2 -0
  17. data/ext/throttle_machines_native/ffi/CHANGELOG.md +59 -0
  18. data/ext/throttle_machines_native/ffi/Cargo.toml +22 -0
  19. data/ext/throttle_machines_native/ffi/extconf.rb +14 -0
  20. data/ext/throttle_machines_native/ffi/src/init.rs +63 -0
  21. data/ext/throttle_machines_native/ffi/src/lib.rs +255 -0
  22. data/ext/throttle_machines_native/ffi/src/values.rs +194 -0
  23. data/lib/throttle_machines/async_breaker.rb +5 -0
  24. data/lib/throttle_machines/async_support.rb +16 -21
  25. data/lib/throttle_machines/breakers.rb +46 -0
  26. data/lib/throttle_machines/control.rb +25 -46
  27. data/lib/throttle_machines/controller_helpers.rb +11 -10
  28. data/lib/throttle_machines/hedged_breaker.rb +4 -6
  29. data/lib/throttle_machines/hedged_request.rb +29 -42
  30. data/lib/throttle_machines/instrumentation.rb +10 -75
  31. data/lib/throttle_machines/limiter.rb +71 -136
  32. data/lib/throttle_machines/middleware.rb +6 -6
  33. data/lib/throttle_machines/rack_middleware/allow2_ban.rb +1 -8
  34. data/lib/throttle_machines/rack_middleware/ban_filter.rb +3 -2
  35. data/lib/throttle_machines/rack_middleware/configuration.rb +4 -2
  36. data/lib/throttle_machines/rack_middleware/fail2_ban.rb +5 -21
  37. data/lib/throttle_machines/rack_middleware/throttle.rb +2 -1
  38. data/lib/throttle_machines/rack_middleware.rb +3 -1
  39. data/lib/throttle_machines/storage/base.rb +49 -48
  40. data/lib/throttle_machines/storage/cache.rb +12 -6
  41. data/lib/throttle_machines/storage/memory.rb +68 -96
  42. data/lib/throttle_machines/storage/null.rb +18 -28
  43. data/lib/throttle_machines/storage/redis/gcra.lua +21 -20
  44. data/lib/throttle_machines/storage/redis/increment_counter.lua +4 -4
  45. data/lib/throttle_machines/storage/redis/peek_gcra.lua +16 -16
  46. data/lib/throttle_machines/storage/redis/peek_token_bucket.lua +22 -18
  47. data/lib/throttle_machines/storage/redis/token_bucket.lua +31 -19
  48. data/lib/throttle_machines/storage/redis.rb +63 -69
  49. data/lib/throttle_machines/throttled_error.rb +4 -1
  50. data/lib/throttle_machines/version.rb +1 -1
  51. data/lib/throttle_machines.rb +130 -87
  52. data/sig/throttle_machines.rbs +555 -0
  53. metadata +55 -71
  54. data/lib/throttle_machines/async_limiter.rb +0 -24
  55. data/lib/throttle_machines/dependency_error.rb +0 -6
  56. data/lib/throttle_machines/native_speedup.rb +0 -110
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: throttle_machines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
@@ -52,141 +52,124 @@ dependencies:
52
52
  - !ruby/object:Gem::Version
53
53
  version: '3.0'
54
54
  - !ruby/object:Gem::Dependency
55
- name: zeitwerk
55
+ name: rb_sys
56
56
  requirement: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '2.7'
60
+ version: '0.9'
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 0.9.130
61
64
  type: :runtime
62
65
  prerelease: false
63
66
  version_requirements: !ruby/object:Gem::Requirement
64
67
  requirements:
65
68
  - - "~>"
66
69
  - !ruby/object:Gem::Version
67
- version: '2.7'
70
+ version: '0.9'
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 0.9.130
68
74
  - !ruby/object:Gem::Dependency
69
- name: breaker_machines
75
+ name: zeitwerk
70
76
  requirement: !ruby/object:Gem::Requirement
71
77
  requirements:
72
- - - ">="
78
+ - - "~>"
73
79
  - !ruby/object:Gem::Version
74
- version: 0.24.0
80
+ version: '2.7'
75
81
  type: :runtime
76
82
  prerelease: false
77
83
  version_requirements: !ruby/object:Gem::Requirement
78
84
  requirements:
79
- - - ">="
85
+ - - "~>"
80
86
  - !ruby/object:Gem::Version
81
- version: 0.24.0
87
+ version: '2.7'
82
88
  - !ruby/object:Gem::Dependency
83
- name: chrono_machines
89
+ name: breaker_machines
84
90
  requirement: !ruby/object:Gem::Requirement
85
91
  requirements:
86
92
  - - ">="
87
93
  - !ruby/object:Gem::Version
88
- version: 0.12.1
94
+ version: 0.28.0
89
95
  type: :runtime
90
96
  prerelease: false
91
97
  version_requirements: !ruby/object:Gem::Requirement
92
98
  requirements:
93
99
  - - ">="
94
100
  - !ruby/object:Gem::Version
95
- version: 0.12.1
101
+ version: 0.28.0
96
102
  - !ruby/object:Gem::Dependency
97
- name: deadline_machines
103
+ name: chrono_machines
98
104
  requirement: !ruby/object:Gem::Requirement
99
105
  requirements:
100
106
  - - ">="
101
107
  - !ruby/object:Gem::Version
102
- version: '0.1'
108
+ version: 0.13.0
103
109
  type: :runtime
104
110
  prerelease: false
105
111
  version_requirements: !ruby/object:Gem::Requirement
106
112
  requirements:
107
113
  - - ">="
108
114
  - !ruby/object:Gem::Version
109
- version: '0.1'
110
- - !ruby/object:Gem::Dependency
111
- name: minitest
112
- requirement: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - "~>"
115
- - !ruby/object:Gem::Version
116
- version: '6.0'
117
- type: :development
118
- prerelease: false
119
- version_requirements: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - "~>"
122
- - !ruby/object:Gem::Version
123
- version: '6.0'
115
+ version: 0.13.0
124
116
  - !ruby/object:Gem::Dependency
125
- name: rb_sys
126
- requirement: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - "~>"
129
- - !ruby/object:Gem::Version
130
- version: '0.9'
131
- type: :development
132
- prerelease: false
133
- version_requirements: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - "~>"
136
- - !ruby/object:Gem::Version
137
- version: '0.9'
138
- - !ruby/object:Gem::Dependency
139
- name: rake
140
- requirement: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - "~>"
143
- - !ruby/object:Gem::Version
144
- version: '13.0'
145
- type: :development
146
- prerelease: false
147
- version_requirements: !ruby/object:Gem::Requirement
148
- requirements:
149
- - - "~>"
150
- - !ruby/object:Gem::Version
151
- version: '13.0'
152
- - !ruby/object:Gem::Dependency
153
- name: rake-compiler
117
+ name: deadline_machines
154
118
  requirement: !ruby/object:Gem::Requirement
155
119
  requirements:
156
- - - "~>"
120
+ - - ">="
157
121
  - !ruby/object:Gem::Version
158
- version: '1.3'
159
- type: :development
122
+ version: 0.2.0
123
+ type: :runtime
160
124
  prerelease: false
161
125
  version_requirements: !ruby/object:Gem::Requirement
162
126
  requirements:
163
- - - "~>"
127
+ - - ">="
164
128
  - !ruby/object:Gem::Version
165
- version: '1.3'
166
- description: ThrottleMachines provides ultra-thin, elegant rate limiting with temporal
167
- precision for distributed systems.
129
+ version: 0.2.0
130
+ description: ThrottleMachines limits call rates with a no_std Rust core on Integer-nanosecond
131
+ clocks, over memory, Redis or cache storage, with Rack and Rails integration.
168
132
  email:
169
133
  - terminale@gmail.com
170
134
  executables: []
171
- extensions: []
135
+ extensions:
136
+ - ext/throttle_machines_native/extconf.rb
172
137
  extra_rdoc_files: []
173
138
  files:
139
+ - CHANGELOG.md
140
+ - Cargo.lock
141
+ - Cargo.toml
174
142
  - LICENSE
175
143
  - README.md
144
+ - ext/throttle_machines_native/core/CHANGELOG.md
145
+ - ext/throttle_machines_native/core/Cargo.toml
146
+ - ext/throttle_machines_native/core/README.md
147
+ - ext/throttle_machines_native/core/src/fixed_window.rs
148
+ - ext/throttle_machines_native/core/src/gate.rs
149
+ - ext/throttle_machines_native/core/src/gcra.rs
150
+ - ext/throttle_machines_native/core/src/keyed.rs
151
+ - ext/throttle_machines_native/core/src/lib.rs
152
+ - ext/throttle_machines_native/core/src/refusal.rs
153
+ - ext/throttle_machines_native/core/src/token_bucket.rs
154
+ - ext/throttle_machines_native/extconf.rb
155
+ - ext/throttle_machines_native/ffi/CHANGELOG.md
156
+ - ext/throttle_machines_native/ffi/Cargo.toml
157
+ - ext/throttle_machines_native/ffi/extconf.rb
158
+ - ext/throttle_machines_native/ffi/src/init.rs
159
+ - ext/throttle_machines_native/ffi/src/lib.rs
160
+ - ext/throttle_machines_native/ffi/src/values.rs
176
161
  - lib/throttle_machines.rb
177
162
  - lib/throttle_machines/async_breaker.rb
178
- - lib/throttle_machines/async_limiter.rb
179
163
  - lib/throttle_machines/async_support.rb
164
+ - lib/throttle_machines/breakers.rb
180
165
  - lib/throttle_machines/control.rb
181
166
  - lib/throttle_machines/controller_helpers.rb
182
- - lib/throttle_machines/dependency_error.rb
183
167
  - lib/throttle_machines/engine.rb
184
168
  - lib/throttle_machines/hedged_breaker.rb
185
169
  - lib/throttle_machines/hedged_request.rb
186
170
  - lib/throttle_machines/instrumentation.rb
187
171
  - lib/throttle_machines/limiter.rb
188
172
  - lib/throttle_machines/middleware.rb
189
- - lib/throttle_machines/native_speedup.rb
190
173
  - lib/throttle_machines/rack_middleware.rb
191
174
  - lib/throttle_machines/rack_middleware/allow2_ban.rb
192
175
  - lib/throttle_machines/rack_middleware/ban_filter.rb
@@ -211,13 +194,14 @@ files:
211
194
  - lib/throttle_machines/throttled_error.rb
212
195
  - lib/throttle_machines/unsupported_operation_error.rb
213
196
  - lib/throttle_machines/version.rb
197
+ - sig/throttle_machines.rbs
214
198
  homepage: https://github.com/seuros/throttle_machines
215
199
  licenses:
216
200
  - MIT
217
201
  metadata:
218
202
  homepage_uri: https://github.com/seuros/throttle_machines
219
203
  source_code_uri: https://github.com/seuros/throttle_machines
220
- changelog_uri: https://github.com/seuros/throttle_machines/blob/main/CHANGELOG.md
204
+ changelog_uri: https://github.com/seuros/throttle_machines/blob/master/CHANGELOG.md
221
205
  rubygems_mfa_required: 'true'
222
206
  cargo_crate_name: throttle_machines_native
223
207
  cargo_manifest_path: ext/throttle_machines_native/ffi/Cargo.toml
@@ -237,5 +221,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
221
  requirements: []
238
222
  rubygems_version: 4.0.20
239
223
  specification_version: 4
240
- summary: Advanced Rate limiting for Ruby applications
224
+ summary: Rate limiting (GCRA, token bucket, fixed window) with a native Rust core
241
225
  test_files: []
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ThrottleMachines
4
- # Backward-compatible async-aware limiter class.
5
- #
6
- # Example:
7
- # limiter = ThrottleMachines::AsyncLimiter.new("api",
8
- # limit: 100,
9
- # period: 60,
10
- # algorithm: :gcra
11
- # )
12
- #
13
- # Async do
14
- # if limiter.allowed_async?
15
- # # Currently allowed without consuming capacity
16
- # end
17
- #
18
- # limiter.throttle_async(max_wait: 5) do
19
- # # Consumes capacity through the same atomic path as throttle!
20
- # end
21
- # end
22
- class AsyncLimiter < Limiter
23
- end
24
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ThrottleMachines
4
- # Error raised when dependencies aren't satisfied
5
- class DependencyError < StandardError; end
6
- end
@@ -1,110 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Native speedup for ThrottleMachines using Rust FFI.
4
- #
5
- # This module is automatically loaded when the native extension is available.
6
- # It prepends optimized implementations onto Storage::Memory.
7
- #
8
- # Environment controls:
9
- # DISABLE_MATRYOSHKA_NATIVE=1 - Disable all matryoshka native extensions
10
- # DISABLE_THROTTLE_MACHINES_NATIVE=1 - Disable only throttle_machines native
11
- # DEBUG_MATRYOSHKA=1 - Log which backend is loaded
12
-
13
- return if ENV["DISABLE_MATRYOSHKA_NATIVE"]
14
- return if ENV["DISABLE_THROTTLE_MACHINES_NATIVE"]
15
-
16
- begin
17
- require "throttle_machines_native/throttle_machines_native"
18
-
19
- module ThrottleMachines
20
- module Storage
21
- # Native GCRA implementation using Rust.
22
- module NativeGCRA
23
- def check_gcra_limit(key, emission_interval, delay_tolerance, ttl)
24
- reclaim_capacity
25
- with_write_lock(key) do
26
- now = current_time
27
- state = @gcra_states[key] || { tat: 0.0 }
28
-
29
- # Rust does the math
30
- ThrottleMachinesNative.gcra_check(
31
- state[:tat], now, emission_interval, delay_tolerance
32
- ) => { allowed:, state: new_tat, retry_after: }
33
-
34
- if allowed && !store_entry(@gcra_states, key, { tat: new_tat, expires_at: now + ttl })
35
- return capacity_gcra_refusal(now, delay_tolerance)
36
- end
37
-
38
- { allowed: allowed, retry_after: retry_after, tat: new_tat }
39
- end
40
- end
41
-
42
- def peek_gcra_limit(key, _emission_interval, delay_tolerance)
43
- reclaim_capacity
44
- with_read_lock(key) do
45
- now = current_time
46
- return capacity_gcra_refusal(now, delay_tolerance) unless capacity_available?(@gcra_states, key)
47
-
48
- state = @gcra_states[key] || { tat: 0.0 }
49
-
50
- # Rust does the math
51
- ThrottleMachinesNative.gcra_peek(
52
- state[:tat], now, delay_tolerance
53
- ) => { allowed:, state: tat, retry_after: }
54
-
55
- { allowed: allowed, retry_after: retry_after, tat: tat }
56
- end
57
- end
58
- end
59
-
60
- # Native Token Bucket implementation using Rust.
61
- module NativeTokenBucket
62
- def check_token_bucket(key, capacity, refill_rate, ttl)
63
- reclaim_capacity
64
- with_write_lock(key) do
65
- now = current_time
66
- bucket = @token_buckets[key] || { tokens: capacity, last_refill: now }
67
-
68
- # Rust does the math
69
- ThrottleMachinesNative.token_bucket_check(
70
- bucket[:tokens], bucket[:last_refill], now, capacity, refill_rate
71
- ) => { allowed:, state: new_tokens, retry_after: }
72
-
73
- if allowed
74
- unless store_entry(@token_buckets, key, { tokens: new_tokens, last_refill: now, expires_at: now + ttl })
75
- return capacity_token_bucket_refusal
76
- end
77
- end
78
-
79
- { allowed: allowed, retry_after: retry_after, tokens_remaining: new_tokens.floor }
80
- end
81
- end
82
-
83
- def peek_token_bucket(key, capacity, refill_rate)
84
- reclaim_capacity
85
- with_read_lock(key) do
86
- now = current_time
87
- return capacity_token_bucket_refusal unless capacity_available?(@token_buckets, key)
88
-
89
- bucket = @token_buckets[key] || { tokens: capacity, last_refill: now }
90
-
91
- # Rust does the math
92
- ThrottleMachinesNative.token_bucket_peek(
93
- bucket[:tokens], bucket[:last_refill], now, capacity, refill_rate
94
- ) => { allowed:, state: tokens, retry_after: }
95
-
96
- { allowed: allowed, retry_after: retry_after, tokens_remaining: tokens.floor }
97
- end
98
- end
99
- end
100
- end
101
- end
102
-
103
- # Prepend native implementations
104
- ThrottleMachines::Storage::Memory.prepend(ThrottleMachines::Storage::NativeGCRA)
105
- ThrottleMachines::Storage::Memory.prepend(ThrottleMachines::Storage::NativeTokenBucket)
106
-
107
- warn "[ThrottleMachines] Native speedup loaded" if ENV["DEBUG_MATRYOSHKA"]
108
- rescue LoadError => e
109
- warn "[ThrottleMachines] Native speedup unavailable: #{e.message}" if ENV["DEBUG_MATRYOSHKA"]
110
- end