freno-client 0.9.0 → 0.10.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: d770b3f1d5945dfd32db3e566c8881d0995afd0679e28e4eb1cb6275d0e44ee7
4
- data.tar.gz: a16e811393f6a20ab6c8fd0ea75a870a824399d29c7d4051df9ee2801b461845
3
+ metadata.gz: 4715bae7724513dca1402c0a15d8e5ad46009a67b9b6ea578f89a3ffbc7752cb
4
+ data.tar.gz: 646cb16cbd0b47093f9f6ade58e1da3877e83023e73ed8d1eefc6644e293405b
5
5
  SHA512:
6
- metadata.gz: bc6292fe3c1221bae5ac70f8f67145f5e2db18d23bb1f0c869d0c98e54d388348e9670495280e6f49e86991612a8a01d3109548ba4575cedb4289856751c09f1
7
- data.tar.gz: 46d9b541781f7bc250e0cb038177cdf8327cae1090bef229b667309c98b06c9f042d34cc583b9ea091014313bc459849c0dcf4c8544ffef10523828433095399
6
+ metadata.gz: da8ed8de2d0526e42f4cc29edacc1f09512c7d843c6ca4b00f094d76fa87f15a1f1b1687ab394efc118004814dc2bf8f65849402aa93904c292ec808d649a7e2
7
+ data.tar.gz: 9015593fd377b5ce44f5912f12906e2bd1def7e6a31430c46cdf1a9fc168fde41eec19d1127adeda657af43c6ca210eafb140eb18b57d8fd3367d798caa820d3
data/README.md CHANGED
@@ -202,6 +202,7 @@ module Freno
202
202
 
203
203
  DEFAULT_WAIT_SECONDS = 0.5
204
204
  DEFAULT_MAX_WAIT_SECONDS = 10
205
+ DEFAULT_MAX_RETRIES = 4
205
206
 
206
207
  def initialize(client: nil,
207
208
  app: nil,
@@ -209,7 +210,10 @@ module Freno
209
210
  instrumenter: Instrumenter::Noop,
210
211
  circuit_breaker: CircuitBreaker::Noop,
211
212
  wait_seconds: DEFAULT_WAIT_SECONDS,
212
- max_wait_seconds: DEFAULT_MAX_WAIT_SECONDS)
213
+ max_wait_seconds: DEFAULT_MAX_WAIT_SECONDS,
214
+ jitter: true,
215
+ backoff: true,
216
+ max_retries: DEFAULT_MAX_RETRIES)
213
217
 
214
218
 
215
219
  @client = client
@@ -219,6 +223,9 @@ module Freno
219
223
  @circuit_breaker = circuit_breaker
220
224
  @wait_seconds = wait_seconds
221
225
  @max_wait_seconds = max_wait_seconds
226
+ @jitter = jitter
227
+ @backoff = backoff
228
+ @max_retries = max_retries
222
229
 
223
230
  yield self if block_given?
224
231
 
@@ -233,10 +240,78 @@ end
233
240
  A Throttler instance will make calls to freno on behalf of the given `app`,
234
241
  using the given `client` (an instance of `Freno::Client`).
235
242
 
236
- You optionally provide the time you want the throttler to sleep in case the check to freno fails, this is `wait_seconds`.
243
+ `wait_seconds` is the minimum retry delay. `max_wait_seconds` is both the
244
+ upper bound for an individual delay and the total sleep budget. Both must
245
+ be positive finite numbers, with `max_wait_seconds` greater than
246
+ `wait_seconds`. Defaults are 0.5 and 10 seconds; caller overrides are used
247
+ directly, not replaced with fixed caps.
248
+
249
+ Backoff grows the base intervals geometrically from `wait_seconds` to
250
+ `max_wait_seconds` across the allowed retry slots. With four retries and
251
+ the defaults, the bases are approximately 0.5, 1.357, 3.684, and 10 seconds.
252
+ With a caller's 1-second minimum and 30-second budget, they become
253
+ approximately 1, 3.107, 9.655, and 30 seconds. With one retry, only the initial
254
+ base interval is used.
255
+
256
+ `max_retries` defaults to 4: at most four retry rounds after the initial
257
+ immediate check, or five rounds total. If the last allowed round succeeds,
258
+ the protected block still runs. If it fails, the throttler raises
259
+ `Freno::Throttler::RetryLimitExceeded`, even when wait budget remains.
260
+ Set `max_retries: 0` to check once without sleeping. The limit must be a
261
+ non-negative integer.
262
+
263
+ For every retry, jitter samples a fresh delay from the interval between
264
+ 50% and 150% of that retry's base, restricted to the configured minimum and
265
+ maximum. Default delays therefore never fall below 0.5 seconds or exceed
266
+ 10 seconds; the 1/30 override stays between 1 and 30 seconds. Sampling within
267
+ the restricted interval avoids grouping out-of-range draws at the bounds.
268
+ The first check is still immediate.
269
+
270
+ Set `backoff: false` for constant base intervals with jitter, or `jitter: false`
271
+ for backoff without randomization. Disable both to retain fixed retry delays:
272
+
273
+ ```ruby
274
+ throttler = Freno::Throttler.new(client: client, app: :my_app, backoff: false, jitter: false)
275
+ ```
276
+
277
+ The retry-count and total-wait limits still apply when backoff and jitter
278
+ are disabled.
237
279
 
238
280
  If replication lags badly, you can control until when you want to keep sleeping
239
- and retrying the check by setting `max_wait_seconds`. When that times out, the throttle will raise a `Freno::Throttler::WaitedTooLong` error.
281
+ and retrying the check by setting `max_wait_seconds`. Each sampled delay counts
282
+ toward that budget and the cumulative `waited` instrumentation. If the next delay
283
+ would exceed the remaining budget, the throttler raises
284
+ `Freno::Throttler::WaitedTooLong` without sleeping again. As before, this budget
285
+ covers retry sleeps, not time spent making HTTP requests.
286
+
287
+ Backoff, retry count, and wait budget reset for each `throttle` call.
288
+ The upper bound is not a guaranteed final sleep. Earlier waits consume the
289
+ total budget, so a final delay equal to that budget cannot fit after earlier
290
+ positive waits. For example, with jitter disabled the default first three
291
+ delays total about 5.54 seconds; the next 10-second delay does not fit.
292
+ Jitter can change how many retries fit, but never allows more than
293
+ `max_retries`. Checking stops immediately if Freno allows the operation.
294
+
295
+ When either limit stops polling, the protected block does not run. Neither
296
+ `RetryLimitExceeded` nor `WaitedTooLong` itself closes HTTP or database
297
+ connections. Both inherit from `Freno::Throttler::Error`. A caller that catches
298
+ the error and retries can start another cycle with fresh limits.
299
+
300
+ Both limits emit `throttler.waited_too_long`, preserving existing terminal
301
+ failure instrumentation. Its payload includes `reason: :retry_limit` or
302
+ `reason: :wait_budget`, the completed `retries`, and `max_retries`, alongside
303
+ the cumulative `waited` and total budget `max`. Existing timeout counters
304
+ that consume this event will also count retry-limit failures.
305
+
306
+ Neither backoff nor jitter retries transport errors, changes Freno's admission
307
+ decisions, or limits concurrent writes. Transport errors still raise
308
+ `ClientError` immediately; separate transport and application retry loops are
309
+ unaffected. Applications still need shared rate and concurrency limits to
310
+ control how much work resumes.
311
+
312
+ The retry cap counts polling rounds, not individual HTTP requests or TCP
313
+ connections. A round can check multiple stores. It does not cap adapter-level
314
+ transport retries or retries made by an application or job framework.
240
315
 
241
316
  #### Instrumenting the throttler
242
317
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Freno
4
- Error = Class.new(StandardError)
4
+ class Error < StandardError
5
+ end
5
6
  end
@@ -5,7 +5,8 @@ module Freno
5
5
  module Preconditions
6
6
  module_function
7
7
 
8
- PreconditionNotMet = Class.new(ArgumentError)
8
+ class PreconditionNotMet < ArgumentError
9
+ end
9
10
 
10
11
  class Checker
11
12
  attr_reader :errors
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Freno
4
4
  class Client
5
- VERSION = Gem::Version.new("0.9.0")
5
+ VERSION = Gem::Version.new("0.10.0")
6
6
  end
7
7
  end
data/lib/freno/client.rb CHANGED
@@ -233,7 +233,7 @@ module Freno
233
233
  outermost = to_decorate[0]
234
234
  current = outermost
235
235
 
236
- (to_decorate[1..]).each do |decorator|
236
+ to_decorate[1..].each do |decorator|
237
237
  current.request = decorator
238
238
  current = current.request
239
239
  end
@@ -15,6 +15,13 @@ module Freno
15
15
  end
16
16
  end
17
17
 
18
+ # Raised if all allowed retry rounds were rejected.
19
+ class RetryLimitExceeded < Error
20
+ def initialize(max_retries:, waited_seconds:)
21
+ super("Retry limit of #{max_retries} reached after waiting #{waited_seconds} seconds")
22
+ end
23
+ end
24
+
18
25
  # Raised if the circuit breaker is open and didn't allow latest request
19
26
  class CircuitOpen < Error; end
20
27
 
@@ -35,6 +35,7 @@ module Freno
35
35
  class Throttler
36
36
  DEFAULT_WAIT_SECONDS = 0.5
37
37
  DEFAULT_MAX_WAIT_SECONDS = 10
38
+ DEFAULT_MAX_RETRIES = 4
38
39
  REQUIRED_ARGS = %i[
39
40
  client
40
41
  app
@@ -51,7 +52,10 @@ module Freno
51
52
  :instrumenter,
52
53
  :circuit_breaker,
53
54
  :wait_seconds,
54
- :max_wait_seconds
55
+ :max_wait_seconds,
56
+ :jitter,
57
+ :backoff,
58
+ :max_retries
55
59
 
56
60
  # Initializes a new instance of the throttler
57
61
  #
@@ -91,9 +95,17 @@ module Freno
91
95
  # By default, the circuit breaker is `CircuitBreaker::Noop`, which
92
96
  # always allows requests, and does not provide resiliency guarantees.
93
97
  #
94
- # - `:wait_seconds`: A positive float indicating the number of seconds the
95
- # throttler will wait before checking again, in case some of the stores
96
- # didn't catch-up the last time they were check.
98
+ # - `:wait_seconds`: A positive float indicating the base delay before
99
+ # checking again when some of the stores haven't caught up.
100
+ #
101
+ # - `:jitter`: Randomize each delay around its base interval, bounded by
102
+ # `wait_seconds` and `max_wait_seconds`. Defaults to true.
103
+ #
104
+ # - `:backoff`: Grow base intervals geometrically from `wait_seconds` to
105
+ # `max_wait_seconds` across the allowed retry slots. Defaults to true.
106
+ #
107
+ # - `:max_retries`: The maximum retry rounds after the initial check.
108
+ # Defaults to 4. Set 0 to check once without retrying.
97
109
  #
98
110
  # - `:max_wait_seconds`: A positive float indicating the maxium number of
99
111
  # seconds the throttler will wait in total for replicas to catch-up
@@ -106,7 +118,10 @@ module Freno
106
118
  instrumenter: Instrumenter::Noop,
107
119
  circuit_breaker: CircuitBreaker::Noop,
108
120
  wait_seconds: DEFAULT_WAIT_SECONDS,
109
- max_wait_seconds: DEFAULT_MAX_WAIT_SECONDS
121
+ max_wait_seconds: DEFAULT_MAX_WAIT_SECONDS,
122
+ jitter: true,
123
+ backoff: true,
124
+ max_retries: DEFAULT_MAX_RETRIES
110
125
  )
111
126
  @client = client
112
127
  @app = app
@@ -115,6 +130,9 @@ module Freno
115
130
  @circuit_breaker = circuit_breaker
116
131
  @wait_seconds = wait_seconds
117
132
  @max_wait_seconds = max_wait_seconds
133
+ @jitter = jitter
134
+ @backoff = backoff
135
+ @max_retries = max_retries
118
136
 
119
137
  yield self if block_given?
120
138
 
@@ -136,10 +154,14 @@ module Freno
136
154
  #
137
155
  # With that information it asks freno whether all the stores are ok.
138
156
  # In case they are, it executes the given block.
139
- # Otherwise, it waits `wait_seconds` before trying again.
157
+ # Otherwise, it waits before trying again, starting at `wait_seconds`
158
+ # and backing off with jitter by default. Both can be disabled independently.
159
+ # The base interval, retry count, and wait budget reset for each call.
160
+ #
161
+ # If all `max_retries` retry rounds fail, it raises `RetryLimitExceeded`.
140
162
  #
141
- # In case the throttler has waited more than `max_wait_seconds`, it raises
142
- # a `WaitedTooLong` error.
163
+ # If the next delay would exceed the remaining `max_wait_seconds` budget,
164
+ # it raises a `WaitedTooLong` error without sleeping.
143
165
  #
144
166
  # In case there's an underlying Freno error, it raises a `ClientError`
145
167
  # error.
@@ -151,11 +173,9 @@ module Freno
151
173
  #
152
174
  # - "throttler.called" each time this method is called
