rapidity 0.0.1 → 0.0.4.64534

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 567c4170351523c2e8850ea4980d0a5099c4bab64bddc1bd60d789aa1eb9d2be
4
- data.tar.gz: f148f201500e64d68fe23bf101c7ed21c42d8bc3486c1ee783b97e7117d24711
3
+ metadata.gz: af052ec73bab58f49e7004d63d9dea9ab27247df2bb012d874c30a1c387a9d3f
4
+ data.tar.gz: a5ff8b93f6ad5f12656a67ea9e938aea0ab4f06494ecab38bd5da234829eb2b4
5
5
  SHA512:
6
- metadata.gz: 9ccac974c7d5bfd96b49a61c98f7db8a731667e97cb614c8a34d465173540f9b8f05e1dea8d6797ee4c4f8112b38471d28b8d92008a9722da9b1080a947b7d8c
7
- data.tar.gz: a0ec68f61eb62c09be61637629d56d628b66af4b03ca0eb31351d9434b24bbb6233314b7e1864c3d28acf04b4827a5bdbb1d25d2f00b6593381e6cc00307f177
6
+ metadata.gz: ba8b36082e31aadc9e083d6974da24d42ecf91253cf96a9fab6a31ca1d57665662e53bca693754edf90adc6f581314f831173a9a365d90352a1d6e7a6ebf4d1c
7
+ data.tar.gz: 7144440be367435bb07ca46820f4e6f2256772c0ffb3c448f49376a868156a88cff4fd9add2d734d6b156944c5fe25e97cf69c20fbce3adb2612b94b72609150
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rapidity (0.0.1)
4
+ rapidity (0.0.4.64534)
5
5
  activesupport
6
6
  connection_pool
7
7
  redis
@@ -58,7 +58,7 @@ GEM
58
58
  psych (3.3.2)
59
59
  public_suffix (4.0.6)
60
60
  rainbow (3.0.0)
61
- redis (4.3.1)
61
+ redis (4.5.0)
62
62
  reek (6.0.4)
63
63
  kwalify (~> 0.7.0)
64
64
  parser (~> 3.0.0)
@@ -123,6 +123,7 @@ GEM
123
123
  zeitwerk (2.4.2)
124
124
 
125
125
  PLATFORMS
126
+ ruby
126
127
  x86_64-linux
127
128
 
128
129
  DEPENDENCIES
@@ -141,4 +142,4 @@ DEPENDENCIES
141
142
  timeouter
142
143
 
143
144
  BUNDLED WITH
144
- 2.2.13
145
+ 2.2.27
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014-2021 Рнд Софт
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -1,4 +1,3 @@
1
-
2
1
  require 'ostruct'
3
2
 
4
3
  require_relative './limiter'
@@ -9,30 +8,30 @@ module Rapidity
9
8
 
10
9
  attr_reader :limits, :limiters, :name, :namespace
11
10
 
12
- # Combine multiple limits
11
+ # Combine multiple limits
13
12
  # @params pool - inititalized Redis pool
14
13
  # @params name - limiter name - part of the Redis key name
15
14
  # @params limits - multiple limits definition
16
15
 
17
16
  ## limits example
18
17
  # ```ruby`
19
- #limits = [
18
+ # limits = [
20
19
  # { threshold: 2, interval: 1 }, # 2 events per second
21
20
  # { threshold: 9, interval: 5 }, # 9 events per 5 seconds
22
21
  # { threshold: 20, interval: 20 }, # 20 events per 20 seconds
23
22
  # { threshold: 42, interval: 60 }, # 42 events per 60 seconds
24
- #]
25
- #```
26
-
23
+ # ]
24
+ # ```
27
25
  # @params namespace - namespace for Redis keys
28
- def initialize pool, name:, limits: [], namespace: 'rapidity'
26
+ def initialize(pool, name:, limits: [], namespace: 'rapidity')
29
27
  @limits = limits
30
28
  @name = name
31
29
  @namespace = namespace
32
-
30
+
33
31
  @limiters = @limits.map.each_with_index do |l, i|
34
32
  limit = OpenStruct.new(l)
35
- ::Rapidity::Limiter.new(pool, name: "#{i}_#{name}_#{limit.limit}/#{limit.interval}", interval: limit.interval, threshold: limit.threshold, namespace: namespace)
33
+ ::Rapidity::Limiter.new(pool, name: "#{i}_#{name}_#{limit.threshold}/#{limit.interval}",
34
+ interval: limit.interval, threshold: limit.threshold, namespace: namespace)
36
35
  end
37
36
  end
38
37
 
@@ -48,8 +47,9 @@ module Rapidity
48
47
  break if count == 0
49
48
  end
50
49
 
51
- return count
50
+ count
52
51
  end
53
52
 
54
53
  end
55
- end
54
+ end
55
+
@@ -1,4 +1,3 @@
1
-
2
1
  require 'connection_pool'
3
2
  require 'redis'
4
3
 
@@ -14,12 +13,12 @@ module Rapidity
14
13
  # @params interval - interval in seconds to apply this limit
15
14
  # @params threshold - maximum available events for this interval
16
15
  # @params namespace - namespace for Redis keys
17
- def initialize pool, name:, interval: 10, threshold: 10, namespace: 'rapidity'
16
+ def initialize(pool, name:, interval: 10, threshold: 10, namespace: 'rapidity')
18
17
  @pool = pool
