didip / tollbooth_chi

Tollbooth - Chi integration layer
13 stars 7 forks source link

Question about multiple toolboth limit handlers #1

Open skylarthai opened 7 years ago

skylarthai commented 7 years ago

Hello, I feel this looks extremely interesting. I was wondering how the support is for having multiple throttlers for the same IP. E.g. one limiter that allows 1 request per 1.5 seconds, and another limiter that allows 1000 requests per hour.

I assume this will be achieved using the code below, but is it efficient, and is there a better way to do it?

package main

import (
    "github.com/didip/tollbooth"
    "github.com/didip/tollbooth_chi"
    "github.com/pressly/chi"
    "net/http"
    "time"
)

func main() {
    // Create a limiter struct.
    limiter1 := tollbooth.NewLimiter(1, 1.5 * time.Second, nil)
    limiter2 := tollbooth.NewLimiter(1000, 1 * time.Hour, nil)

    r := chi.NewRouter()

    r.Use(tollbooth_chi.LimitHandler(limiter1))
    r.Use(tollbooth_chi.LimitHandler(limiter2))

    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, world!"))
    })

    http.ListenAndServe(":12345", r)
}
didip commented 7 years ago

I am not quite sure I understand the outcome that you want to achieve.

Even if above code works, it wouldn't be deterministic. How would you know which limiter should be used?