Retry
Hertz provides users with customized retry logic, Let’s take a look at how to use Client Retry. Note: Hertz version >= v0.4.0
Retry times and delay policy configuration
First create a Client, and use the configuration item WithRetryConfig()
to configure the Retry related logic. (This section mainly configures the times of retry and the delay policy)
Configuration Name | Type | Description |
---|---|---|
WithMaxAttemptTimes | uint | Set the maximum attempts times. Default:1 times (That is, only request once without retry) |
WithInitDelay | time.Duration | Set initial delay time. Default: 1ms |
WithMaxDelay | time.Duration | Set the maximum delay time. Default: 100ms |
WithMaxJitter | time.Duration | Set the maximum jitter time, which needs to be used in conjunction with RandomDelayPolicy , and will generate a random time not exceeding the maximum jitter time. Default: 20ms |
WithDelayPolicy | type DelayPolicyFunc func(attempts uint, err error, retryConfig *Config) time.Duration | Set the delay policy, you can use any combination of the following four policies, FixedDelayPolicy , BackOffDelayPolicy , RandomDelayPolicy , DefaultDelayPolicy ( See the next section Delay Policy for details ) . Default: DefaultDelayPolicy (That is, the retry delay is 0) |
Delay Policy
retry.WithDelayPolicy()
usage
Function Name | Description |
---|---|
CombineDelay | It is used to combine any of the following four policies and sum the values calculated by the selected policy. When you only need one of the following four policies, you can choose to use CombineDelay or directly pass any policy into WithDelayPolicy as a parameter |
FixedDelayPolicy | Set the fixed delay time and use the value set by WithInitDelay to generate an equivalent delay time |
BackOffDelayPolicy | Set the exponential delay time. Use the value set by WithInitDelay . The exponential delay time is generated according to the number of retries currently |
RandomDelayPolicy | Set the random delay time. Use the value set by WithMaxJitte r to generate a random delay time that does not exceed this value |
DefaultDelayPolicy | Set the default delay time (That is, 0) . Generally, it is used alone and has no effect when combined with other policies |
Complete example
Retry condition configuration
If you want to customize the conditions under which retries occur, you can use client SetRetryIfFunc()
configuration. The parameter of this function is a function, and the signature is:
Relevant parameters include the req
, resp
and err
fields in the Hertz request. You can use these parameters to determine whether the request should be retried. In the following example, when the status code returned by the request is not 200 or err!=nil
during the call, we return true, that is, we retry.
Note that if you do not set client SetRetryIfFunc()
. We will judge according to Hertz’s default retry conditions, that is, whether the request meets the following DefaultRetryIf()
function and whether the call is idempotent. ( Idempotent call: when canIdempotentRetry
is true in pkg/protocol/http1/client.go::Do() and pkg/protocol/http1/client.go::doNonNilReqResp() )
Table - 1 When canIdempotentRetry
in Hertz source code doNonNilReqResp() is true.
doNonNilReqResp() return true |
---|
err = conn.SetWriteDeadline(currentTime.Add(c.WriteTimeout)) |
err = reqI.Write(req, zw) |
err = reqI.ProxyWrite(req, zw) |
err = zw.Flush() |
err = conn.SetReadTimeout(c.ReadTimeout) |
( err = respI.ReadHeaderAndLimitBody() || err = respI.ReadBodyStream() ) && (err != errs.ErrBodyTooLarge) |