Add child context for global variables#157
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds context propagation support to the server.Cmd runtime interface so callers can derive a Cmd instance with an overridden context.Context while keeping other runtime dependencies (logger, tracer, defaults, etc.) the same.
Changes:
- Added
WithContext(context.Context) Cmdto theserver.Cmdinterface. - Introduced an internal
childwrapper inpkg/cmd/global.goto overrideContext()while delegating the rest ofCmd. - Implemented
(*global).WithContextto produce achildinstance.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
interfaces.go |
Extends the public server.Cmd interface with WithContext. |
pkg/cmd/global.go |
Adds child wrapper + implements WithContext for *global and Context() for *child. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
81
to
+82
| HTTPTimeout() time.Duration | ||
|
|
| } | ||
|
|
||
| func (c *child) Context() context.Context { | ||
| return c.ctx |
Comment on lines
+179
to
+184
| // WithContext returns a copy of the Cmd with the provided context. | ||
| func (g *global) WithContext(ctx context.Context) server.Cmd { | ||
| return types.Ptr(child{ | ||
| Cmd: g, | ||
| ctx: ctx, | ||
| }) |
Comment on lines
+180
to
+184
| func (g *global) WithContext(ctx context.Context) server.Cmd { | ||
| return types.Ptr(child{ | ||
| Cmd: g, | ||
| ctx: ctx, | ||
| }) |
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 pull request introduces support for context propagation in the
Cmdinterface and its implementations. The main changes include adding aWithContextmethod to theCmdinterface, implementing a newchildstruct to handle context, and updating the relevant methods to support this functionality.Context propagation support:
WithContext(context.Context) Cmdmethod to theCmdinterface ininterfaces.goto allow creating copies ofCmdwith a new context.childstruct inpkg/cmd/global.goto wrapCmdwith a specific context, and implemented itsContext()method. [1] [2]WithContextmethod for theglobalstruct, returning a newchildinstance with the provided context.