Repository navigation
Replies: 2 comments 1 reply
|
There is rate limiter middleware. See docs here https://echo.labstack.com/docs/middleware/rate-limiter it uses official Rate limit package (https://pkg.go.dev/golang.org/x/time/rate) under the hood, so check it for details. |
|
The rate limiter middleware counts requests, not bytes, so it will not do what you want. For a file download, a rate-limited reader is the simplest answer: func download(c echo.Context) error {
f, err := os.Open("report.zip")
if err != nil { return err }
defer f.Close()
c.Response().Header().Set(echo.HeaderContentType, "application/zip")
_, err = io.Copy(c.Response().Writer, rate.NewReader(f, rate.Limit(1024*1024)))
return err
}rate.NewReader comes from golang.org/x/time/rate and limits bytes per second. For a per-IP limit keep one limiter per IP in a small cache with some cleanup, rather than creating one inside the handler. A global budget across several requests needs one shared limiter too. Also watch out for compression. If you gzip the response, the byte count you limited is not the number of bytes that reach the client, so apply the limiter at the layer that writes to the socket. |
Uh oh!
There was an error while loading. Please reload this page.
For example, if the route is configured with limit-rate: "1024k", then the IP speed limit of the route is restricted to a speed of 1MB/s.
If the route is configured with global-limit-rate: "10240k", then the total bandwidth of the route is restricted to a speed of 10MB/s.
All reactions