深入浅出Nodejs--雪崩问题
发布于 2 年前 作者 songqinghehe 2864 次浏览 最后一次编辑是 1 年前 来自 问答

请问哪位大神测试过雪崩的问题: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语法

4 回复

我想大概是这么个意思

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,以供下次查询
        });
    }
}

@zhufengnodejs 最后的"query_over"应该是"selected"吧? 另外, 和楼主的代码没什么大的区别啊?

我写的简单模拟测试例子,已测试通过

  var EventEmitter = require('events').EventEmitter;
  var proxy = new EventEmitter();
  proxy.setMaxListeners(0);
  var selectSqls = [];
  var flags = [];
  for(var i=0;i<10;i++){
	  flags.push('ready');
	  for(var j=0;j<10;j++){
		  selectSqls.push(j);
	  }
  }
  
  var count=1;
  var http=require('http');
  var select = function (num,callback) {
	  proxy.once(''+num,callback);
	  if(flags[num] === 'ready'){
		  flags[num] = 'pending';
		  http.get("http://www.cnblogs.com/", function(res) {
			  console.log("Got response: " + count++);
			  proxy.emit(''+num,num);
			  flags[num] = 'ready';
			  //res.resume();
		  }).on('error', function(e) {
			  console.log("Got error: " + e.message);
		  });
	  }
  };
  
  var callNum= function(num){
	  console.log(num + ':ok');
  };
  for(var num=0;num<selectSqls.length;num++){
	  var sqlNum = selectSqls[num];
	  //console.log(sqlNum);
	  select(sqlNum,callNum);
  }

这个确实比较绕,还是用 promise 比较容易看

var pending = null;
http.createServer(function(req, res) {
  if (pending === null) { 
    pending = db.queryAsync("...");
  }
  pending.then(function(result) {
    res.send(result);
    pending = null;
  });
});
回到顶部