-
Notifications
You must be signed in to change notification settings - Fork 348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple endpoints #1551
Multiple endpoints #1551
Changes from all commits
9c4db6d
918ec08
8a0c5eb
11e051d
afdf918
97b63b6
6af8274
3f0032b
ac7b1f3
c0fe06f
bcb02fd
fea7d0d
d76f432
ffb3a1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2020-2022 OnFinality Limited authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {Injectable, OnApplicationShutdown} from '@nestjs/common'; | ||
import {toNumber} from 'lodash'; | ||
import {getLogger} from '../logger'; | ||
|
||
const logger = getLogger('api'); | ||
|
||
export interface ApiConnection { | ||
apiConnect(): Promise<void>; | ||
apiDisconnect(): Promise<void>; | ||
} | ||
|
||
@Injectable() | ||
export class ConnectionPoolService<T extends ApiConnection> implements OnApplicationShutdown { | ||
private allApi: T[] = []; | ||
private connectionPool: Record<number, T> = {}; | ||
private taskCounter = 0; | ||
|
||
async onApplicationShutdown(): Promise<void> { | ||
await Promise.all( | ||
Object.keys(this.connectionPool)?.map((key) => this.connectionPool[toNumber(key)].apiDisconnect()) | ||
); | ||
} | ||
|
||
addToConnections(api: T): void { | ||
this.allApi.push(api); | ||
this.connectionPool[this.allApi.length - 1] = api; | ||
} | ||
|
||
addBatchToConnections(apis: T[]): void { | ||
apis.forEach((api) => this.addToConnections(api)); | ||
} | ||
|
||
async connectToApi(apiIndex: number): Promise<void> { | ||
await this.allApi[apiIndex].apiConnect(); | ||
} | ||
|
||
get api(): T { | ||
const index = this.getNextConnectedApiIndex(); | ||
if (index === -1) { | ||
throw new Error('No connected api'); | ||
} | ||
return this.connectionPool[index]; | ||
} | ||
|
||
getNextConnectedApiIndex(): number { | ||
if (Object.keys(this.connectionPool).length === 0) { | ||
return -1; | ||
} | ||
const nextIndex = this.taskCounter % Object.keys(this.connectionPool).length; | ||
this.taskCounter++; | ||
return toNumber(Object.keys(this.connectionPool)[nextIndex]); | ||
} | ||
|
||
get numConnections(): number { | ||
return Object.keys(this.connectionPool).length; | ||
} | ||
|
||
async handleApiDisconnects(apiIndex: number, endpoint: string): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe its worth adding in here a check that we have at least some connections. It would be worth testing what happens if they all disconnect (turn off your internet) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it retries to fetch about 5 times and exits after that |
||
logger.warn(`disconnected from ${endpoint}`); | ||
delete this.connectionPool[apiIndex]; | ||
|
||
try { | ||
logger.debug(`reconnecting to ${endpoint}...`); | ||
await this.connectToApi(apiIndex); | ||
} catch (e) { | ||
logger.error(`unable to reconnect to endpoint ${endpoint}`, e); | ||
return; | ||
} | ||
|
||
logger.info(`reconnected to ${endpoint}!`); | ||
this.connectionPool[apiIndex] = this.allApi[apiIndex]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -108,7 +108,7 @@ export class ConfigureModule { | |||||
config.subquery, | ||||||
omitBy<SubstrateProjectNetworkConfig>( | ||||||
{ | ||||||
endpoint: config.networkEndpoint, | ||||||
endpoint: config.networkEndpoints, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's |
||||||
dictionary: config.networkDictionary, | ||||||
}, | ||||||
isNil, | ||||||
|
@@ -170,7 +170,7 @@ export class ConfigureModule { | |||||
argv.subquery, | ||||||
omitBy<SubstrateProjectNetworkConfig>( | ||||||
{ | ||||||
endpoint: config.networkEndpoint, | ||||||
endpoint: config.networkEndpoints, | ||||||
dictionary: config.networkDictionary, | ||||||
}, | ||||||
isNil, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can use
Object.entries
here