Giter VIP home page Giter VIP logo

Comments (3)

rugwirobaker avatar rugwirobaker commented on May 20, 2024 1

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.

from asynq.

hibiken avatar hibiken commented on May 20, 2024

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

from asynq.

hibiken avatar hibiken commented on May 20, 2024

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)

from asynq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.