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

Commit 91afa273 by Sascha Depold

moved connector logic into new class

1 parent 18cecf6c
var ConnectorManager = module.exports = function(config) {
this.connectorCheckTimeoutId = null
this.client = null
this.config = config
}
ConnectorManager.prototype.connect = function() {
var self = this
this.client = require("mysql").createClient({
user: this.config.username,
password: this.config.password,
host: this.config.host,
port: this.config.port,
database: this.config.database
})
this.client.setMaxListeners(100)
}
ConnectorManager.prototype.disconnect = function() {
var self = this
this.client.end(function() {
self.client = null
self.connectorCheckTimeoutId = null
})
}
ConnectorManager.prototype.check = function() {
var self = this
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.__defineGetter__('isConnected', function() {
return (this.client !== null)
})
\ No newline at end of file
......@@ -19,43 +19,11 @@ var Sequelize = module.exports = function(database, username, password, options)
host : options.host || 'localhost',
port : options.port || 3306
}
this.connectorCheckTimeoutId = null
this.connectorManager = new (require('./connector-manager'))(this.config)
}
Sequelize.Utils = Utils
var instanceMethods = {
initConnector: function() {
var self = this
this.client = require("mysql").createClient({
user: this.config.username,
password: this.config.password,
host: this.config.host,
port: this.config.port,
database: this.config.database
})
this.client.setMaxListeners(100)
},
freeConnector: function() {
var self = this
this.client.end(function() {
self.client = null
self.connectorCheckTimeoutId = null
})
},
checkConnector: function() {
var self = this
this.connectorCheckTimeoutId && clearTimeout(this.connectorCheckTimeoutId)
this.connectorCheckTimeoutId = setTimeout(function() {
if(self.client && self.client._queue && (self.client._queue.length === 0))
self.freeConnector()
}, 500)
},
define: function(modelName, attributes, options) {
options = options || {}
......@@ -75,16 +43,16 @@ var instanceMethods = {
options = options || {}
if(this.options.queryOptions) options = Sequelize.Utils.merge(options, this.options.queryOptions)
if(!this.client) this.initConnector()
if(!this.connectorManager.isConnected) this.connectorManager.connect()
options.logging = this.options.hasOwnProperty('logging') ? this.options.logging : true
var self = this
, query = new Query(this.client, callee, options).run(sql)
, query = new Query(this.connectorManager.client, callee, options).run(sql)
query
.on('success', function(){ self.checkConnector() })
.on('failure', function(){ self.checkConnector() })
.on('success', function(){ self.connectorManager.check() })
.on('failure', function(){ self.connectorManager.check() })
return query
},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!