prop 2.2.5 → 2.3.0
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 +4 -4
- data/lib/prop.rb +1 -1
- data/lib/prop/limiter.rb +20 -20
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db081ee0a96819c4b270c57e0205527b122d93b214b2c42af7d50159cfb140d1
|
4
|
+
data.tar.gz: f8ef4abcc3cb6762b70a993fed0446321585a14e685cbb91a1e37f355fe507e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ff690ae0b93ebe4f15d05bb5e8b2b7f5f983607a56a9c9d1ad04c19a9de1aaa21b38b70d5763b091b5db6cd10c9890b3eb33bd5ca910b055f682eb0b5b23b07
|
7
|
+
data.tar.gz: 268563b7482c713097888855260875a19777cdd20982496bec4644dfd1b494dde6666d09b10789617fd8d905d9a18b98265dfebcf1609b20734c46ee56e1a24b
|
data/lib/prop.rb
CHANGED
data/lib/prop/limiter.rb
CHANGED
@@ -72,8 +72,8 @@ module Prop
|
|
72
72
|
#
|
73
73
|
# Returns true if the threshold for this handle has been reached, else returns false
|
74
74
|
def throttle(handle, key = nil, options = {})
|
75
|
-
options, cache_key = prepare(handle, key, options)
|
76
|
-
throttled = _throttle(handle, key, cache_key, options).first
|
75
|
+
options, cache_key, strategy = prepare(handle, key, options)
|
76
|
+
throttled = _throttle(strategy, handle, key, cache_key, options).first
|
77
77
|
block_given? && !throttled ? yield : throttled
|
78
78
|
end
|
79
79
|
|
@@ -87,8 +87,8 @@ module Prop
|
|
87
87
|
# Raises Prop::RateLimited if the threshold for this handle has been reached
|
88
88
|
# Returns the value of the block if given a such, otherwise the current count of the throttle
|
89
89
|
def throttle!(handle, key = nil, options = {}, &block)
|
90
|
-
options, cache_key = prepare(handle, key, options)
|
91
|
-
throttled, counter = _throttle(handle, key, cache_key, options)
|
90
|
+
options, cache_key, strategy = prepare(handle, key, options)
|
91
|
+
throttled, counter = _throttle(strategy, handle, key, cache_key, options)
|
92
92
|
|
93
93
|
if throttled
|
94
94
|
raise Prop::RateLimited.new(options.merge(
|
@@ -108,9 +108,9 @@ module Prop
|
|
108
108
|
#
|
109
109
|
# Returns true if a call to `throttle!` with same parameters would raise, otherwise false
|
110
110
|
def throttled?(handle, key = nil, options = {})
|
111
|
-
options, cache_key = prepare(handle, key, options)
|
112
|
-
counter =
|
113
|
-
|
111
|
+
options, cache_key, strategy = prepare(handle, key, options)
|
112
|
+
counter = strategy.counter(cache_key, options)
|
113
|
+
strategy.compare_threshold?(counter, :>=, options)
|
114
114
|
end
|
115
115
|
|
116
116
|
# Public: Resets a specific throttle
|
@@ -120,8 +120,8 @@ module Prop
|
|
120
120
|
#
|
121
121
|
# Returns nothing
|
122
122
|
def reset(handle, key = nil, options = {})
|
123
|
-
_options, cache_key = prepare(handle, key, options)
|
124
|
-
|
123
|
+
_options, cache_key, strategy = prepare(handle, key, options)
|
124
|
+
strategy.reset(cache_key)
|
125
125
|
end
|
126
126
|
|
127
127
|
# Public: Counts the number of times the given handle/key combination has been hit in the current window
|
@@ -131,8 +131,8 @@ module Prop
|
|
131
131
|
#
|
132
132
|
# Returns a count of hits in the current window
|
133
133
|
def count(handle, key = nil, options = {})
|
134
|
-
options, cache_key = prepare(handle, key, options)
|
135
|
-
|
134
|
+
options, cache_key, strategy = prepare(handle, key, options)
|
135
|
+
strategy.counter(cache_key, options)
|
136
136
|
end
|
137
137
|
alias :query :count
|
138
138
|
|
@@ -143,18 +143,18 @@ module Prop
|
|
143
143
|
|
144
144
|
private
|
145
145
|
|
146
|
-
def _throttle(handle, key, cache_key, options)
|
147
|
-
return [false,
|
146
|
+
def _throttle(strategy, handle, key, cache_key, options)
|
147
|
+
return [false, strategy.zero_counter] if disabled?
|
148
148
|
|
149
149
|
counter = options.key?(:decrement) ?
|
150
|
-
|
151
|
-
|
150
|
+
strategy.decrement(cache_key, options.fetch(:decrement), options) :
|
151
|
+
strategy.increment(cache_key, options.fetch(:increment, 1), options)
|
152
152
|
|
153
|
-
if
|
153
|
+
if strategy.compare_threshold?(counter, :>, options)
|
154
154
|
before_throttle_callback &&
|
155
155
|
before_throttle_callback.call(handle, key, options[:threshold], options[:interval])
|
156
156
|
|
157
|
-
result = if options[:first_throttled] &&
|
157
|
+
result = if options[:first_throttled] && strategy.first_throttled?(counter, options)
|
158
158
|
:first_throttled
|
159
159
|
else
|
160
160
|
true
|
@@ -177,11 +177,11 @@ module Prop
|
|
177
177
|
|
178
178
|
options = Prop::Options.build(key: key, params: params, defaults: defaults)
|
179
179
|
|
180
|
-
|
180
|
+
strategy = options.fetch(:strategy)
|
181
181
|
|
182
|
-
cache_key =
|
182
|
+
cache_key = strategy.build(key: key, handle: handle, interval: options[:interval])
|
183
183
|
|
184
|
-
[ options, cache_key ]
|
184
|
+
[ options, cache_key, strategy ]
|
185
185
|
end
|
186
186
|
end
|
187
187
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Morten Primdahl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|