Former-commit-id: 1747352d45933ad8c8623d8dcfdbcb176ecba50c
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-05-04 06:45:14 +03:00
parent af66e7404f
commit 37251c6b00
4 changed files with 10 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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