Last active
December 15, 2015 15:09
-
-
Save you21979/5279675 to your computer and use it in GitHub Desktop.
node-mysqlのconnect呼ぶだけで遅い。秒間100-300回程度しか回ってくれない
This file contains hidden or 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
"use strict"; | |
var mysql = require('mysql'); | |
var mysqlConfig = { | |
host : "127.0.0.1", | |
port : 3306, | |
user : "root", | |
password : "", | |
database : "test", | |
debug : false, | |
}; | |
function db(){ | |
var conn = mysql.createConnection(mysqlConfig); | |
conn.connect(function(err){ | |
conn.end(); | |
}); | |
} | |
var cnt=0; | |
process.nextTick(function P(){ | |
db(); | |
process.nextTick(P); | |
console.log(++cnt); | |
}); |
This file contains hidden or 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
"use strict"; | |
var mysql = require('mysql'); | |
var mysqlConfig = { | |
host : "127.0.0.1", | |
port : 3306, | |
user : "root", | |
password : "", | |
database : "test", | |
debug : false, | |
connectionLimit : 1000, | |
}; | |
var pool = mysql.createPool(mysqlConfig); | |
function db(){ | |
pool.getConnection(function(err, connection) { | |
if(err){ | |
console.log(err); | |
return; | |
} | |
connection.end(); | |
}); | |
} | |
var cnt=0; | |
process.nextTick(function P(){ | |
db(); | |
process.nextTick(P); | |
console.log(++cnt); | |
}); |
connect自体はノンブロッキングで秒間1450回くらい呼べるようだ
ハンドシェイクで接続した分のコールバックをこなすから遅くなってるように見えるようだ
やっぱり、poolというモジュールがあるということは遅いんだろう。poolは速かった
pool使おうとおもったんだけど正常にクローズする方法って存在するのだろうか。
開いているコネクションがひとつもないときにpool.end()で正常クローズ
poolしてるのが切れた時に接続済みのconnectオブジェクトを復旧する方法がないのが辛いなぁ。query中にきれたらちゃんとエラーを呼び出し元のコールバックに返せるのだろうか
プールした場合トランザクションをちゃんと切ってからにendしないと次に使いまわされた時に前のトランザクションの状態が残ったままになる。
状態管理しておかないと危ない
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ノンブロッキング接続ならもう少し速度出ると思うのだが。。。