ruby_rate_limiter 2.0.0 → 2.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b56de2a59f050c7b8a8096f2d2557b5282dcda5d85364193f6c199d4bbee7720
4
- data.tar.gz: 66b292d959e2b2436080d726c84b218dbf6a3a53eabd78bafd4b2951ad0d04cb
3
+ metadata.gz: 2462f7011d88430891bdc353e44d9a7fdb83077c6b812ccb1403951731393a5e
4
+ data.tar.gz: a27cf78a19b55a6b44dbe46519ee409d843b08b0a57119164f5ef5158e8c19bd
5
5
  SHA512:
6
- metadata.gz: 1b375751848ee9148b9b4e17ad1f7438eabe637ce7a18b4db17975110f36a9a7999566c4be3a9389833bfce3ecae59f68b36ecfc8cf5e8671bbd1847e9b23367
7
- data.tar.gz: e99dfa9eb9381db0fb16405feb2af5c99d16383dadd8f70b11456d924d948263227042513376d5b6a6977b8308ff849d4a5874743ea838847c4914e50e5f1c99
6
+ metadata.gz: 1f5ff9ff0ac134525513565276dbcd30c617772f969dd8b99e864f609c965b22557616ea8f75c004e2e9696f8bdc230dd9e00a5f60ffb21d972e639dce57327f
7
+ data.tar.gz: c66242a80c84285c31f3025f47ecfa5dff10121a1507543e3c6ef41cfd21509cfdceb8689aa93602e10bccaae868f49cfb2aa44f7febbd13d7e455331f311b82
@@ -9,6 +9,7 @@ module RubyRateLimiter
9
9
 
10
10
  DEFAULT_BUCKET_SIZE = 10
11
11
  DEFAULT_REFILL_RATE = 1
12
+ DEFAULT_STORAGE = RubyRateLimiter::Storage::RedisStorage.new
12
13
  TIME_UNITS = {
13
14
  second: 1,
14
15
  minute: 60,
@@ -17,15 +18,21 @@ module RubyRateLimiter
17
18
 
18
19
  def_delegators :@storage, :get, :set
19
20
 
20
- def initialize(user_id, bucket_size: DEFAULT_BUCKET_SIZE, refill_rate: DEFAULT_REFILL_RATE, time_unit: :second, storage: RubyRateLimiter::Storage::RedisStorage.new)
21
+ def initialize(
22
+ user_id,
23
+ bucket_size: DEFAULT_BUCKET_SIZE,
24
+ refill_rate: DEFAULT_REFILL_RATE,
25
+ time_unit: :second,
26
+ storage: DEFAULT_STORAGE
27
+ )
21
28
  @user_id = user_id
22
29
  @bucket_size = bucket_size
23
- @refill_rate = refill_rate * TIME_UNITS[time_unit]
30
+ @refill_rate_per_second = refill_rate.to_f / TIME_UNITS[time_unit]
24
31
  @storage = storage
25
32
  end
26
33
 
27
34
  def allow_request?
28
- refill_tokens
35
+ refill_tokens
29
36
  tokens = get_bucket_size
30
37
  return false if tokens < 1
31
38
 
@@ -56,7 +63,7 @@ module RubyRateLimiter
56
63
  last_refill_time = get_last_refill_time
57
64
  elapsed_time = current_time - last_refill_time
58
65
 
59
- new_tokens = (elapsed_time * @refill_rate).to_i
66
+ new_tokens = (elapsed_time * @refill_rate_per_second).to_i
60
67
  return if new_tokens <= 0
61
68
 
62
69
  tokens = [get_bucket_size + new_tokens, @bucket_size].min
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RateLimiter
4
- VERSION = "2.0.0"
4
+ VERSION = "2.0.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_rate_limiter
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mutuba