Permalink
Browse files

Using a timestamp rather that a bool to more accurately track API rat…

…e-limit
  • Loading branch information...
1 parent b6a524b commit 88748cb9328a420015159ec59cab0b2c2e7d7811 @benrhughes committed Feb 19, 2012
Showing with 8 additions and 4 deletions.
  1. +8 −4 lib/posterous.js
View
@@ -5,7 +5,7 @@ var api = require('./api');
var HOST = 'posterous.com';
var PATH = '/api/2/';
-var callExecuting = false;
+var lastCallMade = Date.now() - 1000;
// Unfortunately Posterous rate-limites API calls to 1/sec. Passing
// forceSynchronous = true will ensure that the calls comply with this limit.
@@ -85,14 +85,18 @@ Posterous.prototype.request = function(method, api, args, callback) {
};
Posterous.prototype.makeRequestSync = function(method, args){
- if(this._synchronous && callExecuting){
+ var gap = Date.now() - lastCallMade;
+ console.log(gap + ' ms since last execution completed');
+
+ if(this._synchronous && gap < 1000){
+ console.log('sleeping');
var that = this;
setTimeout(function(){that.makeRequestSync(method, args)}, 1000);
}
else{
- callExecuting = true;
+ lastCallMade = Date.now();
this.makeRequest(method, args, function(){
- callExecuting = false;
+ lastCallMade = Date.now();
});
}
}

0 comments on commit 88748cb

Please sign in to comment.