Skip to content

Commit

Permalink
adds maxRetriesOn503 option
Browse files Browse the repository at this point in the history
  • Loading branch information
leorossi committed Sep 30, 2021
1 parent 4144ca4 commit 1b1f310
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -198,6 +198,17 @@ By default: `['GET', 'HEAD', 'OPTIONS', 'TRACE' ]`

This plugin will always retry on 503 errors, _unless_ `retryMethods` does not contain `GET`.

---

#### `maxRetriesOn503`

This plugin will always retry on `GET` requests that returns 503 errors, _unless_ `retryMethods` does not contain `GET`.

This option set the limit on how many times the plugin should retry the request, specifically for 503 errors.

By Default: 10


---

### `reply.from(source, [opts])`
Expand Down
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -43,6 +43,7 @@ module.exports = fp(function from (fastify, opts, next) {
const getUpstream = opts.getUpstream || upstreamNoOp
const onError = opts.onError || onErrorDefault
const retriesCount = opts.retriesCount || 0
const maxRetriesOn503 = opts.maxRetriesOn503 || 10

if (!source) {
source = req.url
Expand Down Expand Up @@ -119,7 +120,7 @@ module.exports = fp(function from (fastify, opts, next) {
const contentLength = requestHeaders['content-length']
let requestImpl
if (retryMethods.has(req.method) && !contentLength) {
requestImpl = createRequestRetry(request, this, retriesCount, retryOnError)
requestImpl = createRequestRetry(request, this, retriesCount, retryOnError, maxRetriesOn503)
} else {
requestImpl = request
}
Expand Down Expand Up @@ -212,9 +213,8 @@ function isFastifyMultipartRegistered (fastify) {
return fastify.hasContentTypeParser('multipart') && fastify.hasRequestDecorator('multipart')
}

function createRequestRetry (requestImpl, reply, retriesCount, retryOnError) {
function createRequestRetry (requestImpl, reply, retriesCount, retryOnError, maxRetriesOn503) {
function requestRetry (req, cb) {
const MAX_RETRIES_ON_503 = 10
let retries = 0

function run () {
Expand All @@ -228,7 +228,7 @@ function createRequestRetry (requestImpl, reply, retriesCount, retryOnError) {
if (!reply.sent) {
// always retry on 503 errors
if (res && res.statusCode === 503 && req.method === 'GET') {
if (retriesCount === 0 && retries < MAX_RETRIES_ON_503) {
if (retriesCount === 0 && retries < maxRetriesOn503) {
// we should stop at some point
return retry(retryAfter)
}
Expand Down

0 comments on commit 1b1f310

Please sign in to comment.