-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move server status monitor into background
- Loading branch information
Showing
12 changed files
with
239 additions
and
73 deletions.
There are no files selected for viewing
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
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
101 changes: 101 additions & 0 deletions
101
packages/yoroi-extension/chrome/extension/background/serverStatus.js
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,101 @@ | ||
// @flow | ||
import { ConceptualWalletSchema } from '../../../app/api/ada/lib/storage/database/walletTypes/core/tables'; | ||
import { raii, getAll } from '../../../app/api/ada/lib/storage/database/utils'; | ||
import { getDb } from './state'; | ||
import { networks } from '../../../app/api/ada/lib/storage/database/prepackaged/networks'; | ||
import type { NetworkRow } from '../../../app/api/ada/lib/storage/database/primitives/tables'; | ||
import type { ConceptualWalletRow } from '../../../app/api/ada/lib/storage/database/walletTypes/core/tables'; | ||
import { getCommonStateFetcher } from './utils'; | ||
import environment from '../../../app/environment'; | ||
import type { ServerStatus } from './types'; | ||
import { | ||
getSubscriptions, | ||
registerCallback, | ||
emitUpdateToSubscriptions, | ||
} from './subscriptionManager'; | ||
|
||
const serverStatusByNetworkId: Map<number, ServerStatus> = new Map(); | ||
|
||
async function getAllNetworks(): Promise<$ReadOnlyArray<NetworkRow>> { | ||
const db = await getDb(); | ||
const allConceptualWallets = await raii<$ReadOnlyArray<ConceptualWalletRow>>( | ||
db, | ||
[], | ||
tx => getAll( | ||
db, tx, | ||
ConceptualWalletSchema.name, | ||
) | ||
); | ||
const allNetworkIdSet = new Set(allConceptualWallets.map(w => w.NetworkId)); | ||
return Object.keys(networks).map(n => networks[n]).filter( | ||
({ NetworkId }) => allNetworkIdSet.has(NetworkId) | ||
); | ||
} | ||
|
||
async function updateServerStatus() { | ||
const networks = await getAllNetworks(); | ||
const fetcher = await getCommonStateFetcher(); | ||
|
||
for (const network of networks) { | ||
const backend = network.Backend.BackendServiceZero; | ||
if (!backend) { | ||
throw new Error('unexpectedly missing backend zero'); | ||
} | ||
const startTime = Date.now(); | ||
let resp; | ||
try { | ||
resp = await fetcher.checkServerStatus({ backend }); | ||
} catch { | ||
continue; | ||
} | ||
const endTime = Date.now(); | ||
const roundtripTime = endTime - startTime; | ||
|
||
serverStatusByNetworkId.set( | ||
network.NetworkId, | ||
{ | ||
networkId: network.NetworkId, | ||
isServerOk: resp.isServerOk, | ||
isMaintenance: resp.isMaintenance || false, | ||
// server time = local time + clock skew | ||
clockSkew: resp.serverTime + roundtripTime / 2 - endTime, | ||
lastUpdateTimestamp: startTime + roundtripTime / 2, | ||
} | ||
); | ||
} | ||
} | ||
|
||
let lastUpdateTimestamp: number = 0; | ||
let updatePromise: null | Promise<void> = null; | ||
|
||
async function updateServerStatusTick() { | ||
const refreshInterval = environment.getServerStatusRefreshInterval(); | ||
|
||
if (getSubscriptions().length === 0) { | ||
return; | ||
} | ||
|
||
if (Date.now() - lastUpdateTimestamp > refreshInterval) { | ||
if (!updatePromise) { | ||
updatePromise = updateServerStatus(); | ||
} | ||
await updatePromise; | ||
updatePromise = null; | ||
lastUpdateTimestamp = Date.now(); | ||
} | ||
|
||
emitUpdateToSubscriptions({ | ||
type: 'server-status-update', | ||
params: [...serverStatusByNetworkId.values()], | ||
}); | ||
|
||
setTimeout(updateServerStatusTick, refreshInterval); | ||
} | ||
|
||
export function startMonitorServerStatus() { | ||
registerCallback(params => { | ||
if (params.type === 'subscriptionChange') { | ||
updateServerStatusTick(); | ||
} | ||
}); | ||
} |
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.