-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
.yarn/patches/@polkadot-api-ws-provider-npm-0.3.6-39c80167fe.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
diff --git a/dist/node/esm/node.mjs b/dist/node/esm/node.mjs | ||
index 8707f2d1ba22e2f249330c7e59ed039237d4d7b9..d7352d6561977c758cfc69d76c8a7d4bb940e8b5 100644 | ||
--- a/dist/node/esm/node.mjs | ||
+++ b/dist/node/esm/node.mjs | ||
@@ -2,10 +2,32 @@ import { WebSocket } from 'ws'; | ||
import { getInternalWsProvider } from './ws-provider.mjs'; | ||
export { WsEvent } from './types.mjs'; | ||
|
||
+const PING_TIMEOUT = 45_000; | ||
+ | ||
class WS extends WebSocket { | ||
close() { | ||
this.terminate(); | ||
} | ||
+ // Triggers "close" on network failure | ||
+ // (e.g. when pulling the cord, datacenter network infra migrations, &c.) | ||
+ stayAlive() { | ||
+ function heartbeat() { | ||
+ clearTimeout(this.pingTimeout); | ||
+ | ||
+ // Use `WebSocket#terminate()`, which immediately destroys the connection, | ||
+ // instead of `WebSocket#close()`, which waits for the close timer. | ||
+ // Safe assumption of 45 seconds ping frequency + latency. | ||
+ this.pingTimeout = setTimeout(() => { | ||
+ console.warn(`Terminate: ping timeout (${PING_TIMEOUT/1_000}s)`); | ||
+ this.terminate(); | ||
+ }, PING_TIMEOUT); | ||
+ } | ||
+ | ||
+ this.on('ping', heartbeat); | ||
+ this.on('close', function clear() { | ||
+ clearTimeout(this.pingTimeout); | ||
+ }); | ||
+ } | ||
} | ||
const getWsProvider = getInternalWsProvider( | ||
WS | ||
diff --git a/dist/node/esm/ws-provider.mjs b/dist/node/esm/ws-provider.mjs | ||
index 867316423225f21bd35ed3e2fc60ff330dd20ec3..61a065fd1bbbbcc3de603d3f73efeaf2efdfc004 100644 | ||
--- a/dist/node/esm/ws-provider.mjs | ||
+++ b/dist/node/esm/ws-provider.mjs | ||
@@ -40,6 +40,7 @@ const getInternalWsProvider = (WebsocketClass) => { | ||
const [uri, protocols] = switchTo || endpoints[idx++ % endpoints.length]; | ||
switchTo = null; | ||
const socket = new WebsocketClass(uri, protocols); | ||
+ socket.stayAlive && socket.stayAlive(); | ||
const forceSocketClose = () => { | ||
try { | ||
socket.addEventListener("error", noop, { once: true }); | ||
@@ -71,7 +72,8 @@ const getInternalWsProvider = (WebsocketClass) => { | ||
event: e | ||
} | ||
); | ||
- setTimeout(reject, e ? 300 : 0, e); | ||
+ // don't retry too fast | ||
+ setTimeout(reject, 5_000, e); | ||
}; | ||
const timeoutToken = timeout !== Infinity ? setTimeout(() => { | ||
initialCleanup(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters