Skip to content

Commit 3360c03

Browse files
authored
fix: Ensure new connections inherit client's set defaults
* Apply patch from elastic/elasticsearch-js#2159 * Ensure new connections inherit client's set defaults --------- Co-authored-by: JoshMock <[email protected]>
1 parent 5784f02 commit 3360c03

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/client.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,30 @@ export default class Client extends API {
230230
// node in the list.
231231
let node = options.node ?? options.nodes
232232
if (Array.isArray(node)) node = node[0]
233-
this.connectionPool.addConnection(node)
233+
234+
// ensure default connection values are inherited when creating new connections
235+
// see https://github.com/elastic/elasticsearch-js/issues/1791
236+
type ConnectionDefaults = Record<string, any>
237+
238+
const { tls, headers, auth, requestTimeout: timeout, agent, proxy, caFingerprint } = options
239+
let defaults: ConnectionDefaults = { tls, headers, auth, timeout, agent, proxy, caFingerprint }
240+
241+
// strip undefined values from defaults
242+
defaults = Object.keys(defaults).reduce((acc: ConnectionDefaults, key) => {
243+
const val = defaults[key]
244+
if (val !== undefined) acc[key] = val
245+
return acc
246+
}, {})
247+
248+
let newOpts
249+
if (typeof node === 'string') {
250+
newOpts = {
251+
url: new URL(node)
252+
}
253+
} else {
254+
newOpts = node
255+
}
256+
this.connectionPool.addConnection({ ...defaults, ...newOpts })
234257
}
235258

236259
this.transport = new options.Transport({

test/unit/client.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
* under the License.
1818
*/
1919

20+
import * as http from 'http'
2021
import { test } from 'tap'
2122
import { URL } from 'url'
22-
import { connection } from '../utils'
23+
import FakeTimers from '@sinonjs/fake-timers'
24+
import { buildServer, connection } from '../utils'
2325
import { Client, errors } from '../..'
2426
import * as symbols from '@elastic/transport/lib/symbols'
2527
import { BaseConnectionPool, CloudConnectionPool } from '@elastic/transport'
@@ -463,3 +465,42 @@ test('user agent is in the correct format', t => {
463465
t.ok(/^\d+\.\d+\.\d+/.test(agentSplit[0].split('/')[1]))
464466
t.end()
465467
})
468+
469+
test('Ensure new client instance stores requestTimeout for each connection', t => {
470+
const client = new Client({
471+
node: { url: new URL('http://localhost:9200') },
472+
requestTimeout: 60000,
473+
})
474+
t.equal(client.connectionPool.connections[0].timeout, 60000)
475+
t.end()
476+
})
477+
478+
test('Ensure new client does not time out at default (30s) when client sets requestTimeout', async t => {
479+
const clock = FakeTimers.install({ toFake: ['setTimeout', 'clearTimeout'] })
480+
t.teardown(() => clock.uninstall())
481+
482+
function handler (_req: http.IncomingMessage, res: http.ServerResponse) {
483+
setTimeout(() => {
484+
t.pass('timeout ended')
485+
res.setHeader('content-type', 'application/json')
486+
res.end(JSON.stringify({ success: true }))
487+
}, 31000) // default is 30000
488+
clock.runToLast()
489+
}
490+
491+
const [{ port }, server] = await buildServer(handler)
492+
493+
const client = new Client({
494+
node: `http://localhost:${port}`,
495+
requestTimeout: 60000
496+
})
497+
498+
try {
499+
await client.transport.request({ method: 'GET', path: '/' })
500+
} catch (error) {
501+
t.fail('timeout error hit')
502+
} finally {
503+
server.stop()
504+
t.end()
505+
}
506+
})

0 commit comments

Comments
 (0)