-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLAT-814] Use sdk discovery-node-selection in libs (#3125)
- Loading branch information
1 parent
58005c4
commit 1d78774
Showing
22 changed files
with
242 additions
and
194 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
89 changes: 89 additions & 0 deletions
89
packages/common/src/services/discovery-node-selector/DiscoveryNodeSelectorInstance.ts
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,89 @@ | ||
import { | ||
developmentConfig, | ||
DiscoveryNodeSelector, | ||
productionConfig, | ||
stagingConfig | ||
} from '@audius/sdk' | ||
|
||
import { Env } from '../env' | ||
import { | ||
BooleanKeys, | ||
IntKeys, | ||
RemoteConfigInstance, | ||
StringKeys | ||
} from '../remote-config' | ||
|
||
type DiscoveryNodeSelectorConfig = { | ||
env: Env | ||
remoteConfigInstance: RemoteConfigInstance | ||
} | ||
|
||
export class DiscoveryNodeSelectorInstance { | ||
private env: Env | ||
private remoteConfigInstance: RemoteConfigInstance | ||
private discoveryNodeSelectorPromise: Promise<DiscoveryNodeSelector> | null | ||
|
||
constructor(config: DiscoveryNodeSelectorConfig) { | ||
const { env, remoteConfigInstance } = config | ||
this.env = env | ||
this.remoteConfigInstance = remoteConfigInstance | ||
this.discoveryNodeSelectorPromise = null | ||
} | ||
|
||
private async makeDiscoveryNodeSelector() { | ||
const { getRemoteVar, waitForRemoteConfig } = this.remoteConfigInstance | ||
|
||
await waitForRemoteConfig() | ||
|
||
const bootstrapConfig = | ||
this.env.ENVIRONMENT === 'development' | ||
? developmentConfig | ||
: this.env.ENVIRONMENT === 'staging' | ||
? stagingConfig | ||
: productionConfig | ||
|
||
const { minVersion, discoveryNodes } = bootstrapConfig | ||
|
||
const maxBlockDiff = | ||
getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_BLOCK_DIFF) ?? undefined | ||
const maxSlotDiffPlays = getRemoteVar( | ||
BooleanKeys.ENABLE_DISCOVERY_NODE_MAX_SLOT_DIFF_PLAYS | ||
) | ||
? getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_SLOT_DIFF_PLAYS) | ||
: null | ||
|
||
const healthCheckThresholds = { minVersion, maxBlockDiff, maxSlotDiffPlays } | ||
|
||
const blocklist = this.getBlockList(StringKeys.DISCOVERY_NODE_BLOCK_LIST) | ||
|
||
const requestTimeout = | ||
getRemoteVar(IntKeys.DISCOVERY_PROVIDER_SELECTION_TIMEOUT_MS) ?? undefined | ||
|
||
return new DiscoveryNodeSelector({ | ||
healthCheckThresholds, | ||
blocklist, | ||
requestTimeout, | ||
bootstrapServices: discoveryNodes | ||
}) | ||
} | ||
|
||
private getBlockList(remoteVarKey: StringKeys) { | ||
const list = this.remoteConfigInstance.getRemoteVar(remoteVarKey) | ||
if (list) { | ||
try { | ||
return new Set<string>(list.split(',')) | ||
} catch (e) { | ||
console.error(e) | ||
return null | ||
} | ||
} | ||
return null | ||
} | ||
|
||
public async getDiscoveryNodeSelector() { | ||
if (!this.discoveryNodeSelectorPromise) { | ||
this.discoveryNodeSelectorPromise = this.makeDiscoveryNodeSelector() | ||
} | ||
return await this.discoveryNodeSelectorPromise | ||
} | ||
} |
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 @@ | ||
export * from './DiscoveryNodeSelectorInstance' |
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
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
Oops, something went wrong.