153
175
  # - "throttler.succeeded" when the stores were ok, before yielding the block
154
- # - "throttler.waited" when the stores were not ok, after waiting
155
- # `wait_seconds`
156
- # - "throttler.waited_too_long" when the stores were not ok, but the
157
- # thottler already waited at least `max_wait_seconds`, right before
158
- # raising `WaitedTooLong`
176
+ # - "throttler.waited" after sleeping, with the cumulative sampled delays
177
+ # - "throttler.waited_too_long" when the retry limit or wait budget stops
178
+ # polling. The `reason` is `:retry_limit` or `:wait_budget`.
159
179
  # - "throttler.freno_errored" when there was an error with freno, before
160
180
  # raising `ClientError`.
161
181
  # - "throttler.circuit_open" when the circuit breaker does not allow the
@@ -165,6 +185,7 @@ module Freno
165
185
  store_names = mapper.call(context)
166
186
  instrument(:called, store_names: store_names)
167
187
  waited = 0
188
+ retries = 0
168
189
 
169
190
  while true
170
191
  unless circuit_breaker.allow_request?
@@ -178,13 +199,23 @@ module Freno
178
199
  break
179
200
  end
180
201
 
181
- if waited + wait_seconds > max_wait_seconds
182
- instrument(:waited_too_long, store_names: store_names, waited: waited, max: max_wait_seconds)
202
+ if retries >= max_retries
203
+ instrument(:waited_too_long, store_names: store_names, waited: waited, max: max_wait_seconds,
204
+ reason: :retry_limit, retries: retries, max_retries: max_retries)
205
+ circuit_breaker.failure
206
+ raise RetryLimitExceeded.new(max_retries: max_retries, waited_seconds: waited)
207
+ end
208
+
209
+ retry_wait_seconds = next_wait_seconds(retries)
210
+ if waited + retry_wait_seconds > max_wait_seconds
211
+ instrument(:waited_too_long, store_names: store_names, waited: waited, max: max_wait_seconds,
212
+ reason: :wait_budget, retries: retries, max_retries: max_retries)
183
213
  circuit_breaker.failure
184
214
  raise WaitedTooLong.new(waited_seconds: waited, max_wait_seconds: max_wait_seconds)
185
215
  else
186
- wait
187
- waited += wait_seconds
216
+ wait(retry_wait_seconds)
217
+ waited += retry_wait_seconds
218
+ retries += 1
188
219
  instrument(:waited, store_names: store_names, waited: waited, max: max_wait_seconds)
189
220
  end
190
221
  end
@@ -201,9 +232,16 @@ module Freno
201
232
  errors << "#{argument} must be provided" unless send(argument)
202
233
  end
203
234
 
204
- unless max_wait_seconds > wait_seconds
235
+ valid_wait = positive_finite_number?(wait_seconds)
236
+ valid_max = positive_finite_number?(max_wait_seconds)
237
+ errors << "wait_seconds must be a positive finite number" unless valid_wait
238
+ errors << "max_wait_seconds must be a positive finite number" unless valid_max
239
+ if valid_wait && valid_max && max_wait_seconds <= wait_seconds
205
240
  errors << "max_wait_seconds (#{max_wait_seconds}) has to be greather than wait_seconds (#{wait_seconds})"
206
241
  end
242
+ errors << "jitter must be true or false" unless [true, false].include?(jitter)
243
+ errors << "backoff must be true or false" unless [true, false].include?(backoff)
244
+ errors << "max_retries must be a non-negative integer" unless max_retries.is_a?(Integer) && max_retries >= 0
207
245
 
208
246
  raise ArgumentError, errors.join("\n") if errors.any?
209
247
  end
@@ -218,8 +256,30 @@ module Freno
218
256
  raise ClientError, error
219
257
  end
220
258
 
221
- def wait
222
- sleep wait_seconds
259
+ def positive_finite_number?(value)
260
+ value.is_a?(Numeric) && value.real? && value.finite? && value.positive?
261
+ end
262
+
263
+ def base_wait_seconds(retries)
264
+ return wait_seconds unless backoff && max_retries > 1 && retries.positive?
265
+ return max_wait_seconds if retries == max_retries - 1
266
+
267
+ progress = retries.fdiv(max_retries - 1)
268
+ interval = Math.exp(((1 - progress) * Math.log(wait_seconds)) + (progress * Math.log(max_wait_seconds)))
269
+ interval.clamp(wait_seconds, max_wait_seconds)
270
+ end
271
+
272
+ def next_wait_seconds(retries)
273
+ base = base_wait_seconds(retries)
274
+ return base unless jitter
275
+
276
+ minimum = [wait_seconds, base * 0.5].max
277
+ maximum = [max_wait_seconds, base * 1.5].min
278
+ minimum + (rand * (maximum - minimum))
279
+ end
280
+
281
+ def wait(seconds)
282
+ sleep seconds
223
283
  end