19
18
  @interval = interval
20
19
  @threshold = threshold
21
20
  @name = name
22
-
21
+
23
22
  @namespace = namespace
24
23
  end
25
24
 
@@ -30,43 +29,55 @@ module Rapidity
30
29
  # Get current counter
31
30
  # @return remaining counter value
32
31
  def remains
33
- _, result = @pool.with do |conn|
32
+ results = @pool.with do |conn|
34
33
  conn.multi do
35
34
  conn.set(key('remains'), threshold, ex: interval, nx: true)
36
35
  conn.get(key('remains'))
37
36
  end
38
37
  end
39
- result.to_i
38
+ results[1].to_i #=> conn.get(key('remains'))
40
39
  end
41
40
 
42
41
  # Obtain values from counter
43
42
  # @return count succesfuly obtained send slots
44
43
  def obtain(count = 5)
45
- _, taken = @pool.with do |conn|
44
+ count = count.abs
45
+
46
+ results = @pool.with do |conn|
46
47
  conn.multi do
47
48
  conn.set(key('remains'), threshold, ex: interval, nx: true)
48
49
  conn.decrby(key('remains'), count)
49
50
  end
50
51
  end
51
52
 
52
- obtained = if taken < 0
53
+ taken = results[1].to_i #=> conn.decrby(key('remains'), count)
54
+
55
+ if taken < 0
53
56
  overflow = taken.abs
54
57
  to_return = [count, overflow].min
55
58
 
56
- @pool.with do |conn|
59
+ results = @pool.with do |conn|
57
60
  conn.multi do
58
61
  conn.set(key('remains'), threshold - to_return, ex: interval, nx: true)
59
62
  conn.incrby(key('remains'), to_return)
63
+ conn.ttl(key('remains'))
60
64
  end
61
65
  end
62
66
 
63
- count - overflow
67
+ ttl = results[2].to_i #=> conn.ttl(key('remains'))
68
+
69
+ # reset if no ttl present
70
+ if ttl == -1
71
+ STDERR.puts "ERROR[#{Time.now}]: TTL for key #{key('remains').inspect} disappeared!"
72
+ @pool.with {|c| c.expire(key('remains'), interval) }
73
+ end
74
+
75
+ count - to_return
64
76
  else
65
77
  count
66
78
  end
67
-
68
- return obtained
69
79
  end
70
80
 
71
81
  end
72
- end
82
+ end
83
+
@@ -1,3 +1,6 @@
1
1
  module Rapidity
2
- VERSION='0.0.1'
3
- end
2
+
3
+ VERSION = '0.0.4'.freeze
4
+
5
+ end
6
+
data/lib/rapidity.rb CHANGED
@@ -5,7 +5,7 @@ require_relative 'rapidity/composer'
5
5
 
6
6
  module Rapidity
7
7
 
8
-
8
+
9
9
 
10
10
  end
11
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapidity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4.64534
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yurusov Vlad
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-08-03 00:00:00.000000000 Z
12
+ date: 2021-10-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -26,7 +26,7 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
- name: redis
29
+ name: connection_pool
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - ">="
@@ -40,7 +40,7 @@ dependencies:
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
- name: connection_pool
43
+ name: redis
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ">="
@@ -54,47 +54,47 @@ dependencies:
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
- name: timeouter
57
+ name: bundler
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ">="
60
+ - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '0'
62
+ version: '2.0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ">="
67
+ - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '0'
69
+ version: '2.0'
70
70
  - !ruby/object:Gem::Dependency
71
- name: bundler
71
+ name: rspec
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - "~>"
75
75
  - !ruby/object:Gem::Version
76
- version: '2.0'
76
+ version: '3.0'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
- version: '2.0'
83
+ version: '3.0'
84
84
  - !ruby/object:Gem::Dependency
85
- name: rspec
85
+ name: timeouter
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - "~>"
88
+ - - ">="
89
89
  - !ruby/object:Gem::Version
90
- version: '3.0'
90
+ version: '0'
91
91
  type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - "~>"
95
+ - - ">="
96
96
  - !ruby/object:Gem::Version
97
- version: '3.0'
97
+ version: '0'
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: awesome_print
100
100
  requirement: !ruby/object:Gem::Requirement
@@ -231,6 +231,7 @@ extra_rdoc_files: []
231
231
  files:
232
232
  - Gemfile
233
233
  - Gemfile.lock
234
+ - LICENSE
234
235
  - lib/rapidity.rb
235
236
  - lib/rapidity/composer.rb
236
237
  - lib/rapidity/limiter.rb
@@ -246,14 +247,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
246
247
  requirements:
247
248
  - - ">="
248
249
  - !ruby/object:Gem::Version
249
- version: '0'
250
+ version: 2.5.0
250
251
  required_rubygems_version: !ruby/object:Gem::Requirement
251
252
  requirements:
252
253
  - - ">="
253
254
  - !ruby/object:Gem::Version
254
255
  version: '0'
255
256
  requirements: []
256
- rubygems_version: 3.2.13
257
+ rubygems_version: 3.0.3
257
258
  signing_key:
258
259
  specification_version: 4
259
260
  summary: Simple distributed Redis-backed rate limiter