Node.js TCP / IP服务器队列不工作

我正在写一个TCP / IP队列,应该限制最大连接用户数为3,并让其他人等到用户断开连接。 这些是我正在使用的function:

var maxClients = 3; var currentClients = 0; var _pending = []; function process_pending() { if (_pending.length > 0) { var client = _pending.shift(); currentClients++; client(function() { currentClients--; process.nextTick(process_pending); }); } } function client_limit(client) { if (currentClients < maxClients) { currentClients++; client(function() { currentClients--; process.nextTick(process_pending); }); } else { console.log('Overloaded, queuing clients...'); _pending.push(client); } } 

每当三个客户端连接,然后第四个尝试连接它显示Overloaded消息,这没关系,但是如果现有的客户端断开连接,没有人可以连接。 看起来像

 client(function() { currentClients--; process.nextTick(process_pending); }); 

工作不正常,我该如何解决?