不要怂,就是干,撸起袖子干!

Commit 85149825 by Sascha Depold

refactored connector manager

1 parent 4dab6668
var ConnectorManager = module.exports = function(config) {
this.connectorCheckTimeoutId = null
this.client = null
this.config = config
this.clientRequests = 0
this.disconnectTimeoutId = null
}
ConnectorManager.prototype.connect = function() {
......@@ -14,27 +15,50 @@ ConnectorManager.prototype.connect = function() {
port: this.config.port,
database: this.config.database
})
this.client.setMaxListeners(999999)
this.client.setMaxListeners(999)
}
ConnectorManager.prototype.disconnect = function() {
var self = this
this.client.end(function() {
self.client = null
self.connectorCheckTimeoutId = null
})
this.client.end()
}
ConnectorManager.prototype.check = function() {
var self = this
ConnectorManager.prototype.reconnect = function() {
this.clientRequests = 0
this.disconnect()
this.connect()
}
this.connectorCheckTimeoutId && clearTimeout(this.connectorCheckTimeoutId)
this.connectorCheckTimeoutId = setTimeout(function() {
if(self.client && self.client._queue && (self.client._queue.length === 0))
self.disconnect()
}, 100)
ConnectorManager.prototype.afterQuery = function() {
this.clientRequests++
this._reconnectIfTooManyConnections()
this._disconnectIfNoConnections()
}
ConnectorManager.prototype.__defineGetter__('tooManyRequests', function() {
return (this.clientRequests > 30)
})
ConnectorManager.prototype.__defineGetter__('hasNoConnections', function() {
return this.client._queue && (this.client._queue.length == 0)
})
ConnectorManager.prototype.__defineGetter__('isConnected', function() {
return (this.client !== null)
})
\ No newline at end of file
return this.client != null
})
// private
ConnectorManager.prototype._reconnectIfTooManyConnections = function() {
this.tooManyRequests && this.reconnect()
}
ConnectorManager.prototype._disconnectIfNoConnections = function() {
var self = this
this.disconnectTimeoutId && clearTimeout(this.disconnectTimeoutId)
this.disconnectTimeoutId = setTimeout(function() {
self.isConnected && self.hasNoConnections && self.disconnect()
}, 100)
}
\ No newline at end of file
......@@ -51,8 +51,8 @@ var instanceMethods = {
, query = new Query(this.connectorManager.client, callee, options).run(sql)
query
.on('success', function(){ self.connectorManager.check() })
.on('failure', function(){ self.connectorManager.check() })
.on('success', function(){ self.connectorManager.afterQuery() })
.on('failure', function(){ self.connectorManager.afterQuery() })
return query
},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!