gapic-common 1.2.0 → 1.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: ac5c845df865497acbdcb4656a47667dd657083d85ef964f633fc0540ef80493
4
- data.tar.gz: 851ab12cf7a9e8d5e5f93c991846f0a4701a5a96047fef0ebb17392a55061984
3
+ metadata.gz: e4338a183319704aa9fb613e73d34b4092a94ad56f3136c2d4a0490a3218770f
4
+ data.tar.gz: 3a0d1456f8c2bc573245e5c46afae6b2c83be393085bf6bebc5b28c06368d3f7
5
5
  SHA512:
6
- metadata.gz: 3225a82b3d2b7006fa0191c55e46e836724ec07d240d7fc5eb11188ccc5613572a714a012a5e904d85cc70d13bb816cd735faf0c3b458e54552e36f483f07f21
7
- data.tar.gz: 7d4e26754019a71ae7bca52e2e195b71db46c755c6eca1cce34504990e31f2055d390119d9ae0bf81d1b217f7f1d3185200557da3c4d04b8aa251a3d87a13b54
6
+ metadata.gz: 696026e2b9ca26c060c2b65a336f790ec193938a7740d8bdce3008c6bfae40cd80290077b717174554d7e6a1530e470f2523a5859b210c2bf9bd583e7dd72041
7
+ data.tar.gz: 42979948486f1e55f990bdbcc0ee2a697ffbf3c450b4013a7ae293e0960291533bfc4356fcfb8cfdeca5eae94d97a7ff522e2b5667cd1e9611de93cf39af560e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Release History
2
2
 
