diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-05-24 15:39:28 +0200 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-05-24 15:39:28 +0200 |
| commit | f0eeffed6cfd055cf1aa1d23d240a0a8ded42466 (patch) | |
| tree | 9728d8c38baf76d64d75e0167d775731831c2110 /app | |
| parent | 864b3a2a64e3f7755800b6a0af95f6bf9364992f (diff) | |
| parent | ebbe43cb39bb4742aff6344d8efb11bba0d04afb (diff) | |
| download | mullvadvpn-f0eeffed6cfd055cf1aa1d23d240a0a8ded42466.tar.xz mullvadvpn-f0eeffed6cfd055cf1aa1d23d240a0a8ded42466.zip | |
Merge branch 'backoff'
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; + } +} |
