When a service requests a resource, if it encounters a network exception or other situation that causes the request to fail, it needs a retry mechanism to continue the request. A common practice is to retry 3 times and sleep randomly for a few seconds. For business development scaffolding, the HTTP Client basically encapsulates the retry method and automatically retries when the request fails according to the configuration. Here is an example of a common HTTP Client to see how it implements request retry. Finally, the implementation of some other retry mechanisms are organized.
Implementation of go-resty retry mechanism
Let’s look at the implementation of go-resty to request a retry when sending HTTP requests.
Sort out the retry flow of
Execute(method, url) at request time.
- If no retry count is set, execute
r.client.execute(r): request Request directly, return Response and error.
r.client.RetryCountis not equal to 0, execute
Backoff()method takes a handler argument, makes attempt network requests according to the retry policy, and takes function arguments such as
The Backoff function
Focus on what the
Backoff() function does.
Backoff() code is as follows.
Sort out the flow of the
Backoff()receives a handler function and an optional Option function (retry optione) as arguments
- default policy 3 retries, customize retry policy by Step 1 preset Options.
- set the repsonse and error variables of the request
- start the
- execute the handler function (initiate the HTTP request)
- If the return result is not empty and the context is not empty, keep the request context for repsonse. If the context is wrong, exit the
retryConditions(), set the conditions to check for retry.
- determine whether to exit the process based on needsRetry
- calculate the duration by
sleepDuration()(based on the request resp, wait time configuration, maximum timeout and the number of retries to calculate sleepDuration. Time algorithm is relatively complex, refer to: Exponential Backoff And Jitter)
- waitTime for the next retry. If the request completes, exit the process.
A simple demo
See the request for a specific HTTP Client (with a simple wrapper).
According to the go-resty request process sorted out above, since
RetryCount is greater than 0, a retry mechanism is performed with a retry count of 3. Then
request.Get(url) enters the
Backoff() process, where the boundary condition for retry is:
!response.IsSuccess() , until the request succeeds.
Some other implementations of retry mechanisms
As you can see, go-resty’s retry policy is not very simple, it is a well-developed, customizable mechanism that takes HTTP request scenarios into account, and its business attributes are relatively heavy.
Let’s take a look at two common implementations of Retry.
Each retry waits a randomly extended amount of time until
f() completes or until there are no more retry attempts.
The number of retries for the function is
attempts, each time waiting for the sleep time until
f() finishes executing.