-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathRetryUtils.java
More file actions
105 lines (92 loc) · 3.97 KB
/
RetryUtils.java
File metadata and controls
105 lines (92 loc) · 3.97 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright 2023 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.powertools.testutils;
import java.time.Duration;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
/**
* Utility class for consistent retry configuration across all test utilities.
*/
public final class RetryUtils {
private static final Logger LOG = LoggerFactory.getLogger(RetryUtils.class);
private static final RetryConfig DEFAULT_RETRY_CONFIG = RetryConfig.custom()
.maxAttempts(60) // 60 attempts over 5 minutes
.waitDuration(Duration.ofSeconds(5)) // 5 seconds between attempts
.build();
private RetryUtils() {
// Utility class
}
/**
* Creates a retry instance with default configuration for the specified throwable types.
*
* @param name the name for the retry instance
* @param retryOnThrowables the throwable classes to retry on
* @return configured Retry instance
*/
@SafeVarargs
public static Retry createRetry(String name, Class<? extends Throwable>... retryOnThrowables) {
return createRetry(name, DEFAULT_RETRY_CONFIG, retryOnThrowables);
}
/**
* Creates a retry instance with custom configuration for the specified throwable types.
*
* @param name the name for the retry instance
* @param customConfig the custom retry configuration
* @param retryOnThrowables the throwable classes to retry on
* @return configured Retry instance
*/
@SafeVarargs
public static Retry createRetry(String name, RetryConfig customConfig,
Class<? extends Throwable>... retryOnThrowables) {
RetryConfig config = RetryConfig.from(customConfig)
.retryExceptions(retryOnThrowables)
.build();
Retry retry = Retry.of(name, config);
retry.getEventPublisher().onRetry(event -> LOG.warn("Retry attempt {} for {}: {}",
event.getNumberOfRetryAttempts(), name, event.getLastThrowable().getMessage()));
return retry;
}
/**
* Decorates a supplier with retry logic for the specified throwable types.
*
* @param supplier the supplier to decorate
* @param name the name for the retry instance
* @param retryOnThrowables the throwable classes to retry on
* @return decorated supplier with retry logic
*/
@SafeVarargs
public static <T> Supplier<T> withRetry(Supplier<T> supplier, String name,
Class<? extends Throwable>... retryOnThrowables) {
Retry retry = createRetry(name, retryOnThrowables);
return Retry.decorateSupplier(retry, supplier);
}
/**
* Decorates a supplier with custom retry logic for the specified throwable types.
*
* @param supplier the supplier to decorate
* @param name the name for the retry instance
* @param customConfig the custom retry configuration
* @param retryOnThrowables the throwable classes to retry on
* @return decorated supplier with retry logic
*/
@SafeVarargs
public static <T> Supplier<T> withRetry(Supplier<T> supplier, String name, RetryConfig customConfig,
Class<? extends Throwable>... retryOnThrowables) {
Retry retry = createRetry(name, customConfig, retryOnThrowables);
return Retry.decorateSupplier(retry, supplier);
}
}