From ca527f41783c016a5c992a7e4a0c751870dbdfe5 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 28 Nov 2019 08:36:12 -0500 Subject: [PATCH] fix: use serverDescriptionChanged event to distinguish between timeout vs disconnected re: #8383 --- lib/connection.js | 17 +++++++++-------- test/connection.test.js | 1 - 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index ed1d5b56dd2..ff75f118786 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -677,15 +677,16 @@ Connection.prototype.openUri = function(uri, options, callback) { server.s.pool.on('reconnect', () => { _handleReconnect(); }); - server.s.pool.on('timeout', () => { - _this.emit('timeout'); - }); - client.on('serverClosed', () => { - _this.readyState = STATES.disconnected; - }); - server.s.pool.on('drain', err => { - if (err && err.name === 'MongoNetworkError' && err.message.endsWith('timed out')) { + client.on('serverDescriptionChanged', ev => { + const newDescription = ev.newDescription; + if (newDescription.type === 'Standalone') { + _handleReconnect(); + } else if (newDescription.error != null && + newDescription.error.name === 'MongoNetworkError' && + newDescription.error.message.endsWith('timed out')) { _this.emit('timeout'); + } else { + _this.readyState = STATES.disconnected; } }); } else if (type.startsWith('ReplicaSet')) { diff --git a/test/connection.test.js b/test/connection.test.js index 7a3ca8a0d67..170d0782380 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -341,7 +341,6 @@ describe('connections:', function() { }); conn.on('disconnected', function() { - console.log('Disconnected', new Error().stack); ++numDisconnected; });