Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions lib/sdam/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ class Server extends EventEmitter {
this.s.pool.on('error', errorEventHandler(this));
this.s.pool.on('parseError', parseErrorEventHandler(this));

// it is unclear whether consumers should even know about these events
// this.s.pool.on('timeout', timeoutEventHandler(this));
// this.s.pool.on('reconnect', reconnectEventHandler(this));
// this.s.pool.on('reconnectFailed', errorEventHandler(this));
// relay events to ensure topology reconnection/connection status consistency.
// for more details, check: NODE-2147: Unexpected behaviours using useUnifiedTopology: true
this.s.pool.on('timeout', timeoutEventHandler(this));
this.s.pool.on('reconnect', reconnectEventHandler(this));
this.s.pool.on('reconnectFailed', errorEventHandler(this));

// relay all command monitoring events
relayEvents(this.s.pool, this, ['commandStarted', 'commandSucceeded', 'commandFailed']);
Expand Down Expand Up @@ -389,4 +390,19 @@ function parseErrorEventHandler(server) {
};
}

function timeoutEventHandler(server) {
return function() {
server.s.state = STATE_DISCONNECTED;
server.emit('error', new MongoNetworkError('Pool timeout event triggered.'));
server.emit('close');
};
}

function reconnectEventHandler(server) {
return function() {
server.s.state = STATE_CONNECTING;
server.emit('reconnect');
};
}

module.exports = Server;
8 changes: 7 additions & 1 deletion lib/sdam/topology.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const LOCAL_SERVER_EVENTS = SERVER_RELAY_EVENTS.concat([
'connect',
'descriptionReceived',
'close',
'ended'
'ended',
'reconnectFailed'
]);

/**
Expand Down Expand Up @@ -843,6 +844,11 @@ function updateServers(topology, incomingServerDescription) {

function serverConnectEventHandler(server, topology) {
return function(/* isMaster, err */) {
// close topology on reconnectFailed.
server.s.pool.on('reconnectFailed', () => {
topology.close();
});

server.monitor({
initial: true,
heartbeatFrequencyMS: topology.description.heartbeatFrequencyMS
Expand Down
2 changes: 1 addition & 1 deletion lib/topologies/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ var eventHandler = function(self, event) {
event === 'timeout' ||
event === 'reconnect' ||
event === 'attemptReconnect' ||
'reconnectFailed'
event === 'reconnectFailed'
) {
// Remove server instance from accounting
if (
Expand Down