mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
minor
Former-commit-id: 1747352d45933ad8c8623d8dcfdbcb176ecba50c
This commit is contained in:
parent
af66e7404f
commit
37251c6b00
|
@ -14,6 +14,11 @@ func main() {
|
||||||
// * http://localhost:8080/v1
|
// * http://localhost:8080/v1
|
||||||
// * http://localhost:8080/v1/other
|
// * http://localhost:8080/v1/other
|
||||||
// * http://localhost:8080/v2/list (with X-API-Key request header)
|
// * 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")
|
app.Listen(":8080")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,8 +39,8 @@ func newApp() *iris.Application {
|
||||||
// if a client's last visit was 5 minutes ago ("old" entry)
|
// if a client's last visit was 5 minutes ago ("old" entry)
|
||||||
// and remove it from the memory.
|
// and remove it from the memory.
|
||||||
limitV1 := rate.Limit(1, 5, rate.PurgeEvery(time.Minute, 5*time.Minute))
|
limitV1 := rate.Limit(1, 5, rate.PurgeEvery(time.Minute, 5*time.Minute))
|
||||||
// rate.Every helper: 1 request per minute (with burst of 5):
|
// rate.Every helper:
|
||||||
// rate.Limit(rate.Every(1*time.Minute), 5)
|
// rate.Limit(rate.Every(time.Minute), 5)
|
||||||
v1.Use(limitV1)
|
v1.Use(limitV1)
|
||||||
|
|
||||||
v1.Get("/", index)
|
v1.Get("/", index)
|
||||||
|
@ -47,7 +52,7 @@ func newApp() *iris.Application {
|
||||||
v2.Use(useAPIKey)
|
v2.Use(useAPIKey)
|
||||||
// Initialize a new rate limit middleware to limit requests
|
// Initialize a new rate limit middleware to limit requests
|
||||||
// per API Key(see `useAPIKey` below) instead of client's Remote IP Address.
|
// 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.Use(limitV2)
|
||||||
|
|
||||||
v2.Get("/list", list)
|
v2.Get("/list", list)
|
||||||
|
|
|
@ -1565,8 +1565,7 @@ func (ctx *context) StopWithError(statusCode int, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.WriteString(err.Error())
|
ctx.StopWithText(statusCode, err.Error())
|
||||||
ctx.StopWithStatus(statusCode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StopWithJSON stops the handlers chain, writes the status code
|
// StopWithJSON stops the handlers chain, writes the status code
|
||||||
|
|
|
@ -168,8 +168,6 @@ func (l *Limiter) serveHTTP(ctx context.Context) {
|
||||||
|
|
||||||
ctx.Values().Set(clientContextKey, client)
|
ctx.Values().Set(clientContextKey, client)
|
||||||
|
|
||||||
// reserve := client.Limiter.Reserve()
|
|
||||||
// if reserve.OK() {
|
|
||||||
if client.Limiter.Allow() {
|
if client.Limiter.Allow() {
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
return
|
return
|
||||||
|
|
|
@ -207,9 +207,8 @@ func (s *Sessions) decodeCookieValue(cookieValue string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
var cookieValueDecoded string
|
|
||||||
|
|
||||||
if decode := s.config.Decode; decode != nil {
|
if decode := s.config.Decode; decode != nil {
|
||||||
|
var cookieValueDecoded string
|
||||||
err := decode(s.config.Cookie, cookieValue, &cookieValueDecoded)
|
err := decode(s.config.Cookie, cookieValue, &cookieValueDecoded)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cookieValue = cookieValueDecoded
|
cookieValue = cookieValueDecoded
|
||||||
|
|
Loading…
Reference in New Issue
Block a user