rlimiter 1.0.4 → 1.0.5

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: 528b2e9c5b67936908454361b5d3bbdce2649ef4de738afd9d14097b274f28b3
4
- data.tar.gz: 5a72ff348d385e692cd5fd967c13e9c8543481ffb61f22288f1a66f3a3a6f46b
3
+ metadata.gz: 86ce52b16110098b2ec69c9913b0557416bee91b46c46aeeb840f151938ce9e2
4
+ data.tar.gz: 5496daf4b76627a21d00d8092fd2d5c6806a23b422c4e9ca3f11431a35b1ac85
5
5
  SHA512:
6
- metadata.gz: 3766e84a11dcb16f8c333355bc26d27fe9407a851558e189892acf9bc1a572aeec4d59693e437e692fde4d37a92d3cdf9ef3a0dbdb1827e7df6d700354df2c31
7
- data.tar.gz: 2767cd4c071defd0b2c31087edb81e29216b755319e401159b991355a7b61d6328efbe456fde725ca791e40ff6592e2b66b8ffc7e9567b9f0ffc82fec93907fa
6
+ metadata.gz: 2ce84183c112ab6d55b974cf66faf7b67956e9aaec68c2f4b5448fe8af03de91075e48539d3b7202c94bbfb83bc452858a62d3b3ebb2bda643e7becd7fdbc6b1
7
+ data.tar.gz: 6274a2d372b45c8b064723858c24bf0b99d36e02c62be590ad0245914dd98169adad8fbd1bbed1981a0b05c85daf624c3a070a7a1a5c52c64359d26862b419ef
data/.coveralls.yml CHANGED
@@ -1 +1 @@
1
- repo_token: LhuzAphV1s78Ypdbn9PRsKMDlCpcxMLGk
1
+ repo_token: $COVERALLS_REPO_TOKEN
data/lib/rlimiter.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  require 'rlimiter/version'
2
2
 
3
- # It is important that the files are loaded in the order specified below because of inheritance dependencies.
4
- # TODO: Figure out a better way to load
5
- require_files = %w[/rlimiter/client.rb /rlimiter/invalid_client_error.rb /rlimiter/version.rb /rlimiter/redis_client.rb]
6
- require_files.each do |file|
3
+ # It is important that the files are loaded in the order specified below because of
4
+ # inheritance dependencies.
5
+ %w[/rlimiter/client.rb /rlimiter/invalid_client_error.rb /rlimiter/version.rb /rlimiter/redis_client.rb].each do |file|
7
6
  Dir.glob(File.dirname(File.absolute_path(__FILE__)) + file, &method(:require))
8
7
  end
9
8
 
@@ -15,8 +14,10 @@ module Rlimiter
15
14
  CLIENTS = %w[redis].freeze
16
15
  attr_reader :client
17
16
 
18
- # One time initializes the client which is to be used throughout the application.
19
- # The value of params variable will change depending on the storage client to be initialized.
17
+ # One time initializes the client which is to be used throughout the
18
+ # application. The value of params variable will change depending on the
19
+ # storage client to be initialized.
20
+ #
20
21
  # @param [Hash] params
21
22
  # @return [Rlimiter::Client]
22
23
  def init(params)
@@ -28,8 +29,9 @@ module Rlimiter
28
29
  end
29
30
  end
30
31
 
31
- # Note : Params for the below methods are kept arbitrary for free implementation and are specific to a single client,
32
- # after usage feedback it will be refactored to pertain to a single signature.
32
+ # Note : Params for the below methods are kept arbitrary for free
33
+ # implementation and are specific to a single client, after usage feedback
34
+ # it will be refactored to pertain to a single signature.
33
35
 
34
36
  # Register a hit to the client.
35
37
  def limit(*params)
@@ -14,5 +14,9 @@ module Rlimiter
14
14
  # Stub
15
15
  end
16
16
 
17
+ def clear(key)
18
+ # Stub
19
+ end
20
+
17
21
  end
18
22
  end
@@ -1,13 +1,14 @@
1
1
  require 'redis'
2
2
 
3
3
  module Rlimiter
4
-
5
4
  # Redis concrete class of abstract Client
6
- # Maintains two redis keys, one for number of hits and other for the start of the time window.
7
- # Increases the hit count every time :limit is called, if hit count exceeds the limit count then it is checked whether
8
- # if the previous time window is active or not, on the basis of which true/false is returned.
5
+ # Maintains two redis keys, one for number of hits and other for the start of
6
+ # the time window.
7
+ # Increases the hit count every time :limit is called, if hit count exceeds
8
+ # the limit count then it is checked whether
9
+ # if the previous time window is active or not, on the basis of which
10
+ # true/false is returned.
9
11
  class RedisClient < Client
10
-
11
12
  # Name of key of the hit count number, stores an integer.
12
13
  RATE_COUNT = 'rate_count'.freeze
13
14
 
@@ -33,15 +34,19 @@ module Rlimiter
33
34
  @redis = Redis.new(params)
34
35
  end
35
36
 
36
- # Registers a hit corresponding to the key specified, requires the max hit count and the duration to be passed.
37
+ # Registers a hit corresponding to the key specified, requires the max hit
38
+ # count and the duration to be passed.
37
39
  #
38
- # @param [String] key : Should be unique for one operation, can be added for multiple operations if a single rate
40
+ # @param [String] key : Should be unique for one operation, can be added for
41
+ # multiple operations if a single rate
39
42
  # limiter is to be used for those operations.
40
43
  # @param [Integer] count : Max rate limit count
41
44
  # @param [Integer] duration : Duration of the time window.
42
45
  #
43
- # Count and duration params could change in each call and the limit breach value is returned corresponding to that.
44
- # Ideally this method should be called with each param a constant on the application level.
46
+ # Count and duration params could change in each call and the limit breach
47
+ # value is returned corresponding to that.
48
+ # Ideally this method should be called with each param a constant on the
49
+ # application level.
45
50
  #
46
51
  # Returns false if the limit has been breached.
47
52
  # Returns true if limit has not been breached. (duh)
@@ -53,7 +58,8 @@ module Rlimiter
53
58
  if incr_count > count
54
59
 
55
60
  # :elapsed is the time window start Redis cache
56
- # If the time elapsed is less than window duration, the limit has been breached for the current window (return false).
61
+ # If the time elapsed is less than window duration, the limit has been
62
+ # breached for the current window (return false).
57
63
  return false if @duration - elapsed > 0
58
64
 
59
65
  # Else reset the hit count to zero and window start time.
@@ -77,9 +83,18 @@ module Rlimiter
77
83
  @key = key
78
84
  @duration = duration
79
85
  return 0 if current_count(key) < count
86
+
80
87
  [@duration - elapsed, 0].max
81
88
  end
82
89
 
90
+ # Clear the key from the data store.
91
+ # @param [String] key
92
+ # @return [TrueClass|FalseClass] depending on whether key has been deleted
93
+ # successfully.
94
+ def clear(key)
95
+ @redis.del(key) == 1
96
+ end
97
+
83
98
  private
84
99
 
85
100
  def reset
@@ -1,5 +1,5 @@
1
1
  module Rlimiter
2
2
  #:nocov:
3
- VERSION = '1.0.4'.freeze
3
+ VERSION = '1.0.5'.freeze
4
4
  #:nocov:
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rlimiter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sidharth Patro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-06 00:00:00.000000000 Z
11
+ date: 2019-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project:
121
- rubygems_version: 2.7.7
121
+ rubygems_version: 2.7.8
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: Rate limiting library for Ruby