请求超时(408)的情况下进行HTTP重试。

huangapple go评论72阅读模式
英文:

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字段。

文档参考:

代码参考:

代码可能类似于:

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:

Code ref:

The code might look like something similar to

package main

import (
	&quot;context&quot;
	&quot;net/http&quot;

	&quot;github.com/hashicorp/go-retryablehttp&quot;
)

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 &amp;&amp; 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)

huangapple
  • 本文由 发表于 2021年8月3日 20:57:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/68636506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定