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 +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +21 -0
- data/lib/limiter/mixin.rb +2 -2
- data/lib/limiter/rate_queue.rb +18 -2
- data/lib/limiter/version.rb +1 -1
- data/limiter.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8414ef377bee89d172c77d547a9cc0468bbfbf5a4a2b775ffa8faa137fdbed5b
|
4
|
+
data.tar.gz: 9065acd27f48647174b0cce72bb919869addb4dca0e53e8b5e5d446dfaff53d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58dbd481ed08a6ed0ab24664c94488acb2206590de98f6cc06b4770acd797d1744a39d51a362893b7162381edc6da005ee7ec247cdec1fab4f7d9d4686da8355
|
7
|
+
data.tar.gz: 90c28e01feb7964814da473d1bddbaca86a0167f9d580aa9ac3281fafff73bca8c8e7dbda4147a56fc53ad56b8f424f523c881294e888f810760ad6c47f44624
|
data/CHANGELOG.md
CHANGED
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|
|
data/lib/limiter/rate_queue.rb
CHANGED
@@ -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 =
|
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
|
data/lib/limiter/version.rb
CHANGED
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', '~>
|
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.
|
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:
|
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: '
|
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: '
|
82
|
+
version: '13.0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rubocop
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|