Skip to content

Commit

Permalink
Merge pull request #607 from EdgeApp/william/randomize-polling
Browse files Browse the repository at this point in the history
Randomize polling
  • Loading branch information
swansontec authored Jul 26, 2024
2 parents c427cbf + 48edc13 commit 170305c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- changed: Randomize data-sync start times.

## 2.9.2 (2024-07-25)

- fixed: Work with React Native bridgeless mode on iOS and Android.
Expand Down
14 changes: 9 additions & 5 deletions src/core/account/account-pixie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import { snooze } from '../../util/snooze'
import { syncLogin } from '../login/login'
import { waitForPlugins } from '../plugins/plugins-selectors'
import { RootProps, toApiInput } from '../root-pixie'
import { addStorageWallet, syncStorageWallet } from '../storage/storage-actions'
import {
addStorageWallet,
SYNC_INTERVAL,
syncStorageWallet
} from '../storage/storage-actions'
import { makeAccountApi } from './account-api'
import { loadAllWalletStates, reloadPluginSettings } from './account-files'
import { AccountState, initialCustomTokens } from './account-reducer'
Expand Down Expand Up @@ -143,8 +147,8 @@ const accountPixie: TamePixie<AccountProps> = combinePixies({
}

// We don't report sync failures, since that could be annoying:
const dataTask = makePeriodicTask(doDataSync, 30 * 1000)
const loginTask = makePeriodicTask(doLoginSync, 30 * 1000, {
const dataTask = makePeriodicTask(doDataSync, SYNC_INTERVAL)
const loginTask = makePeriodicTask(doLoginSync, SYNC_INTERVAL, {
onError(error) {
// Only send OTP errors to the GUI:
const otpError = asMaybeOtpError(error)
Expand All @@ -157,8 +161,8 @@ const accountPixie: TamePixie<AccountProps> = combinePixies({
if (input.props.accountOutput?.accountApi == null) return

// Start once the EdgeAccount API exists:
dataTask.start({ wait: true })
loginTask.start({ wait: true })
dataTask.start({ wait: SYNC_INTERVAL * (1 + Math.random()) })
loginTask.start({ wait: SYNC_INTERVAL * (1 + Math.random()) })
},

destroy() {
Expand Down
5 changes: 3 additions & 2 deletions src/core/currency/wallet/currency-wallet-pixie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getCurrencyTools } from '../../plugins/plugins-selectors'
import { RootProps, toApiInput } from '../../root-pixie'
import {
addStorageWallet,
SYNC_INTERVAL,
syncStorageWallet
} from '../../storage/storage-actions'
import {
Expand Down Expand Up @@ -295,7 +296,7 @@ export const walletPixie: TamePixie<CurrencyWalletProps> = combinePixies({
}

// We don't report sync failures, since that could be annoying:
const task = makePeriodicTask(doSync, 30 * 1000)
const task = makePeriodicTask(doSync, SYNC_INTERVAL)

return {
update() {
Expand All @@ -307,7 +308,7 @@ export const walletPixie: TamePixie<CurrencyWalletProps> = combinePixies({
state.storageWallets[walletId] != null &&
state.storageWallets[walletId].status.lastSync > 0
) {
task.start({ wait: true })
task.start({ wait: SYNC_INTERVAL * (1 + Math.random()) })
}
},

Expand Down
2 changes: 2 additions & 0 deletions src/core/storage/storage-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from './repo'
import { StorageWalletStatus } from './storage-reducer'

export const SYNC_INTERVAL = 30 * 1000

export async function addStorageWallet(
ai: ApiInput,
walletInfo: EdgeWalletInfo
Expand Down
23 changes: 16 additions & 7 deletions src/util/periodic-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ interface PeriodicTaskOptions {
}

interface StartOptions {
// True to start in the waiting state, skipping the first run:
wait?: boolean
// True to start in the waiting state, skipping the first run,
// or the number of ms to wait before the first run:
wait?: boolean | number
}

export interface PeriodicTask {
Expand Down Expand Up @@ -39,13 +40,17 @@ export function makePeriodicTask(
running = true
new Promise(resolve => resolve(task()))
.catch(onError)
.then(startWaiting, startWaiting)
.then(resumeWaiting, resumeWaiting)
}

function startWaiting(): void {
function startWaiting(nextGap: number): void {
running = false
if (!out.started) return
timeout = setTimeout(startRunning, msGap)
timeout = setTimeout(startRunning, nextGap)
}

function resumeWaiting(): void {
startWaiting(msGap)
}

const out = {
Expand All @@ -56,9 +61,13 @@ export function makePeriodicTask(
},

start(opts: StartOptions = {}): void {
const { wait = false } = opts
const { wait } = opts
out.started = true
if (!running && timeout == null) wait ? startWaiting() : startRunning()
if (!running && timeout == null) {
if (typeof wait === 'number') startWaiting(wait)
else if (wait === true) startWaiting(msGap)
else startRunning()
}
},

stop(): void {
Expand Down

0 comments on commit 170305c

Please sign in to comment.