Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated superagent and check memory leaks

  • Loading branch information...
commit ce2c48030e3ee8bcf1f1f14382d4261f711ec27b 1 parent b9e4d9d
@impronunciable authored
Showing with 32 additions and 11 deletions.
  1. +31 −10 lib/tuiter.js
  2. +1 −1  package.json
View
41 lib/tuiter.js
@@ -96,6 +96,7 @@ var APIRequest = function(endpoint, params, callback) {
var res = new NJStream('\r');
res.emitAPIEvents();
this.autoReconnect(req, res, arguments);
+
req.pipe(res);
callback(res);
} else {
@@ -110,7 +111,7 @@ var APIRequest = function(endpoint, params, callback) {
* Auto reconnect
*/
-Tuiter.prototype.autoReconnect = function(req, res, args){
+Tuiter.prototype.autoReconnect = function(req, resp, args){
var self = this;
this.tcp_timeout = this.tcp_timeout || 250;
@@ -119,22 +120,34 @@ Tuiter.prototype.autoReconnect = function(req, res, args){
req.on('error', function(error){
debug("tcp error encountered, starting reconnection process");
- req = res = null;
+ resp.emit('error', error);
+
setTimeout(function(){ APIRequest.apply(self, args); }, self.tcp_timeout);
+
self.tcp_timeout *= 2;
});
req.on('response', function(res){
if(res.statusCode > 200 && res.statusCode !== 420) {
debug("http error encountered, starting reconnection process");
- req = res = null;
- setTimeout(function(){ APIRequest.apply(self, args); }, self.http_timeout);
+
+ resp.emit('error', {statusCode: res.statusCode, text: res.text});
+
+ setTimeout(function(){
+ APIRequest.apply(self, args);
+ }, self.http_timeout);
+
self.http_timeout = Math.min(self.http_timeout + 250, 32000);
}
else if(res.statusCode === 420) {
debug("rate limit error encountered, starting reconnection process");
- req = res = null;
- setTimeout(function(){ APIRequest.apply(self, args); }, self.rate_timeout);
+
+ resp.emit('error', {statusCode: res.statusCode, text: res.text});
+
+ setTimeout(function(){
+ APIRequest.apply(self, args);
+ }, self.rate_timeout);
+
self.rate_timeout *= 2;
} else {
self.rate_timeout = null;
@@ -144,16 +157,24 @@ Tuiter.prototype.autoReconnect = function(req, res, args){
res.on('error', function(error){
debug("rate limit error encountered, starting reconnection process");
- req = res = null;
- setTimeout(function(){ APIRequest.apply(self, args); }, self.rate_timeout);
+
+ resp.emit('error', {statusCode: res.statusCode, text: res.text});
+
+ setTimeout(function(){
+ APIRequest.apply(self, args);
+ }, self.rate_timeout);
+
self.rate_timeout *= 2;
});
res.on('end', function(){
if(res.statusCode === 200) {
- req = res = null;
- debug("tcp error encountered, starting reconnection process");
+ debug("connection ended, starting reconnection process");
+
+ APIRequest.apply(self, args);
+
self.tcp_timeout = null;
+ self.http_timeout = null;
}
});
});
View
2  package.json
@@ -7,7 +7,7 @@
, "main" : "index.js"
, "author" : "Dan Zajdband <dan.zajdband@gmail.com>"
, "dependencies" : {
- "superagent": "0.9.7"
+ "superagent": "0.9.8"
, "superagent-oauth": "0.2.x"
, "oauth": "0.9.8"
, "njstream": "0.0.5"
Please sign in to comment.
Something went wrong with that request. Please try again.