Add ability to disable the http agent (#1251)

This commit is contained in:
Tomas Della Vedova
2020-07-13 15:07:51 +02:00
committed by GitHub
parent 39cf023426
commit 1592c4d575
8 changed files with 117 additions and 13 deletions

View File

@ -119,7 +119,8 @@ _Default:_ `null`
|`agent`
a|`http.AgentOptions, function` - http agent https://nodejs.org/api/http.html#http_new_agent_options[options],
or a function that returns an actual http agent instance. +
or a function that returns an actual http agent instance. If you want to disable the http agent use entirely
(and disable the `keep-alive` feature), set the agent to `false`. +
_Default:_ `null`
[source,js]
----
@ -132,6 +133,12 @@ const client = new Client({
node: 'http://localhost:9200',
agent: () => new CustomAgent()
})
const client = new Client({
node: 'http://localhost:9200',
// Disable agent and keep-alive
agent: false
})
----
|`nodeFilter`

2
index.d.ts vendored
View File

@ -83,7 +83,7 @@ interface ClientOptions {
suggestCompression?: boolean;
compression?: 'gzip';
ssl?: TlsConnectionOptions;
agent?: AgentOptions | agentFn;
agent?: AgentOptions | agentFn | false;
nodeFilter?: nodeFilterFn;
nodeSelector?: nodeSelectorFn | string;
headers?: Record<string, any>;

View File

@ -38,12 +38,13 @@ class Connection {
if (typeof opts.agent === 'function') {
this.agent = opts.agent()
} else if (opts.agent === false) {
this.agent = undefined
} else {
const keepAliveFalse = opts.agent && opts.agent.keepAlive === false
const agentOptions = Object.assign({}, {
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: keepAliveFalse ? Infinity : 256,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo'
}, opts.agent)
@ -146,7 +147,9 @@ class Connection {
if (this._openRequests > 0) {
setTimeout(() => this.close(callback), 1000)
} else {
this.agent.destroy()
if (this.agent !== undefined) {
this.agent.destroy()
}
callback()
}
}

View File

@ -24,7 +24,7 @@
],
"scripts": {
"test": "npm run lint && tap test/{unit,acceptance}/{*,**/*}.test.js && npm run test:types",
"test:node8": "npm run lint && tap test/{unit,acceptance}/*.test.js && npm run test:types",
"test:node8": "npm run lint && tap test/{unit,acceptance}/*.test.js",
"test:unit": "tap test/unit/{*,**/*}.test.js",
"test:acceptance": "tap test/acceptance/*.test.js",
"test:integration": "node test/integration/index.js",
@ -68,7 +68,7 @@
"standard": "^13.0.2",
"stoppable": "^1.1.0",
"tap": "^14.4.1",
"tsd": "^0.12.1",
"tsd": "^0.13.1",
"workq": "^2.1.0",
"xmlbuilder2": "^2.1.2"
},

View File

@ -51,12 +51,14 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 42
})
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: {
url: 'http://localhost:9200',
@ -76,6 +78,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
maxRetries: 'five'
@ -93,6 +96,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
requestTimeout: 'five'
@ -110,6 +114,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
pingTimeout: 'five'
@ -134,6 +139,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
sniffInterval: 'five'
@ -151,6 +157,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
sniffOnStart: 'no'
@ -168,6 +175,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
sniffEndpoint: false
@ -185,6 +193,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
sniffOnConnectionFault: 'yes'
@ -216,6 +225,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
resurrectStrategy: 'custom'
@ -233,6 +243,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
suggestCompression: 'no'
@ -250,6 +261,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
compression: 'deflate'
@ -267,6 +279,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
headers: 'foo=bar'
@ -284,6 +297,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
opaqueIdPrefix: 42
@ -308,6 +322,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
name: 42
@ -349,6 +364,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
auth: 'password'
@ -367,6 +383,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
cloud: {
id: 42
@ -389,12 +406,22 @@ expectType<Client>(
})
)
expectType<Client>(
new Client({
node: 'http://localhost:9200',
agent: false
})
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
agent: {
// @ts-expect-error
keepAlive: 'yes',
// @ts-expect-error
keepAliveMsecs: true,
// @ts-expect-error
maxSockets: 'all',
maxFreeSockets: null
}
@ -418,7 +445,9 @@ expectError<errors.ConfigurationError>(
new Client({
node: 'http://localhost:9200',
ssl: {
// @ts-expect-error
ca: 42,
// @ts-expect-error
rejectUnauthorized: 'yes'
}
})
@ -437,6 +466,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
generateRequestId: 'id'
@ -463,6 +493,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
nodeSelector (connections) {
@ -484,6 +515,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
nodeFilter (connection) {
@ -518,6 +550,7 @@ expectError<errors.ConfigurationError>(
}
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
Serializer: CustomSerializer
@ -553,6 +586,7 @@ expectError<errors.ConfigurationError>(
expectError<errors.ConfigurationError>(
new Client({
node: 'http://localhost:9200',
// @ts-expect-error
Connection: CustomConnection
})
)
@ -584,6 +618,7 @@ expectError<errors.ConfigurationError>(
}
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
ConnectionPool: CustomConnectionPool
@ -617,6 +652,7 @@ expectError<errors.ConfigurationError>(
}
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
Transport: CustomTransport

View File

@ -90,15 +90,10 @@ expectError(
body: 42
}).then(console.log)
)
/*
* We cannot test yet the absence of a property because tsd is still shipping
* TypeScript v3.7, as soon as it will be 3.9, we'll uncomment the following test.
// @ts-expect-error
client.async_search.get()
*/
// callback api is not supported
expectError(client.cat.count({ index: 'test' }, {}, (err: any, result: any) => {}))

View File

@ -1093,6 +1093,32 @@ test('Random selector', t => {
})
})
test('Disable keep alive agent', t => {
t.plan(3)
function handler (req, res) {
t.strictEqual(req.headers.connection, 'close')
res.setHeader('Content-Type', 'application/json;utf=8')
res.end(JSON.stringify({ hello: 'world' }))
}
buildServer(handler, ({ port }, server) => {
const client = new Client({
node: `http://localhost:${port}`,
agent: false
})
client.search({
index: 'test',
q: 'foo:bar'
}, (err, { body }) => {
t.error(err)
t.deepEqual(body, { hello: 'world' })
server.stop()
})
})
})
test('name property as string', t => {
t.plan(1)

View File

@ -198,7 +198,7 @@ test('Disable keep alive', t => {
buildServer(handler, ({ port }, server) => {
const connection = new Connection({
url: new URL(`http://localhost:${port}`),
agent: { keepAlive: false }
agent: false
})
connection.request({
path: '/hello',
@ -499,6 +499,43 @@ test('Should not close a connection if there are open requests', t => {
})
})
test('Should not close a connection if there are open requests (with agent disabled)', t => {
t.plan(4)
function handler (req, res) {
setTimeout(() => res.end('ok'), 1000)
}
buildServer(handler, ({ port }, server) => {
const connection = new Connection({
url: new URL(`http://localhost:${port}`),
agent: false
})
setTimeout(() => {
t.strictEqual(connection._openRequests, 1)
connection.close()
}, 500)
connection.request({
path: '/hello',
method: 'GET'
}, (err, res) => {
t.error(err)
t.strictEqual(connection._openRequests, 0)
var payload = ''
res.setEncoding('utf8')
res.on('data', chunk => { payload += chunk })
res.on('error', err => t.fail(err))
res.on('end', () => {
t.strictEqual(payload, 'ok')
server.stop()
})
})
})
})
test('Url with auth', t => {
t.plan(2)