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

Commit d8484e92 by Jan Aagaard Meier

Removed semicolons and a bit of unnessecary code in mariadb connectormanager

1 parent 7be7eb9c
......@@ -29,17 +29,19 @@ module.exports = (function() {
minConnections: 0,
maxIdleTime: 1000,
handleDisconnects: false,
validate: validateConnection
});
this.pendingQueries = 0;
this.useReplicaton = !!config.replication;
this.useQueue = config.queue !== undefined ? config.queue : true;
validate: function(client) {
return client && client.connected
}
})
this.pendingQueries = 0
this.useReplicaton = !!config.replication
this.useQueue = config.queue !== undefined ? config.queue : true
var self = this
if (this.useReplicaton) {
var reads = 0,
writes = 0;
var reads = 0
, writes = 0
// Init configs with options from config if not present
for (var i in config.replication.read) {
......@@ -49,7 +51,7 @@ module.exports = (function() {
username: this.config.username,
password: this.config.password,
database: this.config.database
});
})
}
config.replication.write = Utils._.defaults(config.replication.write, {
host: this.config.host,
......@@ -57,27 +59,27 @@ module.exports = (function() {
username: this.config.username,
password: this.config.password,
database: this.config.database
});
})
// I'll make my own pool, with blackjack and hookers!
this.pool = {
release: function (client) {
if (client.queryType == 'read') {
return this.read.release(client);
return this.read.release(client)
} else {
return this.write.release(client);
return this.write.release(client)
}
},
acquire: function (callback, priority, queryType) {
if (queryType == 'SELECT') {
this.read.acquire(callback, priority);
this.read.acquire(callback, priority)
} else {
this.write.acquire(callback, priority);
this.write.acquire(callback, priority)
}
},
drain: function () {
this.read.drain();
this.write.drain();
this.read.drain()
this.write.drain()
},
read: Pooling.Pool({
name: 'sequelize-read',
......@@ -85,12 +87,12 @@ module.exports = (function() {
if (reads >= self.config.replication.read.length) {
reads = 0
}
var config = self.config.replication.read[reads++];
var config = self.config.replication.read[reads++]
connect.call(self, function (err, connection) {
connection.queryType = 'read'
done(null, connection)
}, config);
}, config)
},
destroy: function(client) {
disconnect.call(self, client)
......@@ -106,7 +108,7 @@ module.exports = (function() {
connect.call(self, function (err, connection) {
connection.queryType = 'write'
done(null, connection)
}, self.config.replication.write);
}, self.config.replication.write)
},
destroy: function(client) {
disconnect.call(self, client)
......@@ -146,9 +148,7 @@ module.exports = (function() {
})
}
Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype);
var isConnecting = false;
Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype)
ConnectorManager.prototype.query = function(sql, callee, options) {
if (!this.isConnected && !this.pool) {
......@@ -160,30 +160,31 @@ module.exports = (function() {
query: new Query(this.client, this.sequelize, callee, options || {}),
client: this.client,
sql: sql
};
}
enqueue.call(this, queueItem, options);
return queueItem.query;
enqueue.call(this, queueItem, options)
return queueItem.query
}
var self = this, query = new Query(this.client, this.sequelize, callee, options || {});
this.pendingQueries++;
var self = this
, query = new Query(this.client, this.sequelize, callee, options || {})
this.pendingQueries++
query.done(function() {
self.pendingQueries--;
self.pendingQueries--
if (self.pool) {
self.pool.release(query.client);
self.pool.release(query.client)
} else {
if (self.pendingQueries === 0) {
setTimeout(function() {
self.pendingQueries === 0 && self.disconnect.call(self)
}, 100);
}, 100)
}
}
});
})
if (!this.pool) {
query.run(sql);
query.run(sql)
} else {
this.pool.acquire(function(err, client) {
if (err) {
......@@ -192,72 +193,64 @@ module.exports = (function() {
query.client = client
query.run(sql)
return;
return
}, undefined, options.type)
}
return query;
};
return query
}
ConnectorManager.prototype.connect = function() {
var self = this;
var self = this
// in case database is slow to connect, prevent orphaning the client
if (this.isConnecting || this.pool) {
return;
return
}
connect.call(self, function(err, client) {
self.client = client;
return;
});
return;
};
self.client = client
return
})
return
}
ConnectorManager.prototype.disconnect = function() {
if (this.client) {
disconnect.call(this, this.client)
}
return
};
}
// private
var disconnect = function(client) {
var self = this;
var self = this
if (!this.useQueue) {
this.client = null;
this.client = null
client.end()
return
}
client.end(function() {
if (!self.useQueue) {
return client.destroy();
var intervalObj = null
var cleanup = function () {
// make sure to let queued items be finish before calling end
if (self && self.hasQueuedItems) {
return
}
var intervalObj = null
var cleanup = function () {
var retryCt = 0
// make sure to let client finish before calling destroy
if (self && self.hasQueuedItems) {
return
}
// needed to prevent mysql connection leak
client.destroy()
if (self && self.client) {
self.client = null
}
clearInterval(intervalObj)
client.end()
if (self && self.client) {
self.client = null
}
intervalObj = setInterval(cleanup, 10)
cleanup()
return
})
clearInterval(intervalObj)
}
intervalObj = setInterval(cleanup, 10)
cleanup()
}
var connect = function(done, config) {
config = config || this.config
var emitter = new (require('events').EventEmitter)()
, self = this
var self = this
, client
this.isConnecting = true
......@@ -268,12 +261,12 @@ module.exports = (function() {
password: config.password,
db: config.database,
metadata: true
};
}
if (config.dialectOptions) {
Object.keys(config.dialectOptions).forEach(function (key) {
connectionConfig[key] = config.dialectOptions[key];
});
})
}
client = new mariadb()
......@@ -310,10 +303,6 @@ module.exports = (function() {
})
}
var validateConnection = function(client) {
return client && client.state !== 'disconnected'
}
var enqueue = function(queueItem, options) {
options = options || {}
if (this.activeQueue.length < this.maxConcurrentQueries) {
......@@ -350,7 +339,7 @@ module.exports = (function() {
var transferQueuedItems = function(count) {
for(var i = 0; i < count; i++) {
var queueItem = this.queue.shift();
var queueItem = this.queue.shift()
if (queueItem) {
enqueue.call(this, queueItem)
}
......
......@@ -58,7 +58,7 @@ module.exports = (function() {
case "TIMESTAMP":
case "DATETIME":
row[prop] = new Date(row[prop] + 'Z')
break;
break
case "BIT":
case "BLOB":
case "TINYBLOB":
......@@ -67,7 +67,7 @@ module.exports = (function() {
if (metadata.charsetNrs[prop] === 63) { // binary
row[prop] = new Buffer(row[prop])
}
break;
break
case "TIME":
case "CHAR":
case "VARCHAR":
......@@ -75,7 +75,7 @@ module.exports = (function() {
case "ENUM":
case "GEOMETRY":
case "NULL":
break;
break
default:
// blank
}
......@@ -85,41 +85,39 @@ module.exports = (function() {
})
.on('error', function(err) {
errorDetected = true
self.emit('sql', this.sql)
self.emit('error', err, this.callee)
}.bind(this))
self.emit('sql', self.sql)
self.emit('error', err, self.callee)
})
.on('end', function(info) {
if (alreadyEnded || errorDetected) {
return
}
alreadyEnded = true
self.emit('sql', this.sql)
self.emit('sql', self.sql)
// we need to figure out whether to send the result set
// or info depending upon the type of query
if (/^call/.test(this.sql.toLowerCase())) {
if (/^call/.test(self.sql.toLowerCase())) {
self.emit('success', resultSet)
} else if( /^show/.test(this.sql.toLowerCase()) ||
/^select/.test(this.sql.toLowerCase()) ||
/^describe/.test(this.sql.toLowerCase())) {
self.emit('success', this.formatResults(resultSet))
} else if( /^show/.test(self.sql.toLowerCase()) ||
/^select/.test(self.sql.toLowerCase()) ||
/^describe/.test(self.sql.toLowerCase())) {
self.emit('success', self.formatResults(resultSet))
} else {
self.emit('success', this.formatResults(info))
self.emit('success', self.formatResults(info))
}
}.bind(this));
}.bind(this))
})
})
.on('error', function(err) {
if (errorDetected) {
return
}
errorDetected = true
self.emit('sql', this.sql)
self.emit('error', err, this.callee)
}.bind(this))
.on('end', function(info) {
// nothing here (query info is returned during the 'result' event)
}.bind(this)).setMaxListeners(100)
self.emit('sql', self.sql)
self.emit('error', err, self.callee)
})
.setMaxListeners(100)
return this
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!