Skip to content

Commit

Permalink
fix(healthcheck): waiting until at least one node is online
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed Aug 3, 2024
1 parent 238e28c commit 28eff49
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/lib/nodes/abstract.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,39 @@ export abstract class Client<N extends Node> {
*/
type: NodeType

/**
* Resolves when at least one node is ready to accept requests
*/
ready: Promise<void>
resolve = () => {}
initialized = false

constructor(type: NodeType, kind: NodeKind = 'node') {
this.type = type
this.kind = kind
this.useFastest = nodesStorage.getUseFastest(type)

this.ready = new Promise((resolve) => {
this.resolve = () => {
if (this.initialized) return

this.initialized = true
resolve()
}
})
}

protected async watchNodeStatusChange() {
for (const node of this.nodes) {
node.onStatusChange((node) => {
node.onStatusChange((nodeStatus) => {
this.updateSyncStatuses()

this.statusUpdateCallback?.(node)
this.statusUpdateCallback?.(nodeStatus)

if (this.isActiveNode(node)) {
// Resolve when at least one node is ready to accept requests
this.resolve()
}
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/nodes/adm/AdmClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class AdmClient extends Client<AdmNode> {
* @param {RequestConfig} config request config
*/
async request<P extends Payload = Payload, R = any>(config: RequestConfig<P>): Promise<R> {
await this.ready

return this.getNode()
.request(config)
.catch((error) => {
Expand Down

0 comments on commit 28eff49

Please sign in to comment.