请问哪位大神测试过雪崩的问题:node-mysql例子?我怎么不会测试?我觉得我的代码写错了,请指点~
var EventProxy = require('eventproxy');
var proxy = new EventProxy();
var mysql = require('mysql');
var http = require('http');
var option = {
host:'localhost',
port:3306,
database:'test',
user:'test',
password:'test',
}
var connection = mysql.createConnection(option);
var status = "ready";
http.createServer(function (request, response) {
proxy.once("selected", function(result){
response.writeHead(200);
response.end('11');
});
if (status === "ready") {
status = "pending";
connection.query("select * from test", function (err,results) {
console.log('1');
proxy.emit("selected", results);
status = "ready";
});
}
}).listen(7777);
管理员提示:学一下markdown语法
2 回复
我想大概是这么个意思
var mysql = require('mysql');
var http = require('http');
var option = {
host: '123.57.143.189',//这是我的服务器,随意使用
port: 3306,
database: 'test',
user: 'readonly',
password: 'readonly'
}
var connection = mysql.createConnection(option);
var events = require('events');
var proxy = new events.EventEmitter();
var status = "nodata";//默认状态没有数据
http.createServer(function (request, response) {
select(send.bind(response));
}).listen(8080);
//声明回调函数
function send(data) {
this.end(data.toString());//向客户端发送数据
}
var select = function(callback){
proxy.once('selected',callback);//把回调压队队列中
if (status === 'nodata') {//如果没有数据,那么去数据库中获取数据,
status = 'querying';//修改状态为查询中,其它请求再也进不来这个分支了
connection.query('select 100 num', function (err,results) {
proxy.emit('query_over', results[0]['num']);//查询结束后发射事件,并把数据做为参数递给回调函数
status = 'nodata';//把状态改回为nodata,以供下次查询
});
}
}