Skip to content

Commit

Permalink
Add 路由中间件 - 限流
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Oct 13, 2019
1 parent f1ed621 commit 83ca9e8
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 2 deletions.
25 changes: 25 additions & 0 deletions app/route/middleware/limiter/limiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package limiter

import (
"fmt"
"github.com/gin-gonic/gin"
"go-gin-api/app/util"
"golang.org/x/time/rate"
"time"
)

func SetUp (maxBurstSize int) gin.HandlerFunc {

limiter := rate.NewLimiter(rate.Every(time.Second*1), maxBurstSize)
return func(c *gin.Context) {
if limiter.Allow() {
c.Next()
return
}
fmt.Println("Too many requests")
utilGin := util.Gin{Ctx: c}
utilGin.Response(-1, "Too many requests", nil)
c.Abort()
return
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ require (
github.com/opentracing/opentracing-go v1.1.0
github.com/uber-go/atomic v1.4.0 // indirect
github.com/uber/jaeger-client-go v2.18.1+incompatible
github.com/uber/jaeger-lib v2.1.1+incompatible
github.com/uber/jaeger-lib v2.1.1+incompatible // indirect
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0
google.golang.org/grpc v1.23.1
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/go-playground/validator.v9 v9.29.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/p
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 h1:xQwXv67TxFo9nC1GJFyab5eq/5B590r6RlnL/G8Sz7w=
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down
3 changes: 3 additions & 0 deletions vendor/golang.org/x/time/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/golang.org/x/time/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/golang.org/x/time/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/time/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 83ca9e8

Please sign in to comment.