lhc 12.2.1 → 12.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41b0c46c3ced081c73e722058eace5a163bb5f84516fb4c6a6d074800ade7d7d
4
- data.tar.gz: e4f37fdf66fa76cc137ec6142bc445468a904f0cbaea2578850c6984478f4228
3
+ metadata.gz: edceeb5ec4bd94dd580a56ed8a0ed0a71a31a8bd5c0b1f08dc96c7f8614d42b3
4
+ data.tar.gz: 183c118e2861b420877c37dfc8f86643d8fd410f8c2abbb8d681cdd168956108
5
5
  SHA512:
6
- metadata.gz: b94b128def1cea71f871c2a14787a52e1ca76755b849fb4504b044f99c9ce4d35ce5a89b8d54621f539f83d51dfa72047b2538adc33f8ab4713f030dcefd31db
7
- data.tar.gz: e1354bbb77a671ef3d98ef33755ada6c4d32373f34609fc858c57744f4c4aed9543373ebf4f837d0ce4bc8c8f8962bcdef60cc2f0ed2045b1dac00f4139ce054
6
+ metadata.gz: 92bd31139ebf0bc508e0681c99c5f35b8b9c93b0a6f56fdf2fcdbfb1faa1c3fa80c2894d48ebc31ad8c4138af5df9548b3f65f87a33bb5ebbf5a873a8d696585
7
+ data.tar.gz: 5b4f2fee1a99c4823ad1d0231624d6bf62794bff60bc27379c180b676439b88f89242813a2b306e44dde1946985114fa1190b1e67a22befdc089f41af520f759
data/README.md CHANGED
@@ -895,16 +895,22 @@ LHC.get('http://local.ch', options)
895
895
  LHC.get('http://local.ch', options)
896
896
  # raises LHC::Throttle::OutOfQuota: Reached predefined quota for local.ch
