Added cloud option and disable dead/alive handling if there is only one node and sniffing is disabled
This commit is contained in:
5
index.d.ts
vendored
5
index.d.ts
vendored
@ -40,6 +40,11 @@ interface ClientOptions {
|
|||||||
agent?: AgentOptions;
|
agent?: AgentOptions;
|
||||||
nodeFilter?: nodeFilterFn;
|
nodeFilter?: nodeFilterFn;
|
||||||
nodeSelector?: nodeSelectorFn | string;
|
nodeSelector?: nodeSelectorFn | string;
|
||||||
|
cloud?: {
|
||||||
|
id: string;
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
declare class Client extends EventEmitter {
|
declare class Client extends EventEmitter {
|
||||||
|
|||||||
14
index.js
14
index.js
@ -14,6 +14,15 @@ const buildApi = require('./api')
|
|||||||
class Client extends EventEmitter {
|
class Client extends EventEmitter {
|
||||||
constructor (opts = {}) {
|
constructor (opts = {}) {
|
||||||
super()
|
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) {
|
if (!opts.node && !opts.nodes) {
|
||||||
throw new ConfigurationError('Missing node(s) option')
|
throw new ConfigurationError('Missing node(s) option')
|
||||||
}
|
}
|
||||||
@ -56,7 +65,10 @@ class Client extends EventEmitter {
|
|||||||
nodeWeighter: options.nodeWeighter,
|
nodeWeighter: options.nodeWeighter,
|
||||||
nodeSelector: options.nodeSelector,
|
nodeSelector: options.nodeSelector,
|
||||||
Connection: options.Connection,
|
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
|
// Add the connections before initialize the Transport
|
||||||
|
|||||||
1
lib/ConnectionPool.d.ts
vendored
1
lib/ConnectionPool.d.ts
vendored
@ -42,6 +42,7 @@ export default class ConnectionPool {
|
|||||||
dead: string[];
|
dead: string[];
|
||||||
_ssl: SecureContextOptions | null;
|
_ssl: SecureContextOptions | null;
|
||||||
_agent: AgentOptions | null;
|
_agent: AgentOptions | null;
|
||||||
|
_sniffEnabled: boolean;
|
||||||
resurrectTimeout: number;
|
resurrectTimeout: number;
|
||||||
resurrectTimeoutCutoff: number;
|
resurrectTimeoutCutoff: number;
|
||||||
pingTimeout: number;
|
pingTimeout: number;
|
||||||
|
|||||||
@ -27,6 +27,7 @@ class ConnectionPool {
|
|||||||
this.nodeFilter = opts.nodeFilter || defaultNodeFilter
|
this.nodeFilter = opts.nodeFilter || defaultNodeFilter
|
||||||
this.Connection = opts.Connection
|
this.Connection = opts.Connection
|
||||||
this.emit = opts.emit || noop
|
this.emit = opts.emit || noop
|
||||||
|
this._sniffEnabled = opts.sniffEnabled || false
|
||||||
|
|
||||||
if (typeof opts.nodeSelector === 'function') {
|
if (typeof opts.nodeSelector === 'function') {
|
||||||
this.nodeSelector = opts.nodeSelector
|
this.nodeSelector = opts.nodeSelector
|
||||||
@ -50,10 +51,13 @@ class ConnectionPool {
|
|||||||
* Marks a connection as 'alive'.
|
* Marks a connection as 'alive'.
|
||||||
* If needed removes the connection from the dead list
|
* If needed removes the connection from the dead list
|
||||||
* and then resets the `deadCount`.
|
* and then resets the `deadCount`.
|
||||||
|
* If sniffing is not enabled and there is only
|
||||||
|
* one node, this method is a noop.
|
||||||
*
|
*
|
||||||
* @param {object} connection
|
* @param {object} connection
|
||||||
*/
|
*/
|
||||||
markAlive (connection) {
|
markAlive (connection) {
|
||||||
|
if (this._sniffEnabled === false && this.connections.size === 1) return
|
||||||
const { id } = connection
|
const { id } = connection
|
||||||
debug(`Marking as 'alive' connection '${id}'`)
|
debug(`Marking as 'alive' connection '${id}'`)
|
||||||
const index = this.dead.indexOf(id)
|
const index = this.dead.indexOf(id)
|
||||||
@ -67,10 +71,13 @@ class ConnectionPool {
|
|||||||
* Marks a connection as 'dead'.
|
* Marks a connection as 'dead'.
|
||||||
* If needed adds the connection to the dead list
|
* If needed adds the connection to the dead list
|
||||||
* and then increments the `deadCount`.
|
* and then increments the `deadCount`.
|
||||||
|
* If sniffing is not enabled and there is only
|
||||||
|
* one node, this method is a noop.
|
||||||
*
|
*
|
||||||
* @param {object} connection
|
* @param {object} connection
|
||||||
*/
|
*/
|
||||||
markDead (connection) {
|
markDead (connection) {
|
||||||
|
if (this._sniffEnabled === false && this.connections.size === 1) return
|
||||||
const { id } = connection
|
const { id } = connection
|
||||||
debug(`Marking as 'dead' connection '${id}'`)
|
debug(`Marking as 'dead' connection '${id}'`)
|
||||||
if (this.dead.indexOf(id) === -1) {
|
if (this.dead.indexOf(id) === -1) {
|
||||||
|
|||||||
Reference in New Issue
Block a user