API health only with providers with subscriptions (#3879)

This commit is contained in:
Jaco
2021-08-16 10:40:10 +02:00
committed by GitHub
parent bfd5642d64
commit 817aaccbb1
2 changed files with 18 additions and 4 deletions
+7
View File
@@ -1,5 +1,12 @@
# CHANGELOG
## master
Changes:
- Only enable API health keepalive on connections supporting subscriptions
## 5.5.1 Aug 15, 2021
Upgrade priority: Low. Recommended for users of TS (`createType` and `bigint` adjustments)
+11 -4
View File
@@ -332,6 +332,15 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
return true;
}
private _subscribeHealth (): void {
// Only enable the health keepalive on WS, not needed on HTTP
this.#healthTimer = this.hasSubscriptions
? setInterval((): void => {
firstValueFrom(this._rpcCore.system.health()).catch(() => undefined);
}, KEEPALIVE_INTERVAL)
: null;
}
private _unsubscribeHealth (): void {
if (this.#healthTimer) {
clearInterval(this.#healthTimer);
@@ -363,15 +372,13 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
: cryptoWaitReady()
]);
this._subscribeHealth();
if (hasMeta && !this._isReady && cryptoReady) {
this._isReady = true;
this.emit('ready', this);
}
this.#healthTimer = setInterval((): void => {
firstValueFrom(this._rpcCore.system.health()).catch(() => undefined);
}, KEEPALIVE_INTERVAL);
} catch (_error) {
const error = new Error(`FATAL: Unable to initialize the API: ${(_error as Error).message}`);