Refactor Context for concurrency safety and simplified panic handling - #2
Merged
Merged
Conversation
- Update context's state to map with mutex. - Update context's isAbort type to atomic.Bool. - Remove panic recovery from Next. Signed-off-by: Chen Su <ghosind@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors the engine.Context internals to change how per-request state is stored and accessed, improve thread-safety around abort signaling, and make Next() propagate panics instead of recovering internally (breaking change reflected in tests).
Changes:
- Replace
sync.Map-backed context state with amap[string]anyprotected by a mutex. - Change
isAbortfrombooltoatomic.Booland updateAbort()/IsAbort()accordingly. - Simplify
Next()by removing panic recovery; update panic-related tests to assert panics.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| context.go | Refactors Context state storage/locking and Next() execution behavior; expands documentation around Next()/Use(). |
| context_test.go | Updates Next() panic tests to validate panic propagation (no internal recovery). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+22
to
24
| state map[string]any | ||
| mu sync.Mutex | ||
| index int |
Comment on lines
+45
to
+48
| ctx.mu.Lock() | ||
| defer ctx.mu.Unlock() | ||
| value, ok := ctx.state[key] | ||
| return value, ok |
Comment on lines
+87
to
+91
| // // pre-processing | ||
| // c.Next() // executes all remaining handlers | ||
| // // post-processing | ||
| // return nil | ||
| // }) |
| // }) | ||
| // | ||
| // Behavior: | ||
| // - Each handler in the chain is executed exactly once per Next call. |
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.
This PR refactors Context state management and changes Next() behavior.
state: change the state management fromsync.Maptomap[string]anywithsync.Mutexfor better performance and type safety.isAbort: change type frombooltoatomic.Boolfor thread-safe access and modification.Next(): remove internal panic recovery. The user should handle panics in their own handlers or use a middleware to recover from panics. It's a breaking change.