3
+ ### 1.3.0 (2026-03-05)
4
+
5
+ #### Features
6
+
7
+ * add jitter to retries ([#54](https://github.com/googleapis/ruby-core-libraries/issues/54))
8
+
3
9
  ### 1.2.0 (2025-09-04)
4
10
 
5
11
  #### Features
@@ -30,8 +30,9 @@ module Gapic
30
30
  # @param initial_delay [Numeric] Initial delay in seconds.
31
31
  # @param multiplier [Numeric] The delay scaling factor for each subsequent retry attempt.
32
32
  # @param max_delay [Numeric] Maximum delay in seconds.
33
+ # @param jitter [Numeric] Random jitter added to the delay in seconds.
33
34
  #
34
- def initialize retry_codes: nil, initial_delay: nil, multiplier: nil, max_delay: nil
35
+ def initialize retry_codes: nil, initial_delay: nil, multiplier: nil, max_delay: nil, jitter: nil
35
36
  super
36
37
  end
37
38
  end
@@ -31,6 +31,11 @@ module Gapic
31
31
  # @return [Numeric] Default timeout threshold value in seconds.
32
32
  DEFAULT_TIMEOUT = 3600 # One hour
33
33
 
34
+ # @private
35
+ # @return [Numeric] Default random jitter added to delay in seconds.
36
+ DEFAULT_JITTER = 1.0
37
+ private_constant :DEFAULT_JITTER
38
+
34
39
  ##
35
40
  # Create new Gapic::Common::RetryPolicy instance.
36
41
  #
@@ -39,14 +44,18 @@ module Gapic
39
44
  # @param multiplier [Numeric] The delay scaling factor for each subsequent retry attempt.
40
45
  # @param retry_codes [Array<String|Integer>] List of retry codes.
41
46
  # @param timeout [Numeric] Timeout threshold value in seconds.
47
+ # @param jitter [Numeric] Random jitter added to the delay in seconds.
42
48
  #
43
- def initialize initial_delay: nil, max_delay: nil, multiplier: nil, retry_codes: nil, timeout: nil
49
+ def initialize initial_delay: nil, max_delay: nil, multiplier: nil, retry_codes: nil, timeout: nil, jitter: nil
50
+ raise ArgumentError, "jitter cannot be negative" if jitter&.negative?
51
+
44
52
  # Instance values are set as `nil` to determine whether values are overriden from default.
45
53
  @initial_delay = initial_delay
46
54
  @max_delay = max_delay
47
55
  @multiplier = multiplier
48
56
  @retry_codes = convert_codes retry_codes
49
57
  @timeout = timeout
58
+ @jitter = jitter
50
59
  start!
51
60
  end
52
61
 
@@ -75,6 +84,11 @@ module Gapic
75
84
  @timeout || DEFAULT_TIMEOUT
76
85
  end
77
86
 
87
+ # @return [Numeric] Random jitter added to the delay in seconds.
88
+ def jitter
89
+ @jitter || DEFAULT_JITTER
90
+ end
91
+
78
92
  ##
79
93
  # Returns a duplicate in a non-executing state, i.e. with the deadline
80
94
  # and current delay reset.
@@ -86,7 +100,8 @@ module Gapic
86
100
  max_delay: @max_delay,
87
101
  multiplier: @multiplier,
88
102
  retry_codes: @retry_codes,
89
- timeout: @timeout
103
+ timeout: @timeout,
104
+ jitter: @jitter
90
105
  end
91
106
 
92
107
  ##
@@ -186,6 +201,7 @@ module Gapic
186
201
  @initial_delay ||= retry_policy[:initial_delay]
187
202
  @multiplier ||= retry_policy[:multiplier]
188
203
  @max_delay ||= retry_policy[:max_delay]
204
+ @jitter ||= retry_policy[:jitter]
189
205
 
190
206
  self
191
207
  end
@@ -197,30 +213,34 @@ module Gapic
197
213
  other.max_delay == max_delay &&
198
214
  other.multiplier == multiplier &&
199
215
  other.retry_codes == retry_codes &&
200
- other.timeout == timeout
216
+ other.timeout == timeout &&
217
+ other.jitter == jitter
201
218
  end
202
219
  alias == eql?
203
220
 
204
221
  # @private Hash code
205
222
  def hash
206
- [initial_delay, max_delay, multiplier, retry_codes, timeout].hash
223
+ [initial_delay, max_delay, multiplier, retry_codes, timeout, jitter].hash
207
224
  end
208
225
 
209
226
  private
210
227
 
211
228
  # @private
229
+ # Perform the currently calculated delay, adding a jitter.
230
+ #
212
231
  # @return [Numeric] The performed delay.
213
232
  def delay!
233
+ delay_to_perform = [delay + Kernel.rand(0.0..jitter), max_delay].min
214
234
  if @mock_time
215
- @mock_time += delay
216
- @mock_delay_callback&.call delay
235
+ @mock_time += delay_to_perform
236
+ @mock_delay_callback&.call delay_to_perform
217
237
  else
218
- Kernel.sleep delay
238
+ Kernel.sleep delay_to_perform
219
239
  end
220
240
  end
221
241
 
222
242
  # @private
223
- # @return [Numeric] The new delay in seconds.
243
+ # @return [Numeric] The new delay (sans jitter) in seconds.
224
244
  def increment_delay!
225
245
  @delay = [delay * multiplier, max_delay].min
226
246
  end
@@ -14,6 +14,6 @@
14
14
 
15
15
  module Gapic
16
16
  module Common
17
- VERSION = "1.2.0".freeze
17
+ VERSION = "1.3.0".freeze
18
18
  end
19
19
  end
@@ -38,13 +38,15 @@ module Gapic
38
38
  # @param multiplier [Numeric] The delay scaling factor for each subsequent retry attempt.
39
39
  # @param max_delay [Numeric] Maximum delay in seconds.
40
40
  # @param timeout [Numeric] Timeout threshold value in seconds.
41
+ # @param jitter [Numeric] Random jitter added to the delay in seconds.
41
42
  #
42
- def initialize initial_delay: nil, multiplier: nil, max_delay: nil, timeout: nil
43
+ def initialize initial_delay: nil, multiplier: nil, max_delay: nil, timeout: nil, jitter: nil
43
44
  super(
44
45
  initial_delay: initial_delay || DEFAULT_INITIAL_DELAY,
45
46
  max_delay: max_delay || DEFAULT_MAX_DELAY,
46
47
  multiplier: multiplier || DEFAULT_MULTIPLIER,
47
- timeout: timeout || DEFAULT_TIMEOUT
48
+ timeout: timeout || DEFAULT_TIMEOUT,
49
+ jitter: jitter
48
50
  )
49
51
  end
50
52
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gapic-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google API Authors