[GH-ISSUE #104] [FEATURE REQUEST] Handler function middleware #28

Closed
opened 2026-03-02 05:18:03 +03:00 by kerem · 3 comments
Owner

Originally created by @rugwirobaker on GitHub (Mar 6, 2020).
Original GitHub issue: https://github.com/hibiken/asynq/issues/104

Originally assigned to: @hibiken on GitHub.

Is your feature request related to a problem? Please describe.
Do plan to support middleware functions that behave the same way for example gorilla mux.Middware do? If not how do logging??

Originally created by @rugwirobaker on GitHub (Mar 6, 2020). Original GitHub issue: https://github.com/hibiken/asynq/issues/104 Originally assigned to: @hibiken on GitHub. **Is your feature request related to a problem? Please describe.** Do plan to support middleware functions that behave the same way for example gorilla mux.Middware do? If not how do logging??
kerem 2026-03-02 05:18:03 +03:00
Author
Owner

@hibiken commented on GitHub (Mar 6, 2020):

@rugwirobaker Thanks for this feature request!

Could you provide an example code for a potential API for middleware support?

Alternatively, can you simply write a middleware function and wrap your handlers with that middleware?

func loggingMiddleware(h asynq.Handler) asynq.Handler {
    return asynq.HandlerFunc(func(ctx context.Context, t *asynq.Task) error {
        // log something before handler processes the task
        h.ProcessTask(ctx, t)
        // log something after handler processed the task
    })
}

mux := asynq.ServeMux()
mux.HandleFunc("something", loggingMiddleware(myHandler))

would that work? Or should the package offer better support for middleware chain?

<!-- gh-comment-id:595940537 --> @hibiken commented on GitHub (Mar 6, 2020): @rugwirobaker Thanks for this feature request! Could you provide an example code for a potential API for middleware support? Alternatively, can you simply write a middleware function and wrap your handlers with that middleware? ```go func loggingMiddleware(h asynq.Handler) asynq.Handler { return asynq.HandlerFunc(func(ctx context.Context, t *asynq.Task) error { // log something before handler processes the task h.ProcessTask(ctx, t) // log something after handler processed the task }) } mux := asynq.ServeMux() mux.HandleFunc("something", loggingMiddleware(myHandler)) ``` would that work? Or should the package offer better support for middleware chain?
Author
Owner

@rugwirobaker commented on GitHub (Mar 7, 2020):

For most use cases this works fine but in cases where you have a lot of middleware functions having something like:
mux.Use(someMiddlware) or mux.Add(someMiddleware) it would help avoid the clutter.

<!-- gh-comment-id:596059987 --> @rugwirobaker commented on GitHub (Mar 7, 2020): For most use cases this works fine but in cases where you have a lot of middleware functions having something like: `mux.Use(someMiddlware) ` or `mux.Add(someMiddleware)` it would help avoid the clutter.
Author
Owner

@hibiken commented on GitHub (Mar 7, 2020):

Ok that makes sense.
Let's add Use method to add middlewares to the chain, similar to gorilla/mux#Route.Use. The method should be a variadic function that takes any number of middleware functions. They should be executed in the order that they are applied.

Example:

mux := asynq.ServeMux()
mux.Use(loggingMiddleware)
mux.HandleFunc("something", myHandler)
<!-- gh-comment-id:596099116 --> @hibiken commented on GitHub (Mar 7, 2020): Ok that makes sense. Let's add `Use` method to add middlewares to the chain, similar to [gorilla/mux#Route.Use](https://godoc.org/github.com/gorilla/mux#Router.Use). The method should be a variadic function that takes any number of middleware functions. They should be executed in the order that they are applied. Example: ```go mux := asynq.ServeMux() mux.Use(loggingMiddleware) mux.HandleFunc("something", myHandler) ```
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#28
No description provided.