[GH-ISSUE #381] [FEATURE REQUEST] Add ability to provide a base context for handler invocations #2190

Closed
opened 2026-03-15 19:36:26 +03:00 by kerem · 4 comments
Owner

Originally created by @binaek on GitHub (Jan 15, 2022).
Original GitHub issue: https://github.com/hibiken/asynq/issues/381

Originally assigned to: @hibiken on GitHub.

Is your feature request related to a problem? Please describe.
I am unable to set a base context for the handlerFunc invocations.

Describe the solution you'd like
Something along the lines of what http.Server provides:

BaseContext func(net.Listener) context.Context

A nice adaptation would be BaseContext func(context.Context) context.Context.

This will allow invocation contexts to be derived from a known context.Context

Originally created by @binaek on GitHub (Jan 15, 2022). Original GitHub issue: https://github.com/hibiken/asynq/issues/381 Originally assigned to: @hibiken on GitHub. **Is your feature request related to a problem? Please describe.** I am unable to set a base `context` for the `handlerFunc` invocations. **Describe the solution you'd like** Something along the lines of what `http.Server` provides: ``` BaseContext func(net.Listener) context.Context ``` A nice adaptation would be `BaseContext func(context.Context) context.Context`. This will allow invocation contexts to be derived from a known `context.Context`
kerem 2026-03-15 19:36:26 +03:00
Author
Owner

@hibiken commented on GitHub (Jan 16, 2022):

@binaek Thank you for creating this feature request!

I think it makes sense to add this BaseContext to Config.
But before we make the change, could you describe possible use cases? So that we have more concrete motivation to make this change :)

<!-- gh-comment-id:1013782472 --> @hibiken commented on GitHub (Jan 16, 2022): @binaek Thank you for creating this feature request! I think it makes sense to add this `BaseContext` to `Config`. But before we make the change, could you describe possible use cases? So that we have more concrete motivation to make this change :)
Author
Owner

@binaek commented on GitHub (Jan 16, 2022):

@hibiken
The use case that I am struggling with is as such:

The application uses Context as a DI container for application wide services like DB connection, Root loggers, telemetry etc.

The context also uses a signal.NotifyContext and so deadlines and cancellations never reach the handlers till an explicit call to Stop or Shutdown

<!-- gh-comment-id:1013834390 --> @binaek commented on GitHub (Jan 16, 2022): @hibiken The use case that I am struggling with is as such: The application uses Context as a DI container for application wide services like DB connection, Root loggers, telemetry etc. The context also uses a signal.NotifyContext and so deadlines and cancellations never reach the handlers till an explicit call to Stop or Shutdown
Author
Owner

@hibiken commented on GitHub (Jan 17, 2022):

I see. I'm onboard with adding this field to Config.

Just to clarify, the proposed function's signature is:

BaseContext func(ctx context.Context) context.Context

The input to the BaseContext is the context that would normally be passed to Handler (in the absence of this BaseContext function provided by the user), is that correct? So in effect this BaseContext function works like a middleware where user gets an opportunity to decorate the context before its passed to the Handler invocation? Let me know if I misunderstood your proposal.

<!-- gh-comment-id:1013985693 --> @hibiken commented on GitHub (Jan 17, 2022): I see. I'm onboard with adding this field to `Config`. Just to clarify, the proposed function's signature is: ```go BaseContext func(ctx context.Context) context.Context ``` The input to the `BaseContext` is the context that would normally be passed to Handler (in the absence of this `BaseContext` function provided by the user), is that correct? So in effect this `BaseContext` function works like a middleware where user gets an opportunity to decorate the context before its passed to the Handler invocation? Let me know if I misunderstood your proposal.
Author
Owner

@hibiken commented on GitHub (Feb 16, 2022):

Closed via #386

<!-- gh-comment-id:1041575308 --> @hibiken commented on GitHub (Feb 16, 2022): Closed via #386
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asynq#2190
No description provided.