-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retry connection on ECONNRESET (#167)
* Retry connection on ECONNRESET * Restrict retry methods * do not retry on req with body * add tests * support undici * rename retry test * Remove PUT & DELETE and add docs * Update README.md * Fix readme Co-authored-by: Artur Koshtei <[email protected]> Co-authored-by: Artur Koshtei <[email protected]> Co-authored-by: Artur Koshtei <[email protected]> Co-authored-by: Artur Koshtei <[email protected]>
- Loading branch information
1 parent
dd28b58
commit 98c8009
Showing
5 changed files
with
278 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
'use strict' | ||
|
||
const Fastify = require('fastify') | ||
const From = require('..') | ||
const got = require('got') | ||
const { test } = require('tap') | ||
|
||
let retryNum = 1 | ||
|
||
const target = require('http').createServer(function (req, res) { | ||
if (retryNum % 2 !== 0) { | ||
req.socket.destroy() | ||
} else { | ||
res.statusCode = 200 | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.end('hello world') | ||
} | ||
|
||
retryNum += 1 | ||
}) | ||
|
||
test('Will retry', async function (t) { | ||
t.teardown(() => { retryNum = 1 }) | ||
|
||
await target.listen(0) | ||
t.teardown(target.close.bind(target)) | ||
|
||
const instance = Fastify() | ||
|
||
instance.register(From, { http: true }) | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
retriesCount: 1, | ||
onError: (reply, { error }) => { | ||
t.equal(error.code, 'ECONNRESET') | ||
reply.send(error) | ||
} | ||
}) | ||
}) | ||
|
||
await instance.listen(0) | ||
t.teardown(instance.close.bind(instance)) | ||
|
||
const { statusCode } = await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 }) | ||
t.equal(statusCode, 200) | ||
}) | ||
|
||
test('will not retry', async function (t) { | ||
t.teardown(() => { retryNum = 1 }) | ||
|
||
await target.listen(0) | ||
t.teardown(target.close.bind(target)) | ||
|
||
const instance = Fastify() | ||
|
||
instance.register(From, { http: true }) | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
retriesCount: 0, | ||
onError: (reply, { error }) => { | ||
t.equal(error.code, 'ECONNRESET') | ||
reply.send(error) | ||
} | ||
}) | ||
}) | ||
|
||
await instance.listen(0) | ||
t.teardown(instance.close.bind(instance)) | ||
|
||
try { | ||
await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 }) | ||
t.fail() | ||
} catch (err) { | ||
t.equal(err.response.statusCode, 500) | ||
} | ||
}) | ||
|
||
test('will not retry unsupported method', async function (t) { | ||
t.teardown(() => { retryNum = 1 }) | ||
|
||
await target.listen(0) | ||
t.teardown(target.close.bind(target)) | ||
|
||
const instance = Fastify() | ||
|
||
instance.register(From, { http: true, retryMethods: ['DELETE'] }) | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
retriesCount: 1, | ||
onError: (reply, { error }) => { | ||
t.equal(error.code, 'ECONNRESET') | ||
reply.send(error) | ||
} | ||
}) | ||
}) | ||
|
||
await instance.listen(0) | ||
t.teardown(instance.close.bind(instance)) | ||
|
||
try { | ||
await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 }) | ||
t.fail() | ||
} catch (err) { | ||
t.equal(err.response.statusCode, 500) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
'use strict' | ||
|
||
const Fastify = require('fastify') | ||
const From = require('..') | ||
const got = require('got') | ||
const { test } = require('tap') | ||
|
||
let retryNum = 1 | ||
|
||
const target = require('http').createServer(function (req, res) { | ||
if (retryNum % 2 !== 0) { | ||
req.socket.destroy() | ||
} else { | ||
res.statusCode = 200 | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.end('hello world') | ||
} | ||
|
||
retryNum += 1 | ||
}) | ||
|
||
test('Will retry', async function (t) { | ||
t.teardown(() => { retryNum = 1 }) | ||
|
||
await target.listen(0) | ||
t.teardown(target.close.bind(target)) | ||
|
||
const instance = Fastify() | ||
|
||
instance.register(From, { undici: true }) | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
retriesCount: 1, | ||
onError: (reply, { error }) => { | ||
t.equal(error.code, 'UND_ERR_SOCKET') | ||
reply.send(error) | ||
} | ||
}) | ||
}) | ||
|
||
await instance.listen(0) | ||
t.teardown(instance.close.bind(instance)) | ||
|
||
const { statusCode } = await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 }) | ||
t.equal(statusCode, 200) | ||
}) | ||
|
||
test('will not retry', async function (t) { | ||
t.teardown(() => { retryNum = 1 }) | ||
|
||
await target.listen(0) | ||
t.teardown(target.close.bind(target)) | ||
|
||
const instance = Fastify() | ||
|
||
instance.register(From, { undici: true }) | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
retriesCount: 0, | ||
onError: (reply, { error }) => { | ||
t.equal(error.code, 'UND_ERR_SOCKET') | ||
reply.send(error) | ||
} | ||
}) | ||
}) | ||
|
||
await instance.listen(0) | ||
t.teardown(instance.close.bind(instance)) | ||
|
||
try { | ||
await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 }) | ||
t.fail() | ||
} catch (err) { | ||
t.equal(err.response.statusCode, 500) | ||
} | ||
}) | ||
|
||
test('will not retry unsupported method', async function (t) { | ||
t.teardown(() => { retryNum = 1 }) | ||
|
||
await target.listen(0) | ||
t.teardown(target.close.bind(target)) | ||
|
||
const instance = Fastify() | ||
|
||
instance.register(From, { undici: true, retryMethods: ['DELETE'] }) | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
retriesCount: 1, | ||
onError: (reply, { error }) => { | ||
t.equal(error.code, 'UND_ERR_SOCKET') | ||
reply.send(error) | ||
} | ||
}) | ||
}) | ||
|
||
await instance.listen(0) | ||
t.teardown(instance.close.bind(instance)) | ||
|
||
try { | ||
await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 }) | ||
t.fail() | ||
} catch (err) { | ||
t.equal(err.response.statusCode, 500) | ||
} | ||
}) |