-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathretryer.rb
47 lines (45 loc) · 1.81 KB
/
retryer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Temporal
module Client
module Retryer
INITIAL_INTERVAL_S = 0.2
MAX_INTERVAL_S = 6.0
BACKOFF_COEFFICIENT = 1.2
DEFAULT_RETRIES = 24 # gets us to about 60s given the other parameters, assuming 0 latency
# List pulled from RpcRetryOptions in the Java SDK
# https://github.com/temporalio/sdk-java/blob/ad8831d4a4d9d257baf3482ab49f1aa681895c0e/temporal-serviceclient/src/main/java/io/temporal/serviceclient/RpcRetryOptions.java#L32
# No amount of retrying will help in these cases.
DO_NOT_RETRY_ERRORS = [
GRPC::AlreadyExists,
GRPC::Cancelled,
GRPC::FailedPrecondition,
GRPC::InvalidArgument,
# If the activity has timed out, the server will return this and will never accept a retry
GRPC::NotFound,
GRPC::PermissionDenied,
GRPC::Unauthenticated,
GRPC::Unimplemented,
].freeze
# Used for backoff retries in certain cases when calling temporal server.
# on_retry - a proc that's executed each time you need to retry
def self.with_retries(times: DEFAULT_RETRIES, on_retry: nil, &block)
# Values taken from the Java SDK
# https://github.com/temporalio/sdk-java/blob/ad8831d4a4d9d257baf3482ab49f1aa681895c0e/temporal-serviceclient/src/main/java/io/temporal/serviceclient/RpcRetryOptions.java#L32
current_interval_s = INITIAL_INTERVAL_S
retry_i = 0
loop do
begin
return yield
rescue *DO_NOT_RETRY_ERRORS
raise
rescue => e
raise e if retry_i >= times
retry_i += 1
on_retry.call if on_retry
sleep(current_interval_s)
current_interval_s = [current_interval_s * BACKOFF_COEFFICIENT, MAX_INTERVAL_S].min
end
end
end
end
end
end