This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from dannycoates/stats
rewrote the ping logic to fix several ping related issues
- Loading branch information
Showing
4 changed files
with
91 additions
and
43 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
var inherits = require('util').inherits, | ||
EventEmitter = require('events').EventEmitter, | ||
Endpoint = require('./lib/endpoint')(inherits, EventEmitter), | ||
Pinger = require('./lib/pinger')(inherits, EventEmitter), | ||
Endpoint = require('./lib/endpoint')(inherits, EventEmitter, Pinger), | ||
RequestSet = require('./lib/request_set') | ||
|
||
module.exports = require('./lib/pool')(inherits, EventEmitter, Endpoint, RequestSet) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module.exports = function (inherits, EventEmitter) { | ||
|
||
function Pinger(request) { | ||
this.onPingResponse = pingResponse.bind(this) | ||
this.request = request.bind(this, this.onPingResponse) | ||
this.running = false | ||
this.attempts = 0 | ||
EventEmitter.call(this) | ||
} | ||
inherits(Pinger, EventEmitter) | ||
|
||
function pingResponse(error, response, body) { | ||
if (!error && response.statusCode === 200) { | ||
this.emit('pong') | ||
this.running = false | ||
} | ||
else { | ||
this.attempts++ | ||
this.ping() | ||
} | ||
} | ||
|
||
function exponentialBackoff(attempt) { | ||
return Math.min( | ||
Math.floor(Math.random() * Math.pow(2, attempt) + 10), | ||
10000) | ||
} | ||
|
||
Pinger.prototype.ping = function () { | ||
if (this.attempts) { | ||
setTimeout(this.request, exponentialBackoff(this.attempts)) | ||
} | ||
else { | ||
this.request() | ||
} | ||
} | ||
|
||
Pinger.prototype.start = function () { | ||
if (!this.running) { | ||
this.running = true | ||
this.attempts = 0 | ||
this.ping() | ||
} | ||
} | ||
|
||
return Pinger | ||
} |
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