diff --git a/_examples/miscellaneous/ratelimit/main.go b/_examples/miscellaneous/ratelimit/main.go index 2f1b235e..01835a9d 100644 --- a/_examples/miscellaneous/ratelimit/main.go +++ b/_examples/miscellaneous/ratelimit/main.go @@ -14,6 +14,11 @@ func main() { // * http://localhost:8080/v1 // * http://localhost:8080/v1/other // * http://localhost:8080/v2/list (with X-API-Key request header) + // Read more at: https://en.wikipedia.org/wiki/Token_bucket + // + // Alternative you may want to use something like: + // https://github.com/iris-contrib/middleware/blob/master/throttler/_example/main.go + // Read more at: https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm app.Listen(":8080") } @@ -34,8 +39,8 @@ func newApp() *iris.Application { // if a client's last visit was 5 minutes ago ("old" entry) // and remove it from the memory. limitV1 := rate.Limit(1, 5, rate.PurgeEvery(time.Minute, 5*time.Minute)) - // rate.Every helper: 1 request per minute (with burst of 5): - // rate.Limit(rate.Every(1*time.Minute), 5) + // rate.Every helper: + // rate.Limit(rate.Every(time.Minute), 5) v1.Use(limitV1) v1.Get("/", index) @@ -47,7 +52,7 @@ func newApp() *iris.Application { v2.Use(useAPIKey) // Initialize a new rate limit middleware to limit requests // per API Key(see `useAPIKey` below) instead of client's Remote IP Address. - limitV2 := rate.Limit(1, 5, rate.PurgeEvery(time.Minute, 5*time.Minute)) + limitV2 := rate.Limit(rate.Every(time.Minute), 300, rate.PurgeEvery(5*time.Minute, 15*time.Minute)) v2.Use(limitV2) v2.Get("/list", list) diff --git a/context/context.go b/context/context.go index 2eff50dc..ab92b9f2 100644 --- a/context/context.go +++ b/context/context.go @@ -1565,8 +1565,7 @@ func (ctx *context) StopWithError(statusCode int, err error) { return } - ctx.WriteString(err.Error()) - ctx.StopWithStatus(statusCode) + ctx.StopWithText(statusCode, err.Error()) } // StopWithJSON stops the handlers chain, writes the status code diff --git a/middleware/rate/rate.go b/middleware/rate/rate.go index 4931c8ea..60a1a975 100644 --- a/middleware/rate/rate.go +++ b/middleware/rate/rate.go @@ -168,8 +168,6 @@ func (l *Limiter) serveHTTP(ctx context.Context) { ctx.Values().Set(clientContextKey, client) - // reserve := client.Limiter.Reserve() - // if reserve.OK() { if client.Limiter.Allow() { ctx.Next() return diff --git a/sessions/sessions.go b/sessions/sessions.go index f80c8344..c8e3194c 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -207,9 +207,8 @@ func (s *Sessions) decodeCookieValue(cookieValue string) string { return "" } - var cookieValueDecoded string - if decode := s.config.Decode; decode != nil { + var cookieValueDecoded string err := decode(s.config.Cookie, cookieValue, &cookieValueDecoded) if err == nil { cookieValue = cookieValueDecoded