Skip to content

Commit ed2a89e

Browse files
authored
Merge pull request #18 from bitcoinerlab/more-aggressive-reconnect
feat: more robust/aggressive electrum reconnect strategy on failure
2 parents 5c808fc + 70be8c1 commit ed2a89e

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@bitcoinerlab/explorer",
33
"description": "Bitcoin Blockchain Explorer: Client Interface featuring Esplora and Electrum Implementations.",
44
"homepage": "https://github.com/bitcoinerlab/explorer",
5-
"version": "0.3.7",
5+
"version": "0.3.8",
66
"author": "Jose-Luis Landabaso",
77
"license": "MIT",
88
"prettier": "@bitcoinerlab/configs/prettierConfig.json",

src/electrum.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ export class ElectrumExplorer implements Explorer {
159159
client: 'bitcoinerlab',
160160
version: '1.4'
161161
},
162-
//don't let it handle auto-reconnect. Handling reconnection here
163-
//in #pingInterval and/or from the libraries using @bitcoinerlab/explorer
164-
{ maxRetry: 0, callback: null },
162+
{ maxRetry: 1000, callback: null },
165163
this.#timeout
166164
);
167165
this.#client.subscribe.on(
@@ -190,7 +188,7 @@ export class ElectrumExplorer implements Explorer {
190188
);
191189
}
192190

193-
// Ping every minute to keep connection alive.
191+
// Ping every few seconds to keep connection alive.
194192
// This function will never throw since it cannot be handled
195193
// In case of connection errors, users will get them on any next function
196194
// call
@@ -202,23 +200,23 @@ export class ElectrumExplorer implements Explorer {
202200
} catch (error: unknown) {
203201
shouldReconnect = true;
204202
console.warn(
205-
'Closing connection and reconnecting in 0.5s after ping error:',
203+
'Closing connection and reconnecting in 1s after ping error:',
206204
getErrorMsg(error)
207205
);
208206
}
209207
if (shouldReconnect) {
210208
try {
211209
if (await this.isConnected(false)) await this.close(); //hide possible socket errors
212-
await new Promise(resolve => setTimeout(resolve, 500));
210+
await new Promise(resolve => setTimeout(resolve, 1000));
213211
await this.connect();
214212
} catch (error) {
215213
console.warn(
216-
'Error while reconnecting connection while pinging the electrum server.',
214+
'Error while reconnecting during interval pinging:',
217215
getErrorMsg(error)
218216
);
219217
}
220218
}
221-
}, 60 * 1000); // 60 * 1000 ms = 1 minute
219+
}, 14 * 1000); // 14 * 1000 ms = 14 seconds
222220
}
223221

224222
/**

0 commit comments

Comments
 (0)