Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
[PAY-1463] Fix d to set discovery node (#3690)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Piazza <michael.piazza.mp@gmail.com>
  • Loading branch information
rickyrombo and piazzatron authored Jul 7, 2023
1 parent 12fe88d commit 8bc0a02
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
8 changes: 0 additions & 8 deletions packages/common/src/services/local-storage/LocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Nullable } from '../../utils'

// TODO: the following should come from @audius/libs/dist/core when
// discoveryProvider/constants is migrated to typescript.
const DISCOVERY_PROVIDER_TIMESTAMP = '@audius/libs:discovery-node-timestamp'

const AUDIUS_ACCOUNT_KEY = '@audius/account'
const AUDIUS_ACCOUNT_USER_KEY = '@audius/audius-user'

Expand Down Expand Up @@ -116,10 +114,4 @@ export class LocalStorage {

getCurrentUserExists = async () =>
this.getValue(CURRENT_USER_EXISTS_LOCAL_STORAGE_KEY)

async getCachedDiscoveryProvider() {
return await this.getJSONValue<CachedDiscoveryProviderType>(
DISCOVERY_PROVIDER_TIMESTAMP
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ import {
type DiscoveryNodeSelectorConfig = {
env: Env
remoteConfigInstance: RemoteConfigInstance
initialSelectedNode?: string
onChange?: (endpoint: string) => void
}

export class DiscoveryNodeSelectorService {
private env: Env
private remoteConfigInstance: RemoteConfigInstance
private discoveryNodeSelectorPromise: Promise<DiscoveryNodeSelector> | null
private initialSelectedNode: string | undefined
private onChange: ((endpoint: string) => void) | undefined

constructor(config: DiscoveryNodeSelectorConfig) {
const { env, remoteConfigInstance } = config
const { env, remoteConfigInstance, initialSelectedNode, onChange } = config
this.env = env
this.remoteConfigInstance = remoteConfigInstance
this.discoveryNodeSelectorPromise = null
this.initialSelectedNode = initialSelectedNode
this.onChange = onChange
}

private async makeDiscoveryNodeSelector() {
Expand Down Expand Up @@ -59,12 +65,17 @@ export class DiscoveryNodeSelectorService {
const requestTimeout =
getRemoteVar(IntKeys.DISCOVERY_PROVIDER_SELECTION_TIMEOUT_MS) ?? undefined

return new DiscoveryNodeSelector({
const dnSelector = new DiscoveryNodeSelector({
healthCheckThresholds,
blocklist,
requestTimeout,
bootstrapServices: discoveryNodes
bootstrapServices: discoveryNodes,
initialSelectedNode: this.initialSelectedNode
})
if (this.onChange) {
dnSelector.addEventListener('change', this.onChange)
}
return dnSelector
}

private getBlockList(remoteVarKey: StringKeys) {
Expand Down
64 changes: 63 additions & 1 deletion packages/web/src/services/audius-sdk/discoveryNodeSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,69 @@ import { DiscoveryNodeSelectorService } from '@audius/common'
import { env } from '../env'
import { remoteConfigInstance } from '../remote-config/remote-config-instance'

const DISCOVERY_PROVIDER_TIMESTAMP = '@audius/libs:discovery-node-timestamp'
const CACHE_TTL = 24 * 60 * 1000 // 24 hours

type CachedDiscoveryNodeTimestamp =
| {
endpoint?: string
timestamp?: number
}
| null
| undefined

const getCachedDiscoveryNode = () => {
const cached = localStorage.getItem(DISCOVERY_PROVIDER_TIMESTAMP)
if (cached) {
try {
const cachedDiscoveryNodeTimestamp = JSON.parse(
cached
) as CachedDiscoveryNodeTimestamp
if (
cachedDiscoveryNodeTimestamp?.timestamp &&
cachedDiscoveryNodeTimestamp.timestamp >
new Date().getTime() - CACHE_TTL
) {
const cachedDiscoveryNode =
cachedDiscoveryNodeTimestamp.endpoint ?? undefined
console.debug(
'[discovery-node-selector-service] Using cached discovery node',
cachedDiscoveryNode
)
return cachedDiscoveryNode
} else {
console.debug(
'[discovery-node-selector-service] Cached discovery node expired',
cachedDiscoveryNodeTimestamp
)
}
} catch (e) {
console.error(
"[discovery-node-selector-service] Couldn't parse cached discovery node",
e
)
}
}
}

const updateCachedDiscoveryNode = (endpoint: string) => {
const newTimestamp: CachedDiscoveryNodeTimestamp = {
endpoint,
timestamp: new Date().getTime()
}
console.debug(
'[discovery-node-selector-service] Updating cached discovery provider',
newTimestamp
)
localStorage.setItem(
DISCOVERY_PROVIDER_TIMESTAMP,
JSON.stringify(newTimestamp)
)
}

export const discoveryNodeSelectorService = new DiscoveryNodeSelectorService({
env,
remoteConfigInstance
remoteConfigInstance,
initialSelectedNode: getCachedDiscoveryNode(),
onChange: updateCachedDiscoveryNode
})

0 comments on commit 8bc0a02

Please sign in to comment.