diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-05-23 10:30:33 +0200 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-05-24 15:00:44 +0200 |
| commit | ebbe43cb39bb4742aff6344d8efb11bba0d04afb (patch) | |
| tree | 9728d8c38baf76d64d75e0167d775731831c2110 /app | |
| parent | 864b3a2a64e3f7755800b6a0af95f6bf9364992f (diff) | |
| download | mullvadvpn-ebbe43cb39bb4742aff6344d8efb11bba0d04afb.tar.xz mullvadvpn-ebbe43cb39bb4742aff6344d8efb11bba0d04afb.zip | |
Add backoff to the reconnection logic
Diffstat (limited to 'app')
| -rw-r--r-- | app/lib/ipc.js | 33 |
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; + } +} |
