Skip to content

Commit

Permalink
add docs for limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 1, 2025
1 parent 59e8bdf commit 33c704a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,34 @@ example with chi router:

router.Use(rest.Reject(http.StatusBadRequest, "X-Request-Id header is required", rejectFn))
```
### Limiter (tollbooth) middleware

Limiter middleware is a rate limiter that limits the number of requests a client can make in a given time window.
It wraps the [tollbooth](https://github.com/didip/tollbooth) library.

example with stdlib router:

```go
router := http.NewServeMux()
limiter := tollbooth.NewLimiter(1, nil)

router.Handle("/api", tollbooth.LimitHandler(limiter, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})))
```

example with [routegroup](https://github.com/go-pkgz/routegroup):

```go
router := routegroup.New(http.NewServeMux())
limiter := tollbooth.NewLimiter(1, nil)
router.Use(rest.Limiter(limiter))

router.HandleFunc("/api", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}))
```
By default, the middleware uses `RemoteAddr` lookup to get the client's IP address. This can be changed by `SetIPLookup` of the limiter itself. For more details see [tollbooth docs](https://github.com/didip/tollbooth?tab=readme-ov-file#features)

### BasicAuth middleware family

Expand Down

0 comments on commit 33c704a

Please sign in to comment.