-
Notifications
You must be signed in to change notification settings - Fork 62
Wallet cache v2 phase 1: emit currency wallets before their engines exist #733
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
Open
j0ntz
wants to merge
10
commits into
master
Choose a base branch
from
jon/wallet-cache-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5cb7d71
Document the wallet cache v2 design
j0ntz 7d4a9d8
Add the account cache file and its cleaners
j0ntz 8073c38
Add a limited-concurrency queue for engine startup
j0ntz a59da7a
Seed Redux from the cache and defer the authoritative loads
j0ntz 547b855
Read every wallet's cached state in one bulk load
j0ntz b766b34
Serve the account API before its storage wallet exists
j0ntz a2001d2
Emit currency wallets before their engines exist
j0ntz a1767b9
Emit the account from cache and write it from one saver
j0ntz 93869ab
Accept hash-suffixed store routes in the fake sync server
j0ntz 3092b88
Add deterministic tests for the cache and the boot window
j0ntz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
| import { Disklet } from 'disklet' | ||
|
|
||
| import { makeJsonFile } from '../../util/file-helpers' | ||
| import { | ||
| AccountCacheFile, | ||
| asAccountCacheFile, | ||
| asStoredAccountCacheFile | ||
| } from './account-cleaners' | ||
|
|
||
| /** | ||
| * Cached account boot state, stored on the account's local disklet. | ||
| * See `asAccountCacheFile` for the schema. | ||
| * | ||
| * The cache lives in two alternating slots. The disklet exposes no | ||
| * rename (neither its JS interface nor its iOS and Android native | ||
| * modules), so the usual write-temp-then-rename trick is unavailable, | ||
| * and Android's backend truncates the target before writing. A kill | ||
| * mid-write can therefore leave one slot torn. Writing generations | ||
| * alternately means the OTHER slot always holds the last complete | ||
| * one, and `loadAccountCache` takes the newest slot that still | ||
| * parses. (iOS writes with `NSDataWritingAtomic` and never tears, so | ||
| * this only earns its keep on Android.) | ||
| */ | ||
| export const ACCOUNT_CACHE_FILES = ['accountCache.json', 'accountCache.2.json'] | ||
| export const accountCacheFile = { | ||
| load: makeJsonFile(asStoredAccountCacheFile).load, | ||
| save: makeJsonFile(asAccountCacheFile).save | ||
| } | ||
|
|
||
| /** | ||
| * Tuning for the account boot-state cache saver. | ||
| * Tests override the throttle to run quickly. | ||
| */ | ||
| export const accountCacheSaverConfig = { | ||
| throttleMs: 5000 | ||
| } | ||
|
|
||
| /** | ||
| * Reads both slots and returns the newest one that parses, plus the | ||
| * slot the next write should use. Returns `undefined` cache data when | ||
| * neither slot is readable (first login, schema bump, both torn), | ||
| * which sends the caller to its cold path. | ||
| */ | ||
| export async function loadAccountCache( | ||
| disklet: Disklet | ||
| ): Promise<{ cache: AccountCacheFile | undefined; nextSlot: number }> { | ||
| const slots = await Promise.all( | ||
| ACCOUNT_CACHE_FILES.map( | ||
| async path => await accountCacheFile.load(disklet, path) | ||
| ) | ||
| ) | ||
|
|
||
| let best: AccountCacheFile | undefined | ||
| let bestSlot = -1 | ||
| for (let slot = 0; slot < slots.length; ++slot) { | ||
| const cache = slots[slot] | ||
| if (cache == null) continue | ||
| if (best == null || cache.sequence > best.sequence) { | ||
| best = cache | ||
| bestSlot = slot | ||
| } | ||
| } | ||
|
|
||
| // Write over the slot we did NOT just read, so a torn write can | ||
| // never damage the generation we are currently relying on: | ||
| return { | ||
| cache: best, | ||
| nextSlot: bestSlot === -1 ? 0 : (bestSlot + 1) % ACCOUNT_CACHE_FILES.length | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Writes the next generation into `slot` and returns the slot the | ||
| * write after this one should use. | ||
| */ | ||
| export async function saveAccountCache( | ||
| disklet: Disklet, | ||
| slot: number, | ||
| data: AccountCacheFile | ||
| ): Promise<number> { | ||
| await accountCacheFile.save(disklet, ACCOUNT_CACHE_FILES[slot], data) | ||
| return (slot + 1) % ACCOUNT_CACHE_FILES.length | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.