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

Commit 85149825 by Sascha Depold

refactored connector manager

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