summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorErik Larkö <erik@mullvad.net>2017-05-23 10:30:33 +0200
committerErik Larkö <erik@mullvad.net>2017-05-24 15:00:44 +0200
commitebbe43cb39bb4742aff6344d8efb11bba0d04afb (patch)
tree9728d8c38baf76d64d75e0167d775731831c2110
parent864b3a2a64e3f7755800b6a0af95f6bf9364992f (diff)
downloadmullvadvpn-ebbe43cb39bb4742aff6344d8efb11bba0d04afb.tar.xz
mullvadvpn-ebbe43cb39bb4742aff6344d8efb11bba0d04afb.zip
Add backoff to the reconnection logic
-rw-r--r--app/lib/ipc.js33
1 files changed, 31 insertions, 2 deletions
diff --git a/app/lib/ipc.js b/app/lib/ipc.js
index 81fad0c1ef..5372611404 100644
--- a/app/lib/ipc.js
+++ b/app/lib/ipc.js
@@ -10,6 +10,7 @@ export default class Ipc {
this._unansweredRequests = {};
this._subscriptions = {};
+ this._backoff = new ReconnectionBackoff();
this._reconnect();
}
@@ -107,6 +108,8 @@ export default class Ipc {
this._websocket.onopen = () => {
log.debug('Websocket is connected');
+ this._backoff.successfullyConnected();
+
while(this._onConnect.length > 0) {
this._onConnect.pop().resolve();
}
@@ -117,8 +120,34 @@ export default class Ipc {
};
this._websocket.onclose = () => {
- log.warn('The websocket connetion closed, attempting to reconnect it');
- this._reconnect();
+ const delay = this._backoff.getIncreasedBackoff();
+ log.warn('The websocket connetion closed, attempting to reconnect it in', delay, 'milliseconds');
+ setTimeout(() => this._reconnect(), delay);
};
}
}
+
+/*
+ * Used to calculate the time to wait before reconnecting
+ * the websocket.
+ *
+ * It uses a linear backoff function that goes from 500ms
+ * to 3000ms
+ */
+class ReconnectionBackoff {
+ constructor() {
+ this._attempt = 0;
+ }
+
+ successfullyConnected() {
+ this._attempt = 0;
+ }
+
+ getIncreasedBackoff() {
+ if (this._attempt < 6) {
+ this._attempt++;
+ }
+
+ return this._attempt * 500;
+ }
+}