perf: jittered backoff between CAS retries#117
Merged
Conversation
A client value that overflowed a Signal[T]'s narrower numeric type (e.g. 9999 into Signal[int8]) was silently truncated, and a shape mismatch was silently dropped — the typed Signal was only as safe as a best-effort decode. Add WithStrictDecode(): a client value that overflows the target int/uint/float width or whose JSON shape doesn't match the field surfaces an action error and the handler does not run, so corrupt input can't reach server state. Off by default (best-effort decode preserved). The decode is factored into decodeScalarChecked, which reports the error; decodeScalarInto wraps it and discards it so server-side store decodes keep the lenient contract. The struct-tag init path stays best-effort (author input, not untrusted client).
StateApp.Update and StateSess.Update retried a CAS conflict immediately, so a contended key spun through all 100 attempts burning CPU with no pause. Add an exponential backoff ceiling (100µs doubling, capped at 10ms) with full jitter in [0, ceiling) so concurrent retriers de-correlate instead of colliding in lockstep. The cap stays well under a frame so a contended Update adds no perceptible action latency; the no-contention path (first-try success) never sleeps.
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.
Critic panel finding #11 (minor; perf).
StateApp.Update/StateSess.Updateretried a CAS conflict immediately — a contended key spun through all 100 attempts burning CPU.Change
Exponential backoff ceiling (
100µsdoubling, capped10ms) with full jitter in[0, ceiling)so concurrent retriers de-correlate. Cap stays under a frame (no perceptible latency); first-try success never sleeps. The ceiling clamp also absorbs the int64 overflow a large shift would wrap negative.Tests
Full
ci-check.shgreen.