This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow passing a http.Agent to ipfs-http-client in node (#3474)
Right now no `http.Agent` is used for requests made using the http client in node, which means each request opens a new connection which can end up hitting process resource limits which means connections get dropped. The change here sets a default `http.Agent` with a `keepAlive: true` and `maxSockets` of 6 which is consistent with [browsers](https://tools.ietf.org/html/rfc2616#section-8.1.4) and [native apps](https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1407597-httpmaximumconnectionsperhost?language=objc). The user can override the agent passed to the `ipfs-http-client` constructor to restore the previous functionality: ```js const http = require('http') const createClient = require('ipfs-http-client') const client = createClient({ url: 'http://127.0.0.1:5002', agent: new http.Agent({ keepAlive: false, maxSockets: Infinity }) }) ``` Refs: #3464
- Loading branch information
1 parent
7b48f14
commit fe93ba0
Showing
8 changed files
with
177 additions
and
19 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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use strict' | ||
|
||
require('./node/agent') | ||
require('./node/swarm') | ||
require('./node/request-api') | ||
require('./node/custom-headers') |
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,111 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const { expect } = require('aegir/utils/chai') | ||
const ipfsClient = require('../../src') | ||
const delay = require('delay') | ||
|
||
function startServer (handler) { | ||
return new Promise((resolve) => { | ||
// spin up a test http server to inspect the requests made by the library | ||
const server = require('http').createServer((req, res) => { | ||
req.on('data', () => {}) | ||
req.on('end', async () => { | ||
const out = await handler(req) | ||
|
||
res.writeHead(200) | ||
res.write(JSON.stringify(out)) | ||
res.end() | ||
}) | ||
}) | ||
|
||
server.listen(0, () => { | ||
resolve({ | ||
port: server.address().port, | ||
close: () => server.close() | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
describe('agent', function () { | ||
let agent | ||
|
||
before(() => { | ||
const { Agent } = require('http') | ||
|
||
agent = new Agent({ | ||
maxSockets: 2 | ||
}) | ||
}) | ||
|
||
it('restricts the number of concurrent connections', async () => { | ||
const responses = [] | ||
|
||
const server = await startServer(() => { | ||
const p = new Promise((resolve) => { | ||
responses.push(resolve) | ||
}) | ||
|
||
return p | ||
}) | ||
|
||
const ipfs = ipfsClient({ | ||
url: `http://localhost:${server.port}`, | ||
agent | ||
}) | ||
|
||
// make three requests | ||
const requests = Promise.all([ | ||
ipfs.id(), | ||
ipfs.id(), | ||
ipfs.id() | ||
]) | ||
|
||
// wait for the first two to arrive | ||
for (let i = 0; i < 5; i++) { | ||
await delay(100) | ||
|
||
if (responses.length === 2) { | ||
// wait a little longer, the third should not arrive | ||
await delay(1000) | ||
|
||
expect(responses).to.have.lengthOf(2) | ||
|
||
// respond to the in-flight requests | ||
responses[0]({ | ||
res: 0 | ||
}) | ||
responses[1]({ | ||
res: 1 | ||
}) | ||
|
||
break | ||
} | ||
} | ||
|
||
// wait for the final request to arrive | ||
for (let i = 0; i < 5; i++) { | ||
await delay(100) | ||
|
||
if (responses.length === 3) { | ||
// respond to it | ||
responses[2]({ | ||
res: 2 | ||
}) | ||
} | ||
} | ||
|
||
const results = await requests | ||
|
||
expect(results).to.deep.equal([{ | ||
res: 0 | ||
}, { | ||
res: 1 | ||
}, { | ||
res: 2 | ||
}]) | ||
|
||
server.close() | ||
}) | ||
}) |