英文:
http retry for Request Timeout (408)
问题
使用HashiCorp的go-retryablehttp
库(https://github.com/hashicorp/go-retryablehttp)
它会自动为所有的5xx
状态码进行重试:
retryablehttp在特定条件下执行自动重试。主要是,如果客户端返回错误(连接错误等),或者收到500范围的响应代码(除了501),则在等待一段时间后会触发重试。否则,响应将被返回,并由调用者解释。
是否可能在Request Timeout
上进行重试,例如在408
HTTP状态码上直接使用库提供的功能?或者我应该构建一些自定义的包装器?
英文:
Using hashicorp go-retryablehttp
library (https://github.com/hashicorp/go-retryablehttp)
It retries automatically for all 5xx
code:<br>
> retryablehttp performs automatic retries under certain conditions. Mainly, if an error is returned by the client (connection errors, etc.), or if a 500-range response code is received (except 501), then a retry is invoked after a wait period. Otherwise, the response is returned and left to the caller to interpret.
Is that possible it retries on Request Timeout
, e.g. on 408
http status code just ootb?<br>
or I should build some custom wrappers?
答案1
得分: 3
你可以实现自己的重试策略并将其传递给Client.CheckRetry字段。
文档参考:
- https://pkg.go.dev/github.com/hashicorp/go-retryablehttp?utm_source=godoc#Client
- https://pkg.go.dev/github.com/hashicorp/go-retryablehttp?utm_source=godoc#DefaultRetryPolicy
代码参考:
- https://github.com/hashicorp/go-retryablehttp/blob/02c1586c8f14be23e7eeb522f1094afbabf45e93/client.go#L401
- https://github.com/hashicorp/go-retryablehttp/blob/02c1586c8f14be23e7eeb522f1094afbabf45e93/client.go#L424
代码可能类似于:
package main
import (
"context"
"net/http"
"github.com/hashicorp/go-retryablehttp"
)
func main() {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 10
retryClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
ok, e := retryablehttp.DefaultRetryPolicy(ctx, resp, err)
if !ok && resp.StatusCode == http.StatusRequestTimeout {
return true, nil
// 如果e为nil,返回true进行重试,
// 你可能想要填充该错误以传播它。
// 参考 https://github.com/hashicorp/go-retryablehttp/blob/02c1586c8f14be23e7eeb522f1094afbabf45e93/client.go#L673
}
return ok, e
}
}
英文:
You can implement your own retry policy and pass it to the Client.CheckRetry field.
Doc ref:
- https://pkg.go.dev/github.com/hashicorp/go-retryablehttp?utm_source=godoc#Client
- https://pkg.go.dev/github.com/hashicorp/go-retryablehttp?utm_source=godoc#DefaultRetryPolicy
Code ref:
- https://github.com/hashicorp/go-retryablehttp/blob/02c1586c8f14be23e7eeb522f1094afbabf45e93/client.go#L401
- https://github.com/hashicorp/go-retryablehttp/blob/02c1586c8f14be23e7eeb522f1094afbabf45e93/client.go#L424
The code might look like something similar to
package main
import (
"context"
"net/http"
"github.com/hashicorp/go-retryablehttp"
)
func main() {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 10
retryClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
ok, e := retryablehttp.DefaultRetryPolicy(ctx, resp, err)
if !ok && resp.StatusCode == http.StatusRequestTimeout {
return true, nil
// return true for a retry,
// if e is nil,
// you might want to populate that error
// to propagate it.
// see https://github.com/hashicorp/go-retryablehttp/blob/02c1586c8f14be23e7eeb522f1094afbabf45e93/client.go#L673
}
return ok, e
}
}
答案2
得分: 1
根据文件client.go中第354行指定的源代码,你可以配置CheckRetry
函数以在任何自定义场景下进行重试。
// CheckRetry specifies the policy for handling retries, and is called
// after each request. The default policy is DefaultRetryPolicy.
CheckRetry CheckRetry
你只需要编写一个符合以下类型的函数,并将retryablehttp.Client.CheckRetry
配置为该自定义实现。
type CheckRetry func(ctx context.Context, resp *http.Response, err error) (bool, error)
英文:
As source code specify in line 354 in file client.go, you can configure the CheckRetry
function to retry in any custom scenario.
// CheckRetry specifies the policy for handling retries, and is called
// after each request. The default policy is DefaultRetryPolicy.
CheckRetry CheckRetry
Only you need to do is write a function in below type and configure the retryablehttp.Client.CheckRetry
with that custom implementation.
type CheckRetry func(ctx context.Context, resp *http.Response, err error) (bool, error)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论