Skip to content

Refactor Context for concurrency safety and simplified panic handling - #2

Merged
ghosind merged 1 commit into
go-amwk:mainfrom
ghosind:refactor/context-sync
Jul 17, 2026
Merged

Refactor Context for concurrency safety and simplified panic handling#2
ghosind merged 1 commit into
go-amwk:mainfrom
ghosind:refactor/context-sync

Conversation

@ghosind

@ghosind ghosind commented Jul 17, 2026

Copy link
Copy Markdown
Member

This PR refactors Context state management and changes Next() behavior.

  • state: change the state management from sync.Map to map[string]any with sync.Mutex for better performance and type safety.
  • isAbort: change type from bool to atomic.Bool for 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.

- 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>
@ghosind
ghosind requested a review from Copilot July 17, 2026 03:08
@ghosind ghosind added the enhancement New feature or request label Jul 17, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 a map[string]any protected by a mutex.
  • Change isAbort from bool to atomic.Bool and update Abort()/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 thread context.go
Comment on lines +22 to 24
state map[string]any
mu sync.Mutex
index int
Comment thread context.go
Comment on lines +45 to +48
ctx.mu.Lock()
defer ctx.mu.Unlock()
value, ok := ctx.state[key]
return value, ok
Comment thread context.go
Comment on lines +87 to +91
// // pre-processing
// c.Next() // executes all remaining handlers
// // post-processing
// return nil
// })
Comment thread context.go
// })
//
// Behavior:
// - Each handler in the chain is executed exactly once per Next call.
@ghosind
ghosind merged commit 04d783f into go-amwk:main Jul 17, 2026
9 checks passed
@ghosind
ghosind deleted the refactor/context-sync branch July 24, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants