Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] Handler function middleware #104

Closed
rugwirobaker opened this issue Mar 6, 2020 · 3 comments · Fixed by #111
Closed

[FEATURE REQUEST] Handler function middleware #104

rugwirobaker opened this issue Mar 6, 2020 · 3 comments · Fixed by #111
Assignees
Labels
enhancement New feature or request

Comments

@rugwirobaker
Copy link

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??

@rugwirobaker rugwirobaker added the enhancement New feature or request label Mar 6, 2020
@rugwirobaker rugwirobaker changed the title [FEATURE REQUEST] Description of the feature request [FEATURE REQUEST] Handler function middlware Mar 6, 2020
@rugwirobaker rugwirobaker changed the title [FEATURE REQUEST] Handler function middlware [FEATURE REQUEST] Handler function middleware Mar 6, 2020
@hibiken
Copy link
Owner

hibiken commented 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?

@rugwirobaker
Copy link
Author

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.

@hibiken
Copy link
Owner

hibiken commented 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)

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 a pull request may close this issue.

2 participants