-
Notifications
You must be signed in to change notification settings - Fork 114
/
peer_client.js
206 lines (169 loc) · 5.55 KB
/
peer_client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var EventEmitter = require('events').EventEmitter;
var path = require('path');
var util = require('util');
var uuid = require('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;
if(wsUrl.indexOf('/', wsUrl.length - 1) === -1) {
wsUrl = wsUrl + peerPath;
} else {
wsUrl = wsUrl.slice(0, wsUrl.length - 1) + peerPath;
}
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;
this.log = server.log || new Logger();
this._backoffTimer = null;
this._stopped = false;
// keep a copy of zetta server for calculating it's peer name
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(), {});
EventEmitter.call(this);
};
util.inherits(PeerClient, EventEmitter);
PeerClient.calculatePeerUrl = calculatePeerUrl;
PeerClient.prototype.updateURL = function(httpUrl) {
var wsUrl = calculatePeerUrl(httpUrl, this._zetta._name);
this.url = wsUrl;
};
PeerClient.prototype._createNewUrl = function() {
this.connectionId = uuid.v4();
return this.url + '?connectionId=' + this.connectionId;
};
PeerClient.prototype.properties = function() {
return {
url: this.url,
connectionId: this.connectionId,
};
};
PeerClient.prototype.start = function() {
this._stopped = false; // If previously closed, reset stopped flag
this._createSocket();
};
// Close and stop reconnecting
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() {
var self = this;
if (this.backoffTimer) {
clearTimeout(this.backoffTimer);
}
// once peer is closed dont create new socket
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 + ')');
});
self.ws.on('error', function onError(err) {
self.connected = false;
self.log.emit('log', 'peer-client', 'Peer connection error (' + self.url + '): ' + err);
self.emit('error', err);
reconnect(err);
});
self.ws.on('close', function(code, message) {
//if (self.retryCount > 0) throw new Error('wtf');
self.connected = false;
self.log.emit('log', 'peer-client', 'Peer connection closed (' + self.url + '): ' + code + ' - ' + message);
self.emit('closed');
reconnect();
});
}
self.ws.start();
}, backoff);
function reconnect(err) {
self.retryCount++;
self._createSocket();
}
};
PeerClient.prototype.checkServerReq = function() {
var self = this;
// remove any previous request listeners
if (self.onRequest) {
this.server.removeListener('request', self.onRequest);
}
// /_initiate_peer/{connection-id}
this.onRequest = function(req, res) {
if (req.url === '/_initiate_peer/' + self.connectionId) {
self.connected = true;
self.retryCount = 0;
self.emit('connected');
self.log.emit('log', 'peer-client', 'Peer connection established (' + self.url + ')');
res.statusCode = 200;
res.end();
// remove request listener
self.server.removeListener('request', self.onRequest);
// set up exchange of reactive queries.
}
};
this.server.on('request', this.onRequest);
};
PeerClient.prototype.generateBackoff = function(attempt) {
if (attempt === 0) {
return 0;
}
var random = parseInt(Math.random() * this.reconnect.maxRandomOffset);
var backoff = (Math.pow(2, attempt) * this.reconnect.min);
if (backoff > this.reconnect.max) {
return this.reconnect.max + random;
} else {
return backoff + random;
}
};