Repository navigation
feat(scontext)!: add clear helpers and presence masks - #139
Merged
Merged
Conversation
Replace the manual index-based iteration over `typ.NumField()` and `typ.Field(i)` with the more idiomatic `range typ.Fields()` loop introduced in Go 1.23.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #139 +/- ##
==========================================
+ Coverage 99.79% 99.92% +0.12%
==========================================
Files 25 26 +1
Lines 2484 2604 +120
==========================================
+ Hits 2479 2602 +123
+ Misses 5 2 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Clearing request identity now removes both its stored values and presence, so callers can clone a context and run actor-free follow-up work without inheriting an authenticated user. Adds all clear helpers requested in #138, including atomic
ClearIdentityandClearClientInfo, with exact generic-type matching, in-place mutation, and no wrapper creation.Correlation clears invalidate the request logger; existing logger snapshots remain immutable. ClearRequestLogger removes the source and cache. Documentation and a runnable identity-context example explain clone-first isolation and logger reacquisition.
The 13 private context presence booleans are packed into a mutex-protected uint16 mask. Correlation snapshots now copy only their four relevant presence bits into a private mask instead of expanding them into booleans. Existing context setter/getter signatures and explicitly present zero/nil semantics are preserved.
Breaking API change
Replace
c.TraceIDSet,c.BuildIDSet,c.ConfigIDSet, andc.UserIDSetwithc.HasTraceID(),c.HasBuildID(),c.HasConfigID(), andc.HasUserID(). The value fields remain exported. Populate snapshots throughGetCorrelation; struct literals can no longer set presence. Default struct serialization no longer includes the removed presence fields. The context guide documents migration and includes a runnable public API example.The zero snapshot has no values present. Snapshots retain their values and presence after later writes/clears, and unrelated client/route metadata does not affect snapshot equality. Tests cover all 16 presence combinations and these invariants.
Validation
go test ./...,go vet ./..., andgo build ./examples/...pass.go test -race -coverpkg=./pkg/...,./internal/requestlog -coverprofile=/tmp/srouter-clear-coverage.out ./...passes; aggregate coverage 97.8%.go test ./internal/doccheck,go run ./examples/identity-context, andgolangci-lint run --timeout=5mpass (0 lint issues).Context-mask measurements
Go 1.27.1, darwin/arm64. Compared base c4d1d92 with the context-mask commit b750dc9 using identical benchmark code, three runs each; timings below are medians. Timing samples are descriptive, not a statistical speedup claim.
Context size decreases by 16 bytes: int IDs 288 → 272 bytes; string and [16]byte IDs 296 → 280 bytes. The measured int context allocation stays in the same allocator size class.
Reproduce with
go test ./pkg/scontext -run TestContextSize -v -bench 'Benchmark(Context|Correlation|GetLoggerFastPath|GetLoggerDerivation)' -benchmem -count=3.Correlation-mask measurements
Compared 2d120d6 (context mask with boolean Correlation fields) against the additional Correlation mask change. Go 1.27.1, darwin/arm64; five runs per version, medians below. Allocation sizes and counts are unchanged in all measured paths.
The isolated snapshot read measured about 11% lower latency, with zero allocations before and after. Broader logger results are mixed, so this is not a general throughput claim. Alignment keeps Correlation size unchanged: 64 bytes for int IDs, 72 bytes for string/[16]byte IDs. The earlier 16-byte context-size reduction remains.
Reproduce with
go test ./pkg/scontext -run TestCorrelationSize -v -bench 'Benchmark(GetCorrelation|Correlation|GetLoggerFastPath|GetLoggerDerivation)' -benchmem -count=5.Closes #138