diff options
Diffstat (limited to 'bridge/ln.js')
| -rw-r--r-- | bridge/ln.js | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/bridge/ln.js b/bridge/ln.js index dce5254..7d1974c 100644 --- a/bridge/ln.js +++ b/bridge/ln.js @@ -33,8 +33,34 @@ function dockerAdapter({ container, network }) { }; } -function socketAdapter({ path }) { +// A socket that refuses every call for a minute is not coming back by itself +// when the socket FILE is bind-mounted into a container: lightningd makes a +// new file when it restarts, and the mount still points at the old one. The +// owner gets told through cfg.onDead (names exits there, and docker's restart +// mounts the new file). `quiet` is how long before it may fire: a minute once +// this process has reached the node, ten if it never has, so a node that is +// simply down doesn't restart its client in a loop. +export function deadSocketWatch(onDead, { streakMs = 60_000, coldMs = 600_000, now = Date.now } = {}) { + const born = now(); + let since = 0, fails = 0, everUp = false, fired = false; + return { + ok() { everUp = true; since = 0; fails = 0; }, + fail(err) { + if (!['ECONNREFUSED', 'ENOENT', 'ENOTSOCK'].includes(err?.code)) return; + const t = now(); + if (!since) since = t; + fails++; + if (fired || fails < 3 || t - since < streakMs) return; + if (!everUp && t - born < coldMs) return; + fired = true; + onDead(err); + }, + }; +} + +function socketAdapter({ path, onDead }) { let id = 0; + const watch = onDead ? deadSocketWatch(onDead) : null; return { call(method, params = {}) { return new Promise((resolve, reject) => { @@ -43,11 +69,11 @@ function socketAdapter({ path }) { // process dies with an uncaught exception. const sock = new Socket(); let buf = ''; - sock.on('error', reject); + sock.on('error', (e) => { watch?.fail(e); reject(e); }); sock.connect(path); - sock.on('connect', () => sock.write(JSON.stringify({ + sock.on('connect', () => { watch?.ok(); sock.write(JSON.stringify({ jsonrpc: '2.0', id: ++id, method, params, - }) + '\n')); + }) + '\n'); }); sock.on('data', (d) => { buf += d; let nl; |
