Repository navigation
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
Merged
+6,719
−320
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7edcff6
Document the wallet cache v2 design
j0ntz 4070317
Add the account cache file and its cleaners
j0ntz 325e8c6
Add a limited-concurrency queue for engine startup
j0ntz 616d6bd
Seed Redux from cache, defer the real loads
j0ntz 7f73f58
Read every wallet's cached state in one bulk load
j0ntz 66d1b43
Serve the account API before its repo exists
j0ntz b9b4e70
Emit currency wallets before their engines exist
j0ntz a4815f6
Emit the account from cache, write from one saver
j0ntz 6fc6020
Accept hash-suffixed routes in the fake server
j0ntz 1c2e1c2
Add deterministic cache and boot-window tests
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,85 @@ | ||
| 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, | ||
| /** A write slower than this logs at `warn` instead of `info`. */ | ||
| slowWriteMs: 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.