From 17959a41e12362aadd8cd0d4e503791cb20d4d87 Mon Sep 17 00:00:00 2001 From: Tomas Della Vedova Date: Tue, 14 Apr 2020 18:14:27 +0200 Subject: [PATCH] Fix 1153 (#1159) * Fix #1153 * Updated test --- lib/pool/ConnectionPool.js | 12 +++++++++++- test/unit/connection-pool.test.js | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/pool/ConnectionPool.js b/lib/pool/ConnectionPool.js index fa3bc04fb..143fd75c7 100644 --- a/lib/pool/ConnectionPool.js +++ b/lib/pool/ConnectionPool.js @@ -60,7 +60,17 @@ class ConnectionPool extends BaseConnectionPool { const { id } = connection debug(`Marking as 'dead' connection '${id}'`) if (this.dead.indexOf(id) === -1) { - this.dead.push(id) + // It might happen that `markDead` is called jsut after + // a pool update, and in such case we will add to the dead + // list a node that no longer exist. The following check verify + // that the connection is still part of the pool before + // marking it as dead. + for (var i = 0; i < this.size; i++) { + if (this.connections[i].id === id) { + this.dead.push(id) + break + } + } } connection.status = Connection.statuses.DEAD connection.deadCount++ diff --git a/test/unit/connection-pool.test.js b/test/unit/connection-pool.test.js index fbc57c999..b8c412d16 100644 --- a/test/unit/connection-pool.test.js +++ b/test/unit/connection-pool.test.js @@ -74,6 +74,14 @@ test('API', t => { }, 10) }) + t.test('markDead should ignore connections that no longer exists', t => { + const pool = new ConnectionPool({ Connection, sniffEnabled: true }) + pool.addConnection('http://localhost:9200/') + pool.markDead({ id: 'foo-bar' }) + t.deepEqual(pool.dead, []) + t.end() + }) + t.test('markAlive', t => { const pool = new ConnectionPool({ Connection, sniffEnabled: true }) const href = 'http://localhost:9200/'