Child client support (#768)
With this pr we introduce the `client.child` API, which returns a new client instance that shares the connection pool with the parent client.
This feature can be handy if you need to have multiple client instances with different configurations, but with a shared connection pool.
Example:
```js
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
const child = client.child({
headers: { 'x-foo': 'bar' },
requestTimeout: 1000
})
client.info(console.log)
child.info(console.log)
```
**Open questions:**
* Currently, the event emitter is shared between the parent and the child(ren), is it ok?
* Currently, if you extend the parent client, the child client will have the same extensions, while if the child client adds an extension, the parent client will not be extended. Is it ok?
**Caveats:**
* You can override _any_ option except for the connection pool specific options (`ssl`, `agent`, `pingTimeout`, `Connection`, and `resurrectStrategy`).
* You can't specify a new `Connection` class.
* If you call `close` in any of the parent/child clients, every client will be closed.
_Note: the `nodeFilter` and `nodeSelector` options are now `Transport` options and no longer `ConnectionPool` options._
This commit is contained in:
committed by
GitHub
parent
66e8d61476
commit
5b856cd4c2
@ -4,6 +4,7 @@ const { test } = require('tap')
|
||||
const { URL } = require('url')
|
||||
const ConnectionPool = require('../../lib/ConnectionPool')
|
||||
const Connection = require('../../lib/Connection')
|
||||
const { defaultNodeFilter, roundRobinSelector } = require('../../lib/Transport').internals
|
||||
const { connection: { MockConnection, MockConnectionTimeout } } = require('../utils')
|
||||
|
||||
test('API', t => {
|
||||
@ -227,22 +228,6 @@ test('API', t => {
|
||||
pool.getConnection({ filter })
|
||||
})
|
||||
|
||||
t.test('filter as ConnectionPool option', t => {
|
||||
t.plan(3)
|
||||
|
||||
const href1 = 'http://localhost:9200/'
|
||||
const href2 = 'http://localhost:9200/other'
|
||||
const pool = new ConnectionPool({
|
||||
Connection,
|
||||
nodeFilter: node => {
|
||||
t.ok('called')
|
||||
return true
|
||||
}
|
||||
})
|
||||
pool.addConnection([href1, href2])
|
||||
t.strictEqual(pool.getConnection().id, href1)
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
@ -498,27 +483,16 @@ test('API', t => {
|
||||
test('Node selector', t => {
|
||||
t.test('round-robin', t => {
|
||||
t.plan(1)
|
||||
const pool = new ConnectionPool({ Connection, nodeSelector: 'round-robin' })
|
||||
const pool = new ConnectionPool({ Connection })
|
||||
pool.addConnection('http://localhost:9200/')
|
||||
t.true(pool.getConnection() instanceof Connection)
|
||||
t.true(pool.getConnection({ selector: roundRobinSelector() }) instanceof Connection)
|
||||
})
|
||||
|
||||
t.test('random', t => {
|
||||
t.plan(1)
|
||||
const pool = new ConnectionPool({ Connection, nodeSelector: 'random' })
|
||||
const pool = new ConnectionPool({ Connection })
|
||||
pool.addConnection('http://localhost:9200/')
|
||||
t.true(pool.getConnection() instanceof Connection)
|
||||
})
|
||||
|
||||
t.test('custom function', t => {
|
||||
t.plan(2)
|
||||
const nodeSelector = connections => {
|
||||
t.ok('called')
|
||||
return connections[0]
|
||||
}
|
||||
const pool = new ConnectionPool({ Connection, nodeSelector })
|
||||
pool.addConnection('http://localhost:9200/')
|
||||
t.true(pool.getConnection() instanceof Connection)
|
||||
t.true(pool.getConnection({ selector: roundRobinSelector() }) instanceof Connection)
|
||||
})
|
||||
|
||||
t.end()
|
||||
@ -529,7 +503,7 @@ test('Node filter', t => {
|
||||
t.plan(1)
|
||||
const pool = new ConnectionPool({ Connection })
|
||||
pool.addConnection({ url: new URL('http://localhost:9200/') })
|
||||
t.true(pool.getConnection() instanceof Connection)
|
||||
t.true(pool.getConnection({ filter: defaultNodeFilter }) instanceof Connection)
|
||||
})
|
||||
|
||||
t.test('Should filter master only nodes', t => {
|
||||
@ -544,29 +518,7 @@ test('Node filter', t => {
|
||||
ml: false
|
||||
}
|
||||
})
|
||||
t.strictEqual(pool.getConnection(), null)
|
||||
})
|
||||
|
||||
t.test('custom', t => {
|
||||
t.plan(2)
|
||||
const nodeFilter = node => {
|
||||
t.ok('called')
|
||||
return true
|
||||
}
|
||||
const pool = new ConnectionPool({ Connection, nodeFilter })
|
||||
pool.addConnection({ url: new URL('http://localhost:9200/') })
|
||||
t.true(pool.getConnection() instanceof Connection)
|
||||
})
|
||||
|
||||
t.test('custom (filter)', t => {
|
||||
t.plan(2)
|
||||
const nodeFilter = node => {
|
||||
t.ok('called')
|
||||
return false
|
||||
}
|
||||
const pool = new ConnectionPool({ Connection, nodeFilter })
|
||||
pool.addConnection({ url: new URL('http://localhost:9200/') })
|
||||
t.strictEqual(pool.getConnection(), null)
|
||||
t.strictEqual(pool.getConnection({ filter: defaultNodeFilter }), null)
|
||||
})
|
||||
|
||||
t.end()
|
||||
|
||||
Reference in New Issue
Block a user