Updated Connections handling (#1127)

* Updated Connections handling

- The ConnectionPool.getConnection method now always returns a connection
- The ConnectionPool.update mehtod now cleans the dead list
- Deprecated the NoLivingConnectionsError

* Updated test

* Updated docs

* The NoLivingConnectionsError can still happen if the filter/selector returns no nodes

* Updated test

* Updated docs

* Catch undefined connections as well

* Updated test

* Updated ApiError type def
This commit is contained in:
Tomas Della Vedova
2020-03-26 13:41:05 +01:00
committed by delvedor
parent 9b400afbc8
commit d84cb382f7
7 changed files with 62 additions and 49 deletions

View File

@ -123,10 +123,9 @@ class Transport {
const makeRequest = () => {
if (meta.aborted === true) return
meta.connection = this.getConnection({ requestId: meta.request.id })
if (meta.connection === null) {
return callback(new NoLivingConnectionsError('There are no living connections'), result)
if (meta.connection == null) {
return callback(new NoLivingConnectionsError(), result)
}
// TODO: make this assignment FAST
const headers = Object.assign({}, this.headers, options.headers)

View File

@ -51,7 +51,7 @@ class NoLivingConnectionsError extends ElasticsearchClientError {
super(message)
Error.captureStackTrace(this, NoLivingConnectionsError)
this.name = 'NoLivingConnectionsError'
this.message = message || 'No Living Connections Error'
this.message = message || 'Given the configuration, the ConnectionPool was not able to find a usable Connection for this request.'
this.meta = meta
}
}

View File

@ -50,13 +50,10 @@ class ConnectionPool extends BaseConnectionPool {
* 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.size === 1) return this
const { id } = connection
debug(`Marking as 'alive' connection '${id}'`)
const index = this.dead.indexOf(id)
@ -71,13 +68,10 @@ class ConnectionPool extends BaseConnectionPool {
* 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.size === 1) return this
const { id } = connection
debug(`Marking as 'dead' connection '${id}'`)
if (this.dead.indexOf(id) === -1) {
@ -183,7 +177,7 @@ class ConnectionPool extends BaseConnectionPool {
/**
* Returns an alive connection if present,
* otherwise returns null.
* otherwise returns a dead connection.
* By default it filters the `master` only nodes.
* It uses the selector to choose which
* connection return.
@ -201,11 +195,13 @@ class ConnectionPool extends BaseConnectionPool {
name: opts.name
})
const noAliveConnections = this.size === this.dead.length
// TODO: can we cache this?
const connections = []
for (var i = 0; i < this.size; i++) {
const connection = this.connections[i]
if (connection.status === Connection.statuses.ALIVE) {
if (noAliveConnections || connection.status === Connection.statuses.ALIVE) {
if (filter(connection) === true) {
connections.push(connection)
}
@ -237,13 +233,7 @@ class ConnectionPool extends BaseConnectionPool {
*/
update (connections) {
super.update(connections)
for (var i = 0; i < this.dead.length; i++) {
if (this.connections.find(c => c.id === this.dead[i]) === undefined) {
this.dead.splice(i, 1)
}
}
this.dead = []
return this
}
}

2
lib/pool/index.d.ts vendored
View File

@ -103,7 +103,7 @@ declare class BaseConnectionPool {
markDead(connection: Connection): this;
/**
* Returns an alive connection if present,
* otherwise returns null.
* otherwise returns a dead connection.
* By default it filters the `master` only nodes.
* It uses the selector to choose which
* connection return.