提问人:Zapnologica 提问时间:10/26/2023 最后编辑:Zapnologica 更新时间:10/26/2023 访问量:78
Polly 重试策略使其阻止或等待成功
Polly Retry policy make it block or wait till success
问:
我有以下 polly 重试策略:
var retry = new ResiliencePipelineBuilder();
retry.AddRetry(new RetryStrategyOptions
{
BackoffType = DelayBackoffType.Exponential,
UseJitter = true, // Adds a random factor to the delay
MaxRetryAttempts = int.MaxValue,
Delay = TimeSpan.FromSeconds(1),
MaxDelay = TimeSpan.FromSeconds(15),
OnRetry = args =>
{
_logger.LogWarning($"Mqtt Reconnecting Attempt: {args.AttemptNumber} with delay {args.RetryDelay}ms.");
return default;
}
});
//Build the policy
_retryPolicy = retry.Build();
然后我像这样使用它:
private async Task ReConnectAsync()
{
try
{
//Check if we are not already re-connecting
if (_isReconnecting) return;
_isReconnecting = true;
// Polly retry
await _retryPolicy.ExecuteAsync(async x => await ConnectToMqttAsync());
}
catch (Exception exception)
{
_logger.LogError(exception, "failed to retry connecting to mqtt.");
}
finally
{
_isReconnecting = false;
}
}
现在我的问题是我正在等待呼叫,但它没有阻止或等待,它似乎触发了一个执行重试的后台任务。我的问题是现在我有多个 Polly 忙于重试的实例。
如何让它阻止/等待重试策略成功或失败?
答: 暂无答案
评论