Added cloud option and disable dead/alive handling if there is only one node and sniffing is disabled

This commit is contained in:
delvedor
2019-02-19 08:30:53 +01:00
parent 59a84216b1
commit 98e8bbd63d
4 changed files with 26 additions and 1 deletions

5
index.d.ts vendored
View File

@ -40,6 +40,11 @@ interface ClientOptions {
agent?: AgentOptions;
nodeFilter?: nodeFilterFn;
nodeSelector?: nodeSelectorFn | string;
cloud?: {
id: string;
username: string;
password: string;
}
}
declare class Client extends EventEmitter {

View File

@ -14,6 +14,15 @@ const buildApi = require('./api')
class Client extends EventEmitter {
constructor (opts = {}) {
super()
if (opts.cloud) {
const { id, username, password } = opts.cloud
// the cloud id is `cluster-name:base64encodedurl`
// the url is a string divided by two '$', the first is the cloud url
// the second the elasticsearch instance, the third the kibana instance
const cloudUrls = Buffer.from(id.split(':')[1], 'base64').toString().split('$')
opts.node = `https://${username}:${password}@${cloudUrls[1]}.${cloudUrls[0]}`
}
if (!opts.node && !opts.nodes) {
throw new ConfigurationError('Missing node(s) option')
}
@ -56,7 +65,10 @@ class Client extends EventEmitter {
nodeWeighter: options.nodeWeighter,
nodeSelector: options.nodeSelector,
Connection: options.Connection,
emit: this.emit.bind(this)
emit: this.emit.bind(this),
sniffEnabled: options.sniffInterval !== false ||
options.sniffOnStart !== false ||
options.sniffOnConnectionFault !== false
})
// Add the connections before initialize the Transport

View File

@ -42,6 +42,7 @@ export default class ConnectionPool {
dead: string[];
_ssl: SecureContextOptions | null;
_agent: AgentOptions | null;
_sniffEnabled: boolean;
resurrectTimeout: number;
resurrectTimeoutCutoff: number;
pingTimeout: number;

View File

@ -27,6 +27,7 @@ class ConnectionPool {
this.nodeFilter = opts.nodeFilter || defaultNodeFilter
this.Connection = opts.Connection
this.emit = opts.emit || noop
this._sniffEnabled = opts.sniffEnabled || false
if (typeof opts.nodeSelector === 'function') {
this.nodeSelector = opts.nodeSelector
@ -50,10 +51,13 @@ class ConnectionPool {
* Marks a connection as 'alive'.
* If needed removes the connection from the dead list
* and then resets the `deadCount`.
* If sniffing is not enabled and there is only
* one node, this method is a noop.
*
* @param {object} connection
*/
markAlive (connection) {
if (this._sniffEnabled === false && this.connections.size === 1) return
const { id } = connection
debug(`Marking as 'alive' connection '${id}'`)
const index = this.dead.indexOf(id)
@ -67,10 +71,13 @@ class ConnectionPool {
* Marks a connection as 'dead'.
* If needed adds the connection to the dead list
* and then increments the `deadCount`.
* If sniffing is not enabled and there is only
* one node, this method is a noop.
*
* @param {object} connection
*/
markDead (connection) {
if (this._sniffEnabled === false && this.connections.size === 1) return
const { id } = connection
debug(`Marking as 'dead' connection '${id}'`)
if (this.dead.indexOf(id) === -1) {