重试 API¶
Note
如果你正在寻找使用流的重试实现,请查看重试流API。
Tip
你可以在任何钩子中抛出 RetryError
来触发重试。
Tip
afterResponse
钩子公开了一个专用函数,用于使用合并选项进行重试。
了解更多.
retry
¶
类型: object
Default:
This option represents the retry
object.
limit
¶
类型: number
The maximum retry count.
methods
¶
类型: string[]
The allowed methods to retry on.
Note
By default, Got does not retry on POST
.
statusCodes
¶
类型: number[]
Note
Only unsuccessful requests are retried. In order to retry successful requests, use an afterResponse
hook.
The allowed HTTP status codes to retry on.
errorCodes
¶
类型: string[]
The allowed error codes to retry on.
ETIMEDOUT
- One of the timeout limits was reached.ECONNRESET
- The connection was forcibly closed.EADDRINUSE
- Could not bind to any free port.ECONNREFUSED
- The connection was refused by the server.EPIPE
- The remote side of the stream being written has been closed.ENOTFOUND
- Could not resolve the hostname to an IP address.ENETUNREACH
- No internet connection.EAI_AGAIN
- DNS lookup timed out.
maxRetryAfter
¶
类型: number | undefined
默认: options.timeout.request
The upper limit of retry-after
header. If undefined
, it will use options.timeout
as the value.
If the limit is exceeded, the request is canceled.
calculateDelay
¶
类型: Function
The function used to calculate the delay before the next request is made. Returning 0
cancels the retry.
Note
This function is responsible for the entire retry mechanism, including the limit
property. To support this, you need to check if computedValue
is different than 0
.
Tip
This is especially useful when you want to scale down the computed value.
backoffLimit
¶
类型: number
The upper limit of the computedValue
.
By default, the computedValue
is calculated in the following way:
The delay increases exponentially.
In order to prevent this, you can set this value to a fixed value, such as 1000
.
noise
¶
类型: number
The maximum acceptable retry noise in the range of -100
to +100
.