Skip to content

Commit

Permalink
refactor: errors before handshake should mark server unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 6, 2020
1 parent 38ae86d commit fc1a775
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions lib/core/sdam/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ class Server extends EventEmitter {
}

this.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);
if (err) {
markServerUnknown(this, err);
return cb(err);
}

conn.command(ns, cmd, options, makeOperationHandler(this, options, cb));
}, callback);
}
Expand All @@ -299,7 +303,11 @@ class Server extends EventEmitter {
}

this.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);
if (err) {
markServerUnknown(this, err);
return cb(err);
}

conn.query(ns, cmd, cursorState, options, makeOperationHandler(this, options, cb));
}, callback);
}
Expand All @@ -319,7 +327,11 @@ class Server extends EventEmitter {
}

this.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);
if (err) {
markServerUnknown(this, err);
return cb(err);
}

conn.getMore(ns, cursorState, batchSize, options, makeOperationHandler(this, options, cb));
}, callback);
}
Expand All @@ -341,7 +353,11 @@ class Server extends EventEmitter {
}

this.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);
if (err) {
markServerUnknown(this, err);
return cb(err);
}

conn.killCursors(ns, cursorState, makeOperationHandler(this, null, cb));
}, callback);
}
Expand Down Expand Up @@ -436,11 +452,22 @@ function executeWriteOperation(args, options, callback) {
}

server.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);
if (err) {
markServerUnknown(server, err);
return cb(err);
}

conn[op](ns, ops, options, makeOperationHandler(server, options, cb));
}, callback);
}

function markServerUnknown(server, error) {
server.emit(
'descriptionReceived',
new ServerDescription(server.description.address, null, { error })
);
}

function makeOperationHandler(server, options, callback) {
return function handleOperationResult(err, result) {
if (err) {
Expand Down

0 comments on commit fc1a775

Please sign in to comment.