Last active
December 15, 2015 07:59
-
-
Save you21979/5227205 to your computer and use it in GitHub Desktop.
MYSQL切断時にリトライするモジュール
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mysql = require('mysql'); | |
var DefaultEvent = { | |
onConnected : function(){ | |
console.log('MYSQL[%s] connected', this.conf.host); | |
}, | |
onClosed : function(reason){ | |
console.log('MYSQL[%s] closed reason: %s', this.conf.host, reason); | |
}, | |
onError : function(err){ | |
console.log(err.stack); | |
}, | |
onQuery : function(sql){ | |
console.log('MYSQL[%s] query: %s', this.conf.host, sql); | |
}, | |
doReconnect : function(err){ | |
console.log('MYSQL[%s] re-connecting lost connection: %s', this.conf.host, err.code); | |
return true; | |
}, | |
}; | |
var mysqlCreateConnect = function(ref) { | |
var conn = ref.conn = mysql.createConnection(ref.conf); | |
conn.on('end', function(err){ | |
var reason = "APP_CLOSE"; | |
if(err){ | |
reason = err.code; | |
} | |
ref.event.onClosed.bind(ref)(reason); | |
}); | |
conn.on('error', function(err) { | |
if (!err.fatal) { | |
return; | |
} | |
if (err.code !== 'PROTOCOL_CONNECTION_LOST') { | |
throw err; | |
} | |
if(ref.isConnect && ref.event.doReconnect.bind(ref)(err)){ | |
mysqlCreateConnect(ref); | |
} | |
}); | |
conn.connect(function(err){ | |
if(err){ | |
switch(err.errno){ | |
case "ECONNREFUSED": | |
mysqlCreateConnect(ref); | |
break; | |
case "ENOTFOUND": | |
ref.event.onError.bind(ref)(err); | |
break; | |
} | |
}else{ | |
ref.event.onConnected.bind(ref)(); | |
} | |
}); | |
} | |
var MysqlClient = module.exports = function(conf, event){ | |
if(!event){ | |
event = DefaultEvent; | |
} | |
this.conn = null; | |
this.isConnect = false; | |
this.event = event; | |
this.conf = conf; | |
}; | |
MysqlClient.prototype = { | |
connect : function(){ | |
if(this.isConnect){ | |
return; | |
} | |
this.isConnect = true; | |
mysqlCreateConnect(this); | |
}, | |
close : function(){ | |
if(this.isConnect){ | |
this.isConnect = false; | |
this.conn.end(); | |
} | |
}, | |
query : function(sql, arg1, arg2){ | |
var log = sql; | |
if(!(arg1 instanceof Function)){ | |
if(arg1 instanceof Array){ | |
arg1.forEach(function(v){ | |
log = log.replace('?', '"'+v.toString()+'"'); | |
}); | |
}else{ | |
log = log.replace('?', '"'+arg1.toString()+'"'); | |
} | |
} | |
this.event.onQuery.bind(this)(log); | |
this.conn.query(sql, arg1, arg2); | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MysqlClient = require("./mysql_client.js"); | |
function test(){ | |
var conf = { | |
host : "127.0.0.1", | |
user : "root", | |
database : "test", | |
}; | |
var cl = new MysqlClient(conf); | |
cl.connect(); | |
setTimeout(function T(){ | |
cl.query('select * from test1 where id = ?;',[Math.random()*100|0],function(err,val){ | |
console.log(val); | |
}); | |
setTimeout(T, Math.random()*12000|0); | |
}, 0); | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment