Skip to content

Apply patch from elastic/elasticsearch-js#2159 #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,30 @@ export default class Client extends API {
// node in the list.
let node = options.node ?? options.nodes
if (Array.isArray(node)) node = node[0]
this.connectionPool.addConnection(node)

// ensure default connection values are inherited when creating new connections
// see https://github.com/elastic/elasticsearch-js/issues/1791
type ConnectionDefaults = Record<string, any>

const { tls, headers, auth, requestTimeout: timeout, agent, proxy, caFingerprint } = options
let defaults: ConnectionDefaults = { tls, headers, auth, timeout, agent, proxy, caFingerprint }

// strip undefined values from defaults
defaults = Object.keys(defaults).reduce((acc: ConnectionDefaults, key) => {
const val = defaults[key]
if (val !== undefined) acc[key] = val
return acc
}, {})

let newOpts
if (typeof node === 'string') {
newOpts = {
url: new URL(node)
}
} else {
newOpts = node
}
this.connectionPool.addConnection({ ...defaults, ...newOpts })
}

this.transport = new options.Transport({
Expand Down
43 changes: 42 additions & 1 deletion test/unit/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* under the License.
*/

import * as http from 'http'
import { test } from 'tap'
import { URL } from 'url'
import { connection } from '../utils'
import FakeTimers from '@sinonjs/fake-timers'
import { buildServer, connection } from '../utils'
import { Client, errors } from '../..'
import * as symbols from '@elastic/transport/lib/symbols'
import { BaseConnectionPool, CloudConnectionPool } from '@elastic/transport'
Expand Down Expand Up @@ -463,3 +465,42 @@ test('user agent is in the correct format', t => {
t.ok(/^\d+\.\d+\.\d+/.test(agentSplit[0].split('/')[1]))
t.end()
})

test('Ensure new client instance stores requestTimeout for each connection', t => {
const client = new Client({
node: { url: new URL('http://localhost:9200') },
requestTimeout: 60000,
})
t.equal(client.connectionPool.connections[0].timeout, 60000)
t.end()
})

test('Ensure new client does not time out at default (30s) when client sets requestTimeout', async t => {
const clock = FakeTimers.install({ toFake: ['setTimeout', 'clearTimeout'] })
t.teardown(() => clock.uninstall())

function handler (_req: http.IncomingMessage, res: http.ServerResponse) {
setTimeout(() => {
t.pass('timeout ended')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ success: true }))
}, 31000) // default is 30000
clock.runToLast()
}

const [{ port }, server] = await buildServer(handler)

const client = new Client({
node: `http://localhost:${port}`,
requestTimeout: 60000
})

try {
await client.transport.request({ method: 'GET', path: '/' })
} catch (error) {
t.fail('timeout error hit')
} finally {
server.stop()
t.end()
}
})