-
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 10 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,96 @@ | ||
// 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 abstract class ApiConnection { | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async create(endpoint: string, args: any): Promise<void> { | ||
throw new Error('create() is not supported'); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async apiConnect(): Promise<void> { | ||
throw new Error('apiConnect() is not supported'); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async apiDisconnect(): Promise<void> { | ||
throw new Error('apiDisconnect() is not supported'); | ||
} | ||
} | ||
|
||
@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()) | ||
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. Can use |
||
); | ||
} | ||
|
||
addToConnections(api: T): void { | ||
this.allApi.push(api); | ||
this.connectionPool[this.allApi.length - 1] = 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]; | ||
} | ||
|
||
get firstConnectedApi(): T { | ||
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. This seems to be unused, can be removed along with |
||
const index = this.getFirstConnectedApiIndex(); | ||
if (index === -1) { | ||
throw new Error('No connected api'); | ||
} | ||
return this.connectionPool[index]; | ||
} | ||
|
||
getNextConnectedApiIndex(): number { | ||
// get the next connected api index | ||
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]); | ||
} | ||
|
||
private getFirstConnectedApiIndex(): number { | ||
// get the first connected api index | ||
if (Object.keys(this.connectionPool).length === 0) { | ||
return -1; | ||
} | ||
return toNumber(Object.keys(this.connectionPool)[0]); | ||
} | ||
|
||
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]; | ||
|
||
logger.debug(`reconnecting to ${endpoint}...`); | ||
await this.connectToApi(apiIndex); | ||
|
||
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.
I think it makes more sense for this to be an interface