224
284
 
225
285
  def instrument(event_name, payload = {}, &block)
metadata CHANGED
@@ -1,80 +1,80 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freno-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
- - GitHub
8
- autorequire:
7
+ - GitHub
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-04-18 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: faraday
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "<"
18
- - !ruby/object:Gem::Version
19
- version: '3'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "<"
25
- - !ruby/object:Gem::Version
26
- version: '3'
27
- description: 'freno-client is a Ruby library that interacts with Freno using HTTP.
28
- Freno is a throttling service and its source code is available at https://github.com/github/freno '
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - "<"
18
+ - !ruby/object:Gem::Version
19
+ version: "3"
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - "<"
26
+ - !ruby/object:Gem::Version
27
+ version: "3"
28
+ description: "freno-client is a Ruby library that interacts with Freno using HTTP. Freno is a throttling service and its source code is available at https://github.com/github/freno "
29
29
  email: opensource+freno-client@github.com
30
30
  executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files:
33
- - README.md
33
+ - README.md
34
34
  files:
35
- - LICENSE.txt
36
- - README.md
37
- - freno-client.gemspec
38
- - lib/freno/client.rb
39
- - lib/freno/client/errors.rb
40
- - lib/freno/client/preconditions.rb
41
- - lib/freno/client/request.rb
42
- - lib/freno/client/requests/check.rb
43
- - lib/freno/client/requests/check_read.rb
44
- - lib/freno/client/requests/replication_delay.rb
45
- - lib/freno/client/result.rb
46
- - lib/freno/client/version.rb
47
- - lib/freno/throttler.rb
48
- - lib/freno/throttler/circuit_breaker.rb
49
- - lib/freno/throttler/errors.rb
50
- - lib/freno/throttler/instrumenter.rb
51
- - lib/freno/throttler/mapper.rb
52
- homepage: https://github.com/github/freno-client
35
+ - LICENSE.txt
36
+ - README.md
37
+ - freno-client.gemspec
38
+ - lib/freno/client.rb
39
+ - lib/freno/client/errors.rb
40
+ - lib/freno/client/preconditions.rb
41
+ - lib/freno/client/request.rb
42
+ - lib/freno/client/requests/check.rb
43
+ - lib/freno/client/requests/check_read.rb
44
+ - lib/freno/client/requests/replication_delay.rb
45
+ - lib/freno/client/result.rb
46
+ - lib/freno/client/version.rb
47
+ - lib/freno/throttler.rb
48
+ - lib/freno/throttler/circuit_breaker.rb
49
+ - lib/freno/throttler/errors.rb
50
+ - lib/freno/throttler/instrumenter.rb
51
+ - lib/freno/throttler/mapper.rb
52
+ homepage: "https://github.com/github/freno-client"
53
53
  licenses:
54
- - MIT
54
+ - MIT
55
55
  metadata:
56
- allowed_push_host: https://rubygems.org
57
- bug_tracker_uri: https://github.com/github/freno-client/issues
58
- homepage_uri: https://github.com/github/freno-client
59
- rubygems_mfa_required: 'true'
60
- source_code_uri: https://github.com/github/freno-client
61
- post_install_message:
56
+ allowed_push_host: "https://rubygems.org"
57
+ bug_tracker_uri: "https://github.com/github/freno-client/issues"
58
+ homepage_uri: "https://github.com/github/freno-client"
59
+ rubygems_mfa_required: "true"
60
+ source_code_uri: "https://github.com/github/freno-client"
62
61
  rdoc_options: []
63
62
  require_paths:
64
- - lib
63
+ - lib
65
64
  required_ruby_version: !ruby/object:Gem::Requirement
66
65
  requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: '3.0'
66
+ -
67
+ - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "3.0"
70
70
  required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- version: '0'
72
+ -
73
+ - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
75
76
  requirements: []
76
- rubygems_version: 3.4.19
77
- signing_key:
77
+ rubygems_version: 4.1.0
78
78
  specification_version: 4
79
- summary: A library for interacting with Freno, the throttler service
79
+ summary: "A library for interacting with Freno, the throttler service"
80
80
  test_files: []