Skip to content

Commit 35df270

Browse files
Update spec/integration/backpressure_examples_spec.rb
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 187cd63 commit 35df270

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

spec/integration/backpressure_examples_spec.rb

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,36 @@
22

33
require 'spec_helper'
44

5-
RETRYABLE_ERROR_LABEL = 'RetryableError'
6-
SYSTEM_OVERLOADED_ERROR = 'SystemOverloadedError'
7-
8-
def system_overloaded_error?(error)
9-
error.respond_to?(:label?) && error.label?(SYSTEM_OVERLOADED_ERROR)
10-
end
5+
describe 'backpressure examples' do
6+
RETRYABLE_ERROR_LABEL = 'RetryableError'
7+
SYSTEM_OVERLOADED_ERROR = 'SystemOverloadedError'
8+
BASE_BACKOFF_MS = 100
9+
MAX_BACKOFF_MS = 10_000
1110

12-
BASE_BACKOFF_MS = 100
13-
MAX_BACKOFF_MS = 10_000
11+
def system_overloaded_error?(error)
12+
error.respond_to?(:label?) && error.label?(SYSTEM_OVERLOADED_ERROR)
13+
end
1414

15-
def calculate_exponential_backoff(attempt)
16-
rand * [ MAX_BACKOFF_MS, BASE_BACKOFF_MS * (2**(attempt - 1)) ].min
17-
end
15+
def calculate_exponential_backoff(attempt)
16+
rand * [ MAX_BACKOFF_MS, BASE_BACKOFF_MS * (2**(attempt - 1)) ].min
17+
end
1818

19-
def with_retries(max_attempts: 2)
20-
max_attempts.times do |attempt|
21-
is_retry = attempt > 0
22-
if is_retry
23-
delay = calculate_exponential_backoff(attempt)
24-
sleep(delay / 1000.0)
25-
end
26-
begin
27-
return yield
28-
rescue StandardError => e
29-
is_retryable_overload_error = system_overloaded_error?(e) && e.label?(RETRYABLE_ERROR_LABEL)
30-
can_retry = is_retryable_overload_error && attempt + 1 < max_attempts
31-
raise e unless can_retry
19+
def with_retries(max_attempts: 2)
20+
max_attempts.times do |attempt|
21+
is_retry = attempt > 0
22+
if is_retry
23+
delay = calculate_exponential_backoff(attempt)
24+
sleep(delay / 1000.0)
25+
end
26+
begin
27+
return yield
28+
rescue StandardError => e
29+
is_retryable_overload_error = system_overloaded_error?(e) && e.label?(RETRYABLE_ERROR_LABEL)
30+
can_retry = is_retryable_overload_error && attempt + 1 < max_attempts
31+
raise e unless can_retry
32+
end
3233
end
3334
end
34-
end
35-
36-
describe 'backpressure examples' do
3735
describe '#system_overloaded_error?' do
3836
it 'returns true for an error with the SystemOverloadedError label' do
3937
error = Mongo::Error.new('overloaded')

0 commit comments

Comments
 (0)