ruby-limiter 2.1.0 → 2.2.2

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: 144e27e5e203e66c7d753c3da5bce267a514de2f32be74849e96104d6450efe6
4
- data.tar.gz: 6ec690da1476b519f6cbb6b22ba921b2c8006db09a706553304c0077a692e553
3
+ metadata.gz: 8414ef377bee89d172c77d547a9cc0468bbfbf5a4a2b775ffa8faa137fdbed5b
4
+ data.tar.gz: 9065acd27f48647174b0cce72bb919869addb4dca0e53e8b5e5d446dfaff53d0
5
5
  SHA512:
6
- metadata.gz: e4568e49199cc0a518439c808bc28e3281bdc44994495a0bee5e379c40c0de29bf45deee1e55d16d4f80df86424f26411c2f8b0fe13af605d18f8eb4b08c61db
7
- data.tar.gz: 385abafc6d5e638829c0cd278a311b1d997a5c2b41e1010ae4b46cac5772498906f5254ab44777d353f36ef7ca228ce134ee2d6b91ceae4310384c4d9d5809e7
6
+ metadata.gz: 58dbd481ed08a6ed0ab24664c94488acb2206590de98f6cc06b4770acd797d1744a39d51a362893b7162381edc6da005ee7ec247cdec1fab4f7d9d4686da8355
7
+ data.tar.gz: 90c28e01feb7964814da473d1bddbaca86a0167f9d580aa9ac3281fafff73bca8c8e7dbda4147a56fc53ad56b8f424f523c881294e888f810760ad6c47f44624
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v2.2.2
4
+
5
+ - security update to rake 13.0.6 [CVE-2020-8130]
6
+
7
+ ## v2.2.0
8
+
9
+ - adds support for "balancing" requests over time
10
+
3
11
  ## v2.1.0
4
12
 
5
13
  - add support to call a block when limiting takes place
data/README.md CHANGED
@@ -55,6 +55,27 @@ class Widget
55
55
  end
56
56
  ```
57
57
 
58
+ #### Load balancing
59
+
60
+ By default all calls to the `limit_method` will be bursted, e.g. as quick as possible, until the rate is exceeded.
61
+ Then we wait for the remainder of the interval to continue. To even out the burst, an optional `balanced` parameter can be
62
+ provided to enable interleaving between the method calls, e.g: `interleave = interval / size`.
63
+
64
+ ``` ruby
65
+ ...
66
+ limit_method :tick, rate: 60, balanced: true
67
+ ...
68
+ ```
69
+
70
+ For example: with an interval of 60 seconds and a rate of 60:
71
+
72
+ `balanced: false`
73
+ : As quickly as possible we call the method 60 times, then we wait for the remainder of the time.
74
+
75
+ `balanced: true`
76
+ : We interleave each call with 1 second so we call this method every second.
77
+
78
+
58
79
  ### Advanced Usage
59
80
 
60
81
  In cases where the mixin is not appropriate the `RateQueue` class can be used directly. As in the mixin examples above,
data/lib/limiter/mixin.rb CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Limiter
4
4
  module Mixin
5
- def limit_method(method, rate:, interval: 60, &b)
6
- queue = RateQueue.new(rate, interval: interval, &b)
5
+ def limit_method(method, rate:, interval: 60, balanced: false, &b)
6
+ queue = RateQueue.new(rate, interval: interval, balanced: balanced, &b)
7
7
 
8
8
  mixin = Module.new do
9
9
  define_method(method) do |*args, **options, &blk|
@@ -4,11 +4,11 @@ module Limiter
4
4
  class RateQueue
5
5
  EPOCH = 0.0
6
6
 
7
- def initialize(size, interval: 60, &blk)
7
+ def initialize(size, interval: 60, balanced: false, &blk)
8
8
  @size = size
9
9
  @interval = interval
10
10
 
11
- @ring = Array.new(size, EPOCH)
11
+ @ring = balanced ? balanced_ring : unbalanced_ring
12
12
  @head = 0
13
13
  @mutex = Mutex.new
14
14
  @blk = blk
@@ -41,5 +41,21 @@ module Limiter
41
41
  def clock
42
42
  Clock
43
43
  end
44
+
45
+ def unbalanced_ring
46
+ Array.new(@size, EPOCH)
47
+ end
48
+
49
+ def balanced_ring
50
+ (0...@size).map { |i| base_time + (gap * i) }
51
+ end
52
+
53
+ def gap
54
+ @interval.to_f / @size.to_f
55
+ end
56
+
57
+ def base_time
58
+ clock.time - @interval
59
+ end
44
60
  end
45
61
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limiter
4
- VERSION = '2.1.0'
4
+ VERSION = '2.2.2'
5
5
  end
data/limiter.gemspec CHANGED
@@ -32,6 +32,6 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency 'minitest', '~> 5.0'
33
33
  spec.add_development_dependency 'minitest-focus', '~> 1.3'
34
34
  spec.add_development_dependency 'mocha', '~> 1.11'
35
- spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rake', '~> 13.0'
36
36
  spec.add_development_dependency 'rubocop', '~> 0.56'
37
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-limiter
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - S. Brent Faulkner
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-30 00:00:00.000000000 Z
11
+ date: 2022-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: '13.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: '13.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rubocop
85
85
  requirement: !ruby/object:Gem::Requirement