Connection backoff

This commit is contained in:
Tony Garnock-Jones 2019-06-20 14:39:05 +01:00
parent f04c743b57
commit dbfd5dc8e6
2 changed files with 40 additions and 8 deletions

View File

@ -21,6 +21,7 @@
"dependencies": { "dependencies": {
"@syndicate-lang/core": "^0.2.7", "@syndicate-lang/core": "^0.2.7",
"@syndicate-lang/driver-timer": "^0.2.7", "@syndicate-lang/driver-timer": "^0.2.7",
"debug": "^4.1.1",
"isomorphic-ws": "^4.0.1", "isomorphic-ws": "^4.0.1",
"ws": "^6.1.2" "ws": "^6.1.2"
} }

View File

@ -18,6 +18,7 @@
import { currentFacet, Bytes, Observe, Dataspace } from "@syndicate-lang/core"; import { currentFacet, Bytes, Observe, Dataspace } from "@syndicate-lang/core";
const { sleep } = activate require("@syndicate-lang/driver-timer"); const { sleep } = activate require("@syndicate-lang/driver-timer");
const debugFactory = require('debug');
const _WebSocket = require('isomorphic-ws'); const _WebSocket = require('isomorphic-ws');
@ -32,26 +33,56 @@ Object.assign(module.exports, {
spawn named 'WebSocketFactory' { spawn named 'WebSocketFactory' {
during Observe(WebSocket($id, $url, $options)) spawn named ['WebSocket', id, url] { during Observe(WebSocket($id, $url, $options)) spawn named ['WebSocket', id, url] {
const facet = currentFacet(); const facet = currentFacet();
const debug = debugFactory('syndicate/driver-websocket:' + (id && id.toString()));
let finish = Dataspace.backgroundTask(); let finish = Dataspace.backgroundTask();
on stop finish(); on stop finish();
const DEFAULT_RECONNECT_DELAY = 1000; // milliseconds
const MAX_RECONNECT_DELAY = 30000; // milliseconds
let ws = null; let ws = null;
field this.connected = false; field this.connected = false;
field this.reconnectDelay = DEFAULT_RECONNECT_DELAY;
const guard = (context, f) => {
try {
f()
} catch (e) {
// Swallow e, which will be some kind of websocket-related exception.
debug('exception in actor '+facet.actor.toString()+' during '+context+':', e.message);
facet.stop();
}
};
const connect = () => { const connect = () => {
disconnect(); disconnect();
console.log('WebSocket', id, url, 'connecting'); debug(url, 'connecting');
ws = new _WebSocket(url, [], options.toJS()); ws = new _WebSocket(url, [], options.toJS());
ws.onerror = Dataspace.wrapExternal((e) => { ws.onerror = Dataspace.wrapExternal((e) => {
console.error('WebSocket', id, url, e); debug(url, e.message);
disconnect(); disconnect();
sleep(1000, connect); if (this.reconnectDelay !== DEFAULT_RECONNECT_DELAY) {
debug('Will reconnect in '+Math.floor(this.reconnectDelay)+' milliseconds');
}
sleep(Math.floor(this.reconnectDelay), connect);
this.reconnectDelay = this.reconnectDelay * 1.618 + (Math.random() * 1000);
this.reconnectDelay =
this.reconnectDelay > MAX_RECONNECT_DELAY
? MAX_RECONNECT_DELAY + (Math.random() * 1000)
: this.reconnectDelay;
}); });
ws.onopen = Dataspace.wrapExternal(() => { this.connected = true; }); ws.onopen = Dataspace.wrapExternal(() => {
ws.onclose = Dataspace.wrapExternal(() => { if (this.connected) { connect(); }}); this.connected = true;
this.reconnectDelay = DEFAULT_RECONNECT_DELAY;
});
ws.onclose = Dataspace.wrapExternal(() => {
if (this.connected) {
connect();
}
});
ws.onmessage = Dataspace.wrapExternal((data) => { ws.onmessage = Dataspace.wrapExternal((data) => {
if (typeof Blob !== 'undefined' && data.data instanceof Blob) { if (typeof Blob !== 'undefined' && data.data instanceof Blob) {
var reader = new FileReader(); var reader = new FileReader();
@ -67,8 +98,8 @@ spawn named 'WebSocketFactory' {
const disconnect = () => { const disconnect = () => {
if (ws) { if (ws) {
console.log('WebSocket', id, url, 'disconnecting'); debug(url, 'disconnecting');
try { ws.close(); } catch (_e) {} guard('close', () => ws.close());
ws = null; ws = null;
} }
this.connected = false; this.connected = false;
@ -81,7 +112,7 @@ spawn named 'WebSocketFactory' {
on message DataOut(id, $data) { on message DataOut(id, $data) {
if (this.connected) { if (this.connected) {
ws.send(Bytes.toIO(data)); guard('send', () => ws.send(Bytes.toIO(data)));
} }
} }
} }