897
897
  ```
898
+
898
899
  **Options Description**
899
900
  * `track`: enables tracking of current limit/remaining requests of rate-limiting
900
901
  * `break`: quota in percent after which errors are raised. Percentage symbol is optional, values will be converted to integer (e.g. '23.5' will become 23)
901
902
  * `provider`: name of the provider under which throttling tracking is aggregated,
902
- * `limit`: either a hard-coded integer, or a hash pointing at the response header containing the limit value
903
+ * `limit`:
904
+ * a hard-coded integer
905
+ * a hash pointing at the response header containing the limit value
906
+ * a proc that receives the response as argument and returns the limit value
903
907
  * `remaining`:
904
908
  * a hash pointing at the response header containing the current amount of remaining requests
905
- * a proc that receives the response as argument and returns the current amount
906
- of remaining requests
907
- * `expires`: a hash pointing at the response header containing the timestamp when the quota will reset
909
+ * a proc that receives the response as argument and returns the current amount of remaining requests
910
+ * `expires`:
911
+ * a hash pointing at the response header containing the timestamp when the quota will reset
912
+ * a proc that receives the response as argument and returns the timestamp when the quota will reset
913
+
908
914
 
909
915
  #### Zipkin
910
916
 
@@ -49,12 +49,12 @@ class LHC::Throttle < LHC::Interceptor
49
49
 
50
50
  def limit(options:, response:)
51
51
  @limit ||=
52
- begin
53
- if options.is_a?(Integer)
54
- options
55
- elsif options.is_a?(Hash) && options[:header]
56
- response.headers[options[:header]]&.to_i
57
- end
52
+ if options.is_a?(Proc)
53
+ options.call(response)
54
+ elsif options.is_a?(Integer)
55
+ options
56
+ elsif options.is_a?(Hash) && options[:header]
57
+ response.headers[options[:header]]&.to_i
58
58
  end
59
59
  end
60
60
 
@@ -79,6 +79,7 @@ class LHC::Throttle < LHC::Interceptor
79
79
 
80
80
  def convert_expires(value)
81
81
  return if value.blank?
82
+ return value.call(response) if value.is_a?(Proc)
82
83
  return Time.parse(value) if value.match(/GMT/)
83
84
  Time.zone.at(value.to_i).to_datetime
84
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHC
4
- VERSION ||= '12.2.1'
4
+ VERSION ||= '12.3.0'
5
5
  end
@@ -100,34 +100,95 @@ describe LHC::Throttle do
100
100
  end
101
101
  end
102
102
 
103
- describe 'calculate "remaining" in proc' do
104
- let(:quota_current) { 8100 }
105
- let(:options_remaining) do
106
- ->(response) { (response.headers['limit']).to_i - (response.headers['current']).to_i }
103
+ describe 'configuration values as Procs' do
104
+ describe 'calculate "limit" in proc' do
105
+ let(:options_limit) do
106
+ ->(*) { 10_000 }
107
+ end
108
+
109
+ before(:each) do
110
+ LHC.get('http://local.ch', options)
111
+ end
112
+
113
+ context 'breaks' do
114
+ let(:options_break) { '80%' }
115
+
116
+ it 'hit the breaks if throttling quota is reached' do
117
+ expect { LHC.get('http://local.ch', options) }.to raise_error(
118
+ LHC::Throttle::OutOfQuota,
119
+ 'Reached predefined quota for local.ch'
120
+ )
121
+ end
122
+
123
+ context 'still within quota' do
124
+ let(:options_break) { '90%' }
125
+
126
+ it 'does not hit the breaks' do
127
+ LHC.get('http://local.ch', options)
128
+ end
129
+ end
130
+ end
107
131
  end
108
132
 
109
- before(:each) do
110
- stub_request(:get, 'http://local.ch').to_return(
111
- headers: { 'limit' => quota_limit, 'current' => quota_current, 'reset' => quota_reset }
112
- )
113
- LHC.get('http://local.ch', options)
133
+ describe 'calculate "remaining" in proc' do
134
+ let(:quota_current) { 8100 }
135
+ let(:options_remaining) do
136
+ ->(response) { (response.headers['limit']).to_i - (response.headers['current']).to_i }
137
+ end
138
+
139
+ before(:each) do
140
+ stub_request(:get, 'http://local.ch').to_return(
141
+ headers: { 'limit' => quota_limit, 'current' => quota_current, 'reset' => quota_reset }
142
+ )
143
+ LHC.get('http://local.ch', options)
144
+ end
145
+
146
+ context 'breaks' do
147
+ let(:options_break) { '80%' }
148
+
149
+ it 'hit the breaks if throttling quota is reached' do
150
+ expect { LHC.get('http://local.ch', options) }.to raise_error(
151
+ LHC::Throttle::OutOfQuota,
152
+ 'Reached predefined quota for local.ch'
153
+ )
154
+ end
155
+
156
+ context 'still within quota' do
157
+ let(:options_break) { '90%' }
158
+
159
+ it 'does not hit the breaks' do
160
+ LHC.get('http://local.ch', options)
161
+ end
162
+ end
163
+ end
114
164
  end
115
165
 
116
- context 'breaks' do
117
- let(:options_break) { '80%' }
166
+ describe 'calculate "reset" in proc' do
167
+ let(:options_expires) { ->(*) { Time.zone.now + 1.second } }
118
168
 
119
- it 'hit the breaks if throttling quota is reached' do
120
- expect { LHC.get('http://local.ch', options) }.to raise_error(
121
- LHC::Throttle::OutOfQuota,
122
- 'Reached predefined quota for local.ch'
169
+ before(:each) do
170
+ stub_request(:get, 'http://local.ch').to_return(
171
+ headers: { 'limit' => quota_limit, 'remaining' => quota_remaining }
123
172
  )
173
+ LHC.get('http://local.ch', options)
124
174
  end
125
175
 
126
- context 'still within quota' do
127
- let(:options_break) { '90%' }
176
+ context 'breaks' do
177
+ let(:options_break) { '80%' }
128
178
 
129
- it 'does not hit the breaks' do
130
- LHC.get('http://local.ch', options)
179
+ it 'hit the breaks if throttling quota is reached' do
180
+ expect { LHC.get('http://local.ch', options) }.to raise_error(
181
+ LHC::Throttle::OutOfQuota,
182
+ 'Reached predefined quota for local.ch'
183
+ )
184
+ end
185
+
186
+ context 'still within quota' do
187
+ let(:options_break) { '90%' }
188
+
189
+ it 'does not hit the breaks' do
190
+ LHC.get('http://local.ch', options)
191
+ end
131
192
  end
132
193
  end
133
194
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 12.2.1
4
+ version: 12.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-25 00:00:00.000000000 Z
11
+ date: 2020-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport