diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-05-27 19:32:32 +0200 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-05-27 19:32:32 +0200 |
| commit | 27a2a447cddbd55055afe9de1b9a0590c8b97bf1 (patch) | |
| tree | 733e28d17159e9b71baeb6074746648d691fa82b /app/lib | |
| parent | 02c3997d7ae6aaea112448bdfe9c7a0cf100ea02 (diff) | |
| parent | 858e865b33b8145f784e0f1b621bc64105a55980 (diff) | |
| download | mullvadvpn-27a2a447cddbd55055afe9de1b9a0590c8b97bf1.tar.xz mullvadvpn-27a2a447cddbd55055afe9de1b9a0590c8b97bf1.zip | |
Merge branch 'timeouts'
Diffstat (limited to 'app/lib')
| -rw-r--r-- | app/lib/ipc.js | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/app/lib/ipc.js b/app/lib/ipc.js index 5372611404..8471360c9c 100644 --- a/app/lib/ipc.js +++ b/app/lib/ipc.js @@ -2,6 +2,8 @@ import jsonrpc from 'jsonrpc-lite'; import uuid from 'uuid'; import log from 'electron-log'; +const DEFAULT_TIMEOUT_MILLIS = 750; + export default class Ipc { constructor(connectionString) { @@ -52,12 +54,29 @@ export default class Ipc { const id = uuid.v4(); const jsonrpcMessage = jsonrpc.request(id, action, data); - this._unansweredRequests[id] = {resolve: resolve, reject: reject}; + const timeout = setTimeout(() => this._onTimeout(id), DEFAULT_TIMEOUT_MILLIS); + this._unansweredRequests[id] = { + resolve: resolve, + reject: reject, + timeout: timeout, + }; log.debug('Sending message', id, action); websocket.send(jsonrpcMessage); }); } + _onTimeout(requestId) { + const request = this._unansweredRequests[requestId]; + delete this._unansweredRequests[requestId]; + + if (!request) { + log.debug(requestId, 'timed out but it seems to already have been answered'); + return; + } + + request.reject('The request timed out'); + } + _onMessage(message) { const json = JSON.parse(message); const c = jsonrpc.parseObject(json); @@ -92,6 +111,9 @@ export default class Ipc { } log.debug('Got answer to', id, message.type); + + clearTimeout(request.timeout); + if (message.type === 'error') { request.reject(message.payload.error.message); } else { |
