我正在尝试包装两个 Polly 策略并想要返回IAsyncPolicy
,但它给出了错误,
从 Polly.Retry.RetryPolicy < System.Net.Http.HttpResponseMessage> 转换为 Polly.IAsyncPolicy
public static IAsyncPolicy CreateDefaultRetryPolicy()
{
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromSeconds(180));
var waitAndRetryPolicy = Polly.Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)),
(result, timeSpan, context) =>
{
});
return Policy.WrapAsync(timeoutPolicy, waitAndRetryPolicy);
}
Run Code Online (Sandbox Code Playgroud)
如何包装并返回?
我正在尝试用纯 Java 创建一个实用程序类,其中包含指数退避算法实现所需的逻辑,并且具有完全抖动,因为将有多个客户端发送请求。我有另一个类,它的方法执行 GET 或 POST 请求并返回带有状态代码的响应。仅当状态代码位于 5xx 时,我才想重试(也称为使用指数退避策略)。当前代码未编译。
调用方法如下所示:
HttpResponse response = executeGetRequest( params );
int statusCode = response.getStatusCode();
//some status code validation
Run Code Online (Sandbox Code Playgroud)
我的 ExponentialBackoffStrategy 类是:
public class ExponentialBackoffStrategy {
private final long maxBackoff;
private long backoffValue;
private long attempts;
private static final long DEFAULT_MAX_RETRIES = 900_000;
private Random random = new Random();
public ExponentialBackoffStrategy( long maxBackoff ) {
this.maxBackoff = maxBackoff;
}
public long getWaitTimeExp() {
if( backoffValue >= maxBackoff ) {
return maxBackoff;
}
double pow = …
Run Code Online (Sandbox Code Playgroud) 这2个重试策略表明相同吗?
Policy
.Handle<SomeExceptionType>()
.WaitAndRetry(
new[]
{
TimeSpan.FromMinutes(1),
TimeSpan.FromMinutes(1),
TimeSpan.FromMinutes(1)
});
Run Code Online (Sandbox Code Playgroud)
Policy
.Handle<SomeExceptionType>()
.WaitAndRetry(
3,
retryAttempt => TimeSpan.FromMinutes(1)
);
Run Code Online (Sandbox Code Playgroud) 我的一位同事问我这个问题,断路器和重试之间有什么区别,但我无法正确回答他。据我所知,如果请求负载很大,断路器很有用,但这可以通过重试来实现。然后何时使用断路器以及何时重试。
另外,是否可以在同一个 API 上使用两者?
circuit-breaker spring-boot microservices retry-logic resilience4j-retry
我有以下策略设置:
// Slack api for postMessage is generally 1 per channel per second.
// Some workspace specific limits may apply.
// We just limit everything to 1 second.
var slackApiRateLimitPerChannelPerSecond = 1;
var rateLimit = Policy.RateLimitAsync(slackApiRateLimitPerChannelPerSecond, TimeSpan.FromSeconds(slackApiRateLimitPerChannelPerSecond),
(retryAfter, _) => retryAfter.Add(TimeSpan.FromSeconds(slackApiRateLimitPerChannelPerSecond)));
Run Code Online (Sandbox Code Playgroud)
这应该:
我无法将其包装成第二个会重试的策略......
我可以像这样重试:
try
{
_policy.Execute(...)
}
catch(RateLimitedException ex)
{
// Policy.Retry with ex.RetryAfter
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不对。
我想重试几次(3?)次,以便该方法更具弹性 - 我该怎么做?
我想在一段时间内失败时重试我的函数调用。这样做的最佳方法是什么。这会正常工作吗。
CompletableFuture.runAsync(() -> {
for (int i = 0; i < 3; i++) {
try {
dndService.initateDNDRequest(transactionId, circle, category, "PREPAID");
break;
} catch (Exception e) {
try {
TimeUnit.SECONDS.sleep(10);//wait for few minutes while next attempt
} catch (InterruptedException e1) {
LOGGER.error("Error while retrying request for DND.");
}
LOGGER.error("Request retry for DND count"+i);
}
}
}, executor);
Run Code Online (Sandbox Code Playgroud) AnHttpRequestMessage
只能发送一次。尝试重新发送会导致InvalidOperationException
.
那么 Polly 如何能够规避这种行为,换句话说,使用AddPolicyHandler
重试策略时幕后发生了什么?我知道它使用 aDelegatingHandler
但它如何能够多次处理同一条消息?
我有一个服务 AAA,每分钟向 RabbitMQ 交换发布 10 到 5 万条消息。.NET Core 服务 BBB 订阅一个队列(所有消息都路由到该队列),并为每条消息调用另一个通过 Internet 的 HTTP 服务 CCC。问题是 CCC 非常不可靠,每天有几次它会完全关闭一两分钟,每周至少有一次它会关闭一个小时。
我无法控制 AAA 或 CCC。如何使用 RabbitMQ 路由功能可靠地传递所有消息?
unreliable-connection rabbitmq reliable-message-delivery polly retry-logic
我已经实现了以下代码,用于在Go中重试http发布请求。
在第二次重试尝试中,我总是将请求正文设为null。我试过推迟req.body.close(),但是它不起作用。谁能帮我解决这个问题?
func httpRetry(req *http.Request, timeOut time.Duration, retryAttempts int, retryDelay time.Duration) (*http.Response, error) {
attempts := 0
for {
attempts++
fmt.Println("Attempt - ", attempts)
statusCode := 0
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var netClient = &http.Client{
Timeout: time.Second * timeOut,
Transport: tr,
}
fmt.Println("ret 88888 ", req)
response, err := netClient.Do(req)
//defer req.Body.Close()
fmt.Println(response, " dd", err)
if response != nil {
fmt.Println(response.StatusCode)
statusCode = response.StatusCode
}
if err != nil {
fmt.Println(err)
//return response, err
}
if …
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现重试逻辑。我的代码按预期工作,直到重试方法的返回类型为空。当我将其更改为 String 时,@Recover 停止工作。
@Component
public class AdapterImpl {
int count = 0;
@Retryable(include = {NullPointerException.class, IllegalStateException.class}, backoff = @Backoff(delay = 100, maxDelay = 101), maxAttempts = 5)
public void retry(String foo) {
System.out.println(foo + " " + count++);
if (foo.equals("foo")) {
throw new NullPointerException("foo");
} else if (foo.equals("bar")) {
throw new IllegalStateException("bar");
}
// return "hi";
}
@Recover
public void connectionException(NullPointerException e) {
System.out.println("Retry failure NullPointerException");
}
@Recover
public void connectionException(IllegalStateException e) {
System.out.println("Retry failure IllegalStateException");
}
}
Run Code Online (Sandbox Code Playgroud)
对于 …
retry-logic ×10
polly ×5
c# ×4
java ×3
http ×2
asp.net-core ×1
go ×1
java-8 ×1
policywrap ×1
rabbitmq ×1
schedule ×1
spring-boot ×1
spring-retry ×1
timeout ×1