1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-10 13:35:59 +00:00
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)