我在DefaultRetryPolicy中将 1500 设置为initialTimeoutMs如下,但它不考虑超时:
request.setRetryPolicy(new DefaultRetryPolicy(1500
, DefaultRetryPolicy.DEFAULT_MAX_RETRIES
, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Run Code Online (Sandbox Code Playgroud)
我断开了设备上的 WiFi 以测试它的超时,我在Logcat 中看到了这些时间:
2019-12-16 14:28:15.892 I/MyClass: request sent
2019-12-16 14:28:35.930 I/MyClass: request caught onError
Run Code Online (Sandbox Code Playgroud)
我期望在 1.5 秒后捕获 onResponse 或 onError 花了 20 多秒!!!
我正在使用一个非常脆弱的 API。有时我500 Server Error
用Timeout
,其他时间我还可以得到500 Server Error
,因为我给它输入,它不能处理SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
。
这两种情况都给了我,HttpRequestException
但我可以查看来自服务器的回复消息并确定异常的原因。如果是超时错误,我应该再试一次。如果这是一个错误的输入,我应该重新抛出异常,因为再多的重试也无法解决错误数据的问题。
我想对 Polly 做的是在尝试重试之前检查响应消息。但是到目前为止我看到的所有样本都只包含异常类型。
到目前为止,我想出了这个:
HttpResponseMessage response = null;
String stringContent = null;
Policy.Handle<FlakyApiException>()
.WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
async (exception, timeSpan, context) =>
{
response = await client.PostAsync(requestUri, new StringContent(serialisedParameters, Encoding.UTF8, "application/json"));
stringContent = await response.Content.ReadAsStringAsync();
if (response.StatusCode == HttpStatusCode.InternalServerError && stringContent.Contains("Timeout"))
{
throw new FlakyApiException(stringContent);
}
});
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来进行这种检查?
我有一个使用 Spring Batch 3.0.6 的应用程序。Spring Batch 作业使用块处理。提交间隔设置为 100,重试限制设置为 5。
当达到提交间隔时,写入器将数据块(100 条记录)写入另一个应用程序(通过 TCP)。如果其中一条记录损坏(无论出于何种原因导致异常),则 Spring Batch 会尝试写入该块 5 次(重试限制),并且每次都会失败(如预期)。
然后,Spring Batch 通过每次重试一条记录(即提交间隔 = 1)来尝试找出块中 100 条记录中的哪一条失败。这似乎是重试的默认行为。
有没有一种方法可以提高效率,例如一次重试 10 条记录,并逐渐缩小重试大小,直到找到有问题的记录。这样,如果第 95 条记录是坏的,那么我们发现它比从记录 1 开始一次检查一条记录要快。
我发现了这篇2011年的帖子:
有没有办法让 SB 首先尝试较小的块来缩小坏记录的位置。较小的块大小(例如,10 个项目)可以是可选的用户可配置项目,之后 SB 可以一次尝试一个项目来查找坏记录。
我们正在使用Azure表存储,并且在执行InsertOrMerge操作时偶尔会遇到408超时.在这种情况下,我们想重试,但似乎没有遵循这些错误的重试策略.
这是我们用来处理表交互的类.GetFooEntityAsync方法尝试从表存储中检索实体.如果不能,则创建一个新的FooEntity并将其添加到表中(映射到FooTableEntity).
public class FooTableStorageBase
{
private readonly string tableName;
protected readonly CloudStorageAccount storageAccount;
protected TableRequestOptions DefaultTableRequestOptions { get; }
protected OperationContext DefaultOperationContext { get; }
public CloudTable Table
{
get
{
return storageAccount.CreateCloudTableClient().GetTableReference(tableName);
}
}
public FooTableStorage(string tableName)
{
if (String.IsNullOrWhiteSpace(tableName))
{
throw new ArgumentNullException(nameof(tableName));
}
this.tableName = tableName;
storageAccount = CloudStorageAccount.Parse(ConnectionString);
ServicePoint tableServicePoint = ServicePointManager.FindServicePoint(storageAccount.TableEndpoint);
tableServicePoint.UseNagleAlgorithm = false;
tableServicePoint.ConnectionLimit = 100; // Increasing connection limit from default of 2.
DefaultTableRequestOptions = new TableRequestOptions()
{
PayloadFormat = TablePayloadFormat.JsonNoMetadata,
MaximumExecutionTime …
Run Code Online (Sandbox Code Playgroud) Spring 的 @Retryable 注释将重试 3 次(默认)并回退到 @Recovery 方法。然而@CircuitBreaker,将重试一次并在状态关闭时回退。
我想将这两者结合起来:当断路器状态为闭合时,会在回落之前重试3次(以处理瞬态错误),如果状态为打开,将直接回落。
有什么优雅的方法可以做到这一点吗?一种可能的方法是在函数内部实现重试逻辑,但我觉得这不是最好的解决方案。
我正在尝试将我现有的函数转换为 Polly Retry 策略
public static T Execute<T>(Func<T> getTask) where T : Task
{
var retryCount = 3;
while (retryCount-- > 0)
{
try
{
getTask().Wait();
return getTask();
} catch(Exception ex){
// handle retry
}
}
}
Run Code Online (Sandbox Code Playgroud)
转换成这个
public static T Execute<T>(Func<T> func) where T : Task
{
var task = func();
Policy.Handle<HttpRequestException>()
.Or<TimeoutException>()
.WaitAndRetryAsync(
3,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(5, retryAttempt)),
(exception, timeSpan, retryCount, context) =>
{
//do some logging
})
.ExecuteAsync(func).Wait();
return task;
}
Run Code Online (Sandbox Code Playgroud)
和测试代码是
var retryCount = 0;
var …
Run Code Online (Sandbox Code Playgroud) 我的工作,我已经使用了Spring Batch的应用RetryTemplate
有SimpleRetryPolicy
。
在此应用程序中,ItemProcessor
通常需要30-35分钟才能完成特定任务。但是有时候,完成同一任务需要不到2个小时。
ItemProcessor
如果分配的任务在给定时间内未完成,是否可以重试我的方法?
我正在寻找一些Java / Spring内置功能,而不是编写自己的超时逻辑。
spring spring-batch spring-retry retrypolicy spring-batch-job-monitoring
我一直在阅读2013年4月推出的事件驱动消息编程模型,OnMessageOptions.ExceptionReceived 事件,内置RetryPolicy(2013年5月,RetryPolicy.Default),瞬态故障处理应用程序块(2011)已过时等等(见下).
我一直在监视通过消息泵收到的异常错误,我每天都会收到MessagingCommunicationExceptions.此文章(更新日期:2014年9月16日),提出以下建议:
此异常表示当无法成功建立从消息传递客户端到Service Bus基础结构的连接时可能出现的通信错误.在大多数情况下,如果存在网络连接,则可以将此错误视为瞬态错误.客户端可以尝试重试导致此类异常的操作.还建议您验证域名解析服务(DNS)是否可操作,因为此错误可能表示无法解析目标主机名.
我的理解是,在版本2.1(2013)之后,没有额外的代码可用于处理服务总线上的瞬态错误.除非我的前提是错误的,为什么我每天都会收到瞬态错误?是否应忽略通过消息泵收到的异常?如果忽略,我只能假设意外的异常也会被忽略..我当然不希望这种情况发生.
Microsoft.ServiceBus的版本是2.4.0.0
同样感兴趣:从1.x的升级Windows Azure的服务总线2.0 -重试政策,介绍了Windows Azure的服务总线事件驱动的消息编程模型,是什么在Azure的SDK 2.0版本(2013年4月)的新功能,什么是新Service Bus 2.1版本(2013年5月),瞬态故障处理.
以高效的方式写入Azure Queue Storage的正确调用/代码模式是什么?
现在,伪代码是
使用StorageCredentials和CloudStorage帐户属性创建静态类.在应用程序启动时,将配置文件中的值读入{get;} - 仅属性.
使用具有我的应用程序消息类型的输入参数的异步Task方法创建类.该方法序列化类型,创建新的CloudQueueMessage,新的CloudQueueClient,新的CloudQueue引用.在需要配置信息的地方,从静态类中读取.我的代码然后:
await Task.Run( ()=> theref.AddMessage(themessage).
Run Code Online (Sandbox Code Playgroud)
它看起来好像我在代码中有一些冗余,并且不确定是否/如何将连接汇集到队列中,以及我是否需要重试逻辑,就像我对数据库(SQL Server等)连接一样.
我试图了解哪些队列访问步骤可以以任何方式减少或优化.
所有想法都赞赏.
使用.NET 4.5.2,C#.代码正在Cloud Service(工作者角色)中执行.
谢谢.
我采纳RequestAdapter
并RequestRetrier
自动对我的要求补充更新凭证的行为。重试的问题在不受控制的情况下会无限期地发生。我有以下代码:
class AuthHandler: RequestAdapter, RequestRetrier {
func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
return urlRequest
}
public func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 401 {
_ = Credential.current.renew({ (credential, error) in
completion(true, 0.0) /// Retry request
})
} else {
completion(false, 0.0) // Don't retry
}
}
}
Run Code Online (Sandbox Code Playgroud)
失败后如何只重试一次请求?
我尝试了这个:
request.retryCount = 1
Run Code Online (Sandbox Code Playgroud)
但是行不通...有什么主意吗?
谢谢!
retrypolicy ×10
azure ×3
c# ×3
java ×3
spring ×3
spring-retry ×3
polly ×2
spring-batch ×2
alamofire ×1
android ×1
count ×1
policy ×1
queue ×1
request ×1