leaky_bucket 0.2.5 → 0.2.6
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 +5 -5
- data/lib/leaky_bucket/throttler.rb +7 -3
- data/lib/leaky_bucket/version.rb +1 -1
- metadata +7 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: eeb963d24d10ae96cc014d86836affef0df906e147ad446090d3544e56b6b267
|
|
4
|
+
data.tar.gz: 94142251a3a5d8221a7fcbc95ff6f32e8fa2bc83e6b45a3f184beff973327f46
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c80b6414dec5d9ad124147bfa4e9e4c0ed7ea1595f8eff62c634adf0ac5c284bafb3366f8c392217d8cd74e3a1b5007ef085a5e873fd99c789e85b178c0c926a
|
|
7
|
+
data.tar.gz: f0992ad95ce62c9a5d393ca166f156320870f5724f5dc2e497ec23f2273d6200779a1ae3de53a88daa744f34c5cf153f67d5496a2c912db731fe4dfaf7836ba9
|
|
@@ -20,7 +20,11 @@ module LeakyBucket
|
|
|
20
20
|
bucket = leak(bucket, threshold, interval)
|
|
21
21
|
|
|
22
22
|
if bucket[:current_load] > burst
|
|
23
|
-
|
|
23
|
+
leak_rate = interval.to_f / threshold
|
|
24
|
+
time_elapsed = Time.now.to_i - bucket[:last_request_made_at]
|
|
25
|
+
requests_to_leak = bucket[:current_load] - burst
|
|
26
|
+
retry_in = [(requests_to_leak * leak_rate - (time_elapsed % leak_rate)).ceil, 1].max
|
|
27
|
+
raise LeakyBucket::TooManyRequests.new(retry_in)
|
|
24
28
|
end
|
|
25
29
|
|
|
26
30
|
increment(ip, bucket)
|
|
@@ -31,8 +35,8 @@ module LeakyBucket
|
|
|
31
35
|
def leak(bucket, threshold, interval)
|
|
32
36
|
now = Time.now.to_i
|
|
33
37
|
#the bucket leaks 100 requests/hour by default ~= 1.6 request/minute
|
|
34
|
-
leak_rate = interval / threshold
|
|
35
|
-
leak_amount = (now - bucket[:last_request_made_at]) / leak_rate
|
|
38
|
+
leak_rate = interval.to_f / threshold
|
|
39
|
+
leak_amount = ((now - bucket[:last_request_made_at]) / leak_rate).to_i
|
|
36
40
|
#decrement the bucket load, or empty it if it's been a long enough time for it to leak everything
|
|
37
41
|
bucket[:current_load] = [bucket[:current_load] - leak_amount, 0].max
|
|
38
42
|
bucket
|
data/lib/leaky_bucket/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: leaky_bucket
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ahmad
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-03-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -66,7 +66,7 @@ dependencies:
|
|
|
66
66
|
- - ">="
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
68
|
version: '0'
|
|
69
|
-
description:
|
|
69
|
+
description:
|
|
70
70
|
email:
|
|
71
71
|
- ahmad.kaate@gmail.com
|
|
72
72
|
executables: []
|
|
@@ -92,7 +92,7 @@ homepage: https://github.com/Hamada92/leaky_bucket
|
|
|
92
92
|
licenses:
|
|
93
93
|
- MIT
|
|
94
94
|
metadata: {}
|
|
95
|
-
post_install_message:
|
|
95
|
+
post_install_message:
|
|
96
96
|
rdoc_options: []
|
|
97
97
|
require_paths:
|
|
98
98
|
- lib
|
|
@@ -107,9 +107,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
107
107
|
- !ruby/object:Gem::Version
|
|
108
108
|
version: '0'
|
|
109
109
|
requirements: []
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
signing_key:
|
|
110
|
+
rubygems_version: 3.5.23
|
|
111
|
+
signing_key:
|
|
113
112
|
specification_version: 4
|
|
114
113
|
summary: limit the number of requests made to your api using the solid leaky bucket
|
|
115
114
|
algorithm
|