我对 C# 编码非常陌生,我只想知道如果函数失败,如何为我的函数设置 polly WaitAndRetry。以下是我的步骤
public async Task<string> ConfigInsert(config model)
{
try
{
SendToDatabase(model);
await Policy.Handle<Exception>()
.RetryAsync(NUMBER_OF_RETRIES)
.ExecuteAsync(async () =>
await SendToDatabase(model))
.ConfigureAwait(false);
}
Catch(Exception e)
{
_log.write("error occurred");
}
public async Task<string> SendToDataBase(config model)
{
var ss = DataBase.PostCallAsync(model)
.GetAwaiter()
.GetResult();
return ss;
}
}
Run Code Online (Sandbox Code Playgroud)
但这通电话却是连续不断地呼叫着,没有任何的延迟。我尝试在 catch 调用中使用 WaitAndRetryAsync 但它不起作用。WaitAndRetryAsync 仅接受 HTTP 休息消息。我想在 try-catch 中实现 ait 和重试选项
我正在尝试创建一个可以按以下方式工作的功能:
我已经尝试调试重试函数有一段时间了,但它不起作用,并且在第一次重试后基本上停止了(即使我将尝试次数指定为 100)。我可以做什么来确保它不断重试拉取记录?
代码如下:
// RETRY FUNCTION
func retry(attempts int, sleep time.Duration, f func() error) (err error) {
for i := 0; ; i++ {
err = f()
if err == nil {
return
}
if i >= (attempts - 1) {
break
}
time.Sleep(sleep)
sleep *= 2
log.Println("retrying after error:", err)
}
return fmt.Errorf("after %d attempts, last error: %s", attempts, err) }
//Save Data function
type Records struct { …
Run Code Online (Sandbox Code Playgroud) 我试图让 Polly 在 3 秒后超时以及返回某些 http 代码时再次尝试。但是,直到 100 秒后 HttpClient 超时后才会超时。
这是我的代码:
private static Polly.Wrap.AsyncPolicyWrap<HttpResponseMessage> GetPolicy()
{
var timeoutPolicy = Policy.TimeoutAsync(3, Polly.Timeout.TimeoutStrategy.Optimistic);
var retryPolicy = Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r =>
r.StatusCode == HttpStatusCode.TooManyRequests ||
r.StatusCode == HttpStatusCode.ServiceUnavailable ||
r.StatusCode == HttpStatusCode.Forbidden)
.WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(3));
var policy = retryPolicy.WrapAsync(timeoutPolicy);
return policy;
}
Run Code Online (Sandbox Code Playgroud)
更新
根据要求,这是我使用该策略的代码。
var pollyResponse = await GetPolicy().ExecuteAndCaptureAsync(() =>
httpClient.SendAsync(GetMessage(HttpMethod.Delete, endpoint))
);
Run Code Online (Sandbox Code Playgroud)
以及生成 HttpRequestMessage 的辅助方法:
private HttpRequestMessage GetMessage<T>(HttpMethod method, string endpoint, T content)
{
var message = new HttpRequestMessage
{ …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用标准 SQS 调用 lambda 函数。我已经使用 try-catch 块处理了错误,每当捕获到错误时,就会返回该错误。否则,将返回 200 OK 响应消息。
我想重新处理返回错误的消息。但 lambda 不会重新处理这些消息。即使保留时间段(5 分钟)> 可见性超时(1 分钟) 为什么会发生这种情况?
const { spawnSync, execSync } = require('child_process');
const fs = require('fs');
const { S3Client, GetObjectCommand, PutObjectCommand } = require("@aws-sdk/client-s3");
const { DynamoDBClient, UpdateItemCommand } = require("@aws-sdk/client-dynamodb");
const { marshall } = require('@aws-sdk/util-dynamodb');
exports.lambdaHandler = async (event) => {
try {
const body = JSON.parse(event["Records"][0]['body']);
try {
// Code base 1
// All the above imported dependencies will be used here
const response = …
Run Code Online (Sandbox Code Playgroud) 当出现 HttpRequestException、5XX 和 408 错误时,下面的代码重试 2 次,并且工作正常。
这里我想重试一下是否有401
错误?我们如何通过 Polly 实现这一目标?
services.AddHttpClient(Options.DefaultName)
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { })
.AddPolicyHandler(HttpPolicyExtensions.HandleTransientHttpError()
.WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));
Run Code Online (Sandbox Code Playgroud) retry-logic ×5
c# ×3
polly ×3
amazon-sqs ×1
asp.net-core ×1
async-await ×1
aws-lambda ×1
go ×1
timeout ×1