From 98e8bbd63d2fce9b07215ccd7a6108245e912129 Mon Sep 17 00:00:00 2001 From: delvedor Date: Tue, 19 Feb 2019 08:30:53 +0100 Subject: [PATCH] Added cloud option and disable dead/alive handling if there is only one node and sniffing is disabled --- index.d.ts | 5 +++++ index.js | 14 +++++++++++++- lib/ConnectionPool.d.ts | 1 + lib/ConnectionPool.js | 7 +++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 0dc7f7a11..dfcf930a1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 { diff --git a/index.js b/index.js index 1fc9aba54..daf209791 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/lib/ConnectionPool.d.ts b/lib/ConnectionPool.d.ts index dd55540fd..8b14ced59 100644 --- a/lib/ConnectionPool.d.ts +++ b/lib/ConnectionPool.d.ts @@ -42,6 +42,7 @@ export default class ConnectionPool { dead: string[]; _ssl: SecureContextOptions | null; _agent: AgentOptions | null; + _sniffEnabled: boolean; resurrectTimeout: number; resurrectTimeoutCutoff: number; pingTimeout: number; diff --git a/lib/ConnectionPool.js b/lib/ConnectionPool.js index e4221a16b..f5e82f401 100644 --- a/lib/ConnectionPool.js +++ b/lib/ConnectionPool.js @@ -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) {