Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added watchdog for ping going into the peer_client. #247

Merged
merged 1 commit into from
Oct 26, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions lib/peer_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ var EventEmitter = require('events').EventEmitter;
var path = require('path');
var util = require('util');
var uuid = require('node-uuid');
var spdy = require('spdy');
var Logger = require('./logger');
var WebSocket = require('./web_socket');

// monkey patch spdy connection to get access to ping event
var originalPingHandler = spdy.Connection.prototype._handlePing;
spdy.Connection.prototype._handlePing = function() {
this.socket.emit('spdyPing', this);
originalPingHandler.apply(this, arguments);
};

function calculatePeerUrl(url, name){
var wsUrl = url.replace(/^http/, 'ws');
var peerPath = '/peers/' + name;
Expand All @@ -16,14 +24,16 @@ function calculatePeerUrl(url, name){
return wsUrl;
}


var PeerClient = module.exports = function(url, server) {
this.reconnect = {
min: 100,
max: 30000, // max amount of time allowed to backoff
maxRandomOffset: 1000, // max amount of time
};

// 3x the interval the peer pings down to the client
this.pingTimeout = 30000;

this.server = server.httpServer.spdyServer;
this.connected = false;
this.retryCount = 0;
Expand All @@ -35,7 +45,7 @@ var PeerClient = module.exports = function(url, server) {
this._zetta = server;

this.updateURL(url);

// create a unique connection id peer connection, used to associate initiaion request
this.connectionId = null;
this.ws = new WebSocket(this._createNewUrl(), {});
Expand Down Expand Up @@ -73,6 +83,23 @@ PeerClient.prototype.close = function() {
clearTimeout(this._backoffTimer);
this._stopped = true;
this.ws.close();
this._stopPingTimeout();
};

PeerClient.prototype._resetPingTimeout = function() {
var self = this;
clearTimeout(this._pingTimer);
this._pingTimer = setTimeout(function() {
if (self.ws) {
self.log.emit('warn', 'peer-client', 'Communication to peer timed out. Reconnecting (' + self.url + ')');
self.emit('timeout');
self.ws.close();
}
}, this.pingTimeout);
};

PeerClient.prototype._stopPingTimeout = function() {
clearTimeout(this._pingTimer);
};

PeerClient.prototype._createSocket = function() {
Expand All @@ -86,16 +113,27 @@ PeerClient.prototype._createSocket = function() {
if (this._stopped) {
return;
}

// stop ping timer until backoff finishes
self._stopPingTimeout();

var backoff = this.generateBackoff(this.retryCount);
this._backoffTimer = setTimeout(function(){

// start ping timer
self._resetPingTimeout();

// create a new connection id
self.ws.setAddress(self._createNewUrl());
if (self.retryCount === 0) {
self.ws.on('open', function onOpen(socket) {
self.checkServerReq();
self.emit('connecting');
self.server.emit('connection', socket);
socket.on('spdyPing', function(connection) {
// reset ping timer on a spdy ping from the peer
self._resetPingTimeout();
});
self.log.emit('log', 'peer-client', 'WebSocket to peer established (' + self.url + ')');
});

Expand Down