ruby_rate_limiter 2.0.6 → 2.0.8
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/lib/ruby_rate_limiter/token_bucket.rb +15 -8
- data/lib/ruby_rate_limiter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f812a1ae8b8449217f489de3234d4f26a077bdc4f8bd6653dbe29457c363ab23
|
4
|
+
data.tar.gz: f2941667395af0a013e11c87394ab355ed5c01ec789fdc294e5eb84ef4ac4c3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebd5d104e4172e0b32856dc26b815b04d0a49b89b35e7a9c8520ad474ab00e2dfb5c2771078a579e8c9075b49ed84a75c2b98e5e5ad672ec4b750ef576ea1eaf
|
7
|
+
data.tar.gz: e402b83a46be87121443283dc178a8eabed68056c44a23423f2c008d0bc5f9c7d8f5a870fb1c00ef629f5ad006501c42267fbb4ae01532030f434409662b8d1c
|
@@ -47,9 +47,10 @@ module RubyRateLimiter
|
|
47
47
|
private
|
48
48
|
|
49
49
|
def initialize_bucket
|
50
|
-
if
|
50
|
+
if @storage.get("#{@user_id}_tokens").nil?
|
51
51
|
update_bucket_size(@bucket_size)
|
52
52
|
update_last_refill_time(Time.now.to_f)
|
53
|
+
puts "Initialized bucket: tokens = #{@bucket_size}, time = #{Time.now.to_f}"
|
53
54
|
end
|
54
55
|
end
|
55
56
|
|
@@ -77,15 +78,21 @@ module RubyRateLimiter
|
|
77
78
|
current_time = Time.now.to_f
|
78
79
|
last_refill_time = get_last_refill_time
|
79
80
|
elapsed_time = current_time - last_refill_time
|
81
|
+
|
82
|
+
if elapsed_time < 0
|
83
|
+
puts "Warning: elapsed_time is negative. Adjusting to zero."
|
84
|
+
elapsed_time = 0
|
85
|
+
end
|
86
|
+
|
80
87
|
new_tokens = (elapsed_time * @refill_rate_per_second).to_i
|
81
88
|
puts "Refill tokens: current_time = #{current_time}, last_refill_time = #{last_refill_time}, elapsed_time = #{elapsed_time}, new_tokens = #{new_tokens}" # Debugging line
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
+
|
90
|
+
if new_tokens > 0
|
91
|
+
tokens = [get_bucket_size + new_tokens, @bucket_size].min
|
92
|
+
update_bucket_size(tokens)
|
93
|
+
update_last_refill_time(current_time)
|
94
|
+
puts "Refill tokens: updated tokens = #{tokens}, current_time = #{current_time}" # Debugging line
|
95
|
+
end
|
89
96
|
end
|
90
97
|
end
|
91
98
|
end
|