Skip to content
Open
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
56 changes: 41 additions & 15 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function MongolianServer(mongolian, url) {
connected = true
callback(null, connection)
})
connection.on("message", function(message) {
connection.on('message', function(message) {
var reply = new mongo.MongoReply
reply.parseHeader(message, bsonSerializer)
if (reply.messageLength != message.length) {
Expand All @@ -249,11 +249,18 @@ function MongolianServer(mongolian, url) {
delete self._callbacks[reply.responseTo]
self._callbackCount--
reply.parseBody(message, bsonSerializer, null, function() {
cb(null,reply)
try {
cb(null,reply)
} catch(err) {
connection.emit('callbackError', err)
}
})
}
}
})
connection.on('callbackError', function(error) {
mongolian.log.error(self+": CallbackError: "+error.stack)
})
connection.start()
})

Expand Down Expand Up @@ -282,18 +289,37 @@ MongolianServer.prototype.toString = function() {
/**
* Closes the current connection, passing the optional error object to any pending request callbacks.
*/
MongolianServer.prototype.close = function(error) {
error = error || new Error("Connection closed")
if (this._connection.value) {
var callbacks = this._callbacks
this._callbacks = {}
this._callbackCount = 0
this._connection.value.close()
this._connection.reset()
delete this._type
for (var requestId in callbacks) {
callbacks[requestId](error)
}
this.mongolian._replicaSet.emit('lost', this)
MongolianServer.prototype.close = function(error, callback) {
var self = this

// function: error all callbacks
var _error_callbacks = function() {
error = error || new Error("Connection closed")
var callbacks = self._callbacks
self._callbacks = {}
self._callbackCount = 0
for (var requestId in callbacks) {
callbacks[requestId](error)
}
}

// error some callbacks immediately
_error_callbacks()

// close the connection when it becomes available
self._connection(function(err, connection) {
delete self._type
self.mongolian._replicaSet.emit('lost', self)

// error any new callbacks
_error_callbacks()

if (connection) {
connection.close()
}

if (callback) {
callback(err)
}
})
}