Repository navigation
Refresh exchange rates cached with a future timestamp - #44
Draft
adambuchweitz wants to merge 1 commit into
Draft
adambuchweitz wants to merge 1 commit into
adambuchweitz wants to merge 1 commit into
Conversation
The rates are refreshed once their fetchedAt is more than 12 hours old. If currencies.json was written while the clock ran ahead, now - fetchedAt is negative, so the cached rates never count as stale and are never refreshed until the clock passes that time. The same arithmetic holds off retries and time-zone updates after the clock is set back. Calculator.isStale treats a time more than five minutes in the future (or not a number) as stale, and refresh() uses it for all three checks. A future fetchedAt is dropped when the cache loads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Exchange rates are refreshed once their
fetchedAtis more than 12 hours old, computed asnow - fetchedAt. Ifcurrencies.jsonwas written while the clock ran ahead, that age is negative, so the cached rates never count as stale and aren't refreshed until the clock passes that time, which could be months away. The same arithmetic holds off retries and time-zone updates after the clock is set back.Calculator.isStale(at, now, maxAgeMs)treats a time more than five minutes in the future, or one that isn't a number, as stale.refresh()uses it for all three checks: zones, rates and the retry gate.fetchedAtfrom the future is dropped when the cache loads, so it doesn't linger in state.One trade-off: if the clock is behind when the launcher opens (for example before NTP syncs at boot), a valid cache looks like it's from the future and the rates are fetched again. Before, it was kept.
Found while reading #16's rate service, which handled this case.
Testing:
node --test tests/*.test.jspasses, including newisStaletests intests/calculator.test.js.🤖 Generated with Claude Code