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._
132 lines
3.5 KiB
TypeScript
132 lines
3.5 KiB
TypeScript
/// <reference types="node" />
|
|
|
|
import { SecureContextOptions } from 'tls';
|
|
import Connection, { AgentOptions } from './Connection';
|
|
import { nodeFilterFn, nodeSelectorFn } from './Transport';
|
|
|
|
interface ConnectionPoolOptions {
|
|
ssl?: SecureContextOptions;
|
|
agent?: AgentOptions;
|
|
pingTimeout?: number;
|
|
Connection: typeof Connection;
|
|
resurrectStrategy?: string;
|
|
}
|
|
|
|
export interface getConnectionOptions {
|
|
filter?: nodeFilterFn;
|
|
selector?: nodeSelectorFn;
|
|
}
|
|
|
|
export interface ResurrectMeta {
|
|
strategy: string;
|
|
isAlive: boolean;
|
|
connection: Connection;
|
|
}
|
|
|
|
export default class ConnectionPool {
|
|
static resurrectStrategies: {
|
|
none: number;
|
|
ping: number;
|
|
optimistic: number;
|
|
};
|
|
connections: any;
|
|
dead: string[];
|
|
_ssl: SecureContextOptions | null;
|
|
_agent: AgentOptions | null;
|
|
_sniffEnabled: boolean;
|
|
resurrectTimeout: number;
|
|
resurrectTimeoutCutoff: number;
|
|
pingTimeout: number;
|
|
Connection: typeof Connection;
|
|
resurrectStrategy: number;
|
|
constructor(opts?: ConnectionPoolOptions);
|
|
/**
|
|
* Marks a connection as 'alive'.
|
|
* If needed removes the connection from the dead list
|
|
* and then resets the `deadCount`.
|
|
*
|
|
* @param {object} connection
|
|
*/
|
|
markAlive(connection: Connection): void;
|
|
/**
|
|
* Marks a connection as 'dead'.
|
|
* If needed adds the connection to the dead list
|
|
* and then increments the `deadCount`.
|
|
*
|
|
* @param {object} connection
|
|
*/
|
|
markDead(connection: Connection): void;
|
|
/**
|
|
* If enabled, tries to resurrect a connection with the given
|
|
* resurrect strategy ('ping', 'optimistic', 'none').
|
|
*
|
|
* @param {number} epoch
|
|
* @param {function} callback (isAlive, connection)
|
|
*/
|
|
resurrect(now?: number, callback?: (isAlive: boolean | null, connection: Connection | null) => void): void;
|
|
/**
|
|
* Returns an alive connection if present,
|
|
* otherwise returns null.
|
|
* By default it filters the `master` only nodes.
|
|
* It uses the selector to choose which
|
|
* connection return.
|
|
*
|
|
* @param {object} options (filter and selector)
|
|
* @returns {object|null} connection
|
|
*/
|
|
getConnection(opts?: getConnectionOptions): Connection | null;
|
|
/**
|
|
* Adds a new connection to the pool.
|
|
*
|
|
* @param {object|string} host
|
|
* @returns {ConnectionPool}
|
|
*/
|
|
addConnection(opts: any): Connection | void;
|
|
/**
|
|
* Removes a new connection to the pool.
|
|
*
|
|
* @param {object} connection
|
|
* @returns {ConnectionPool}
|
|
*/
|
|
removeConnection(connection: Connection): ConnectionPool;
|
|
/**
|
|
* Empties the connection pool.
|
|
*
|
|
* @returns {ConnectionPool}
|
|
*/
|
|
empty(): ConnectionPool;
|
|
/**
|
|
* Update the ConnectionPool with new connections.
|
|
*
|
|
* @param {array} array of connections
|
|
* @returns {ConnectionPool}
|
|
*/
|
|
update(connections: Connection[]): ConnectionPool;
|
|
/**
|
|
* Transforms the nodes objects to a host object.
|
|
*
|
|
* @param {object} nodes
|
|
* @returns {array} hosts
|
|
*/
|
|
nodesToHost(nodes: any): any[];
|
|
/**
|
|
* Transforms an url string to a host object
|
|
*
|
|
* @param {string} url
|
|
* @returns {object} host
|
|
*/
|
|
urlToHost(url: string): any;
|
|
}
|
|
|
|
declare function defaultNodeFilter(node: Connection): boolean;
|
|
declare function roundRobinSelector(): (connections: Connection[]) => Connection;
|
|
declare function randomSelector(connections: Connection[]): Connection;
|
|
|
|
export declare const internals: {
|
|
defaultNodeFilter: typeof defaultNodeFilter;
|
|
roundRobinSelector: typeof roundRobinSelector;
|
|
randomSelector: typeof randomSelector;
|
|
};
|
|
|
|
export {};
|