Created
November 24, 2010 19:44
-
-
Save itissid/714259 to your computer and use it in GitHub Desktop.
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
for(doc=0 ; doc<this.docs.length; doc++ ){ | |
var self = this; | |
//Need to scale as too many connections are formed too quickly.. Server will drown...\ | |
//Suggested: process all the data through a Queue and an interval... | |
//The counter implementation is flaky... | |
(function testServer(data, docnum){ | |
var s = new net.Stream(); | |
s.setEncoding('utf8'); | |
s.on('connect', function(){ | |
//console.log('***Sending data for document:'+docnum); | |
//console.log(data.length); | |
self.sentButNotProcessed++;//actually opened connections | |
s.write(data+'\n', encoding='utf8'); | |
}) | |
//handler to recieve the data back after the inference. /Recieve the inference results. | |
s.on('data', function(data){ | |
//console.log('*******DATA RECIEVED FOR:'+docnum); | |
console.log(data); | |
self.procesedDocs++; | |
}) | |
s.on('close', function(had_error){ | |
//Should mean connection is closed by the server. | |
}) | |
s.on('end', function(){ | |
s.end(); | |
self.connCount--; | |
//console.log('***Active connections count:: '+self.connCount);//these many docs recieved atleast | |
self.sentButNotProcessed--;//How many are currently in the Queue | |
//Wait for the queue to drain. | |
}) | |
if(self.connCount < 500){ | |
self.connCount++;//Increases at full speed due to v8 loop | |
console.log('connCount: '+self.connCount); | |
s.connect(socketPath); | |
}else{ | |
//Busy wait here until the bucket is empty. | |
//This will stop the for loop to empty itself... | |
setInterval(function(){ | |
console.log('*** Waiting in the interval'+self.connCount); | |
if(self.connCount < 10){ | |
clearInterval(arguments.callee); | |
console.log('Should be out of here'+arguments.callee); | |
return; | |
}else{ | |
console.log("***WAITING IN INTERVAL. Connection count: "+self.connCount); | |
} | |
}, 3000) | |
} | |
})(this.docs[doc], doc); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment