From ea9a6918c90e0631450ef276f197a65560f6bea7 Mon Sep 17 00:00:00 2001 From: thinkgos Date: Fri, 12 May 2023 15:15:25 +0800 Subject: [PATCH] fix: move middleware to gin-contrib --- go.mod | 1 - go.sum | 2 -- middleware/max_bytes.go | 23 ----------------------- middleware/max_conns.go | 32 -------------------------------- 4 files changed, 58 deletions(-) delete mode 100644 middleware/max_bytes.go delete mode 100644 middleware/max_conns.go diff --git a/go.mod b/go.mod index e57a9bc..08ada5a 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/gin-gonic/gin v1.9.0 github.com/golang-jwt/jwt/v4 v4.5.0 github.com/stretchr/testify v1.8.2 - github.com/things-go/limiter v0.0.3 github.com/things-go/x v0.1.1 go.uber.org/zap v1.24.0 golang.org/x/exp v0.0.0-20230321023759-10a507213a29 diff --git a/go.sum b/go.sum index 21684f3..dcff2fd 100644 --- a/go.sum +++ b/go.sum @@ -58,8 +58,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/things-go/limiter v0.0.3 h1:j5FdBLrxpGW8GMStdIaguzVROAlAOREOgLI90tbt01k= -github.com/things-go/limiter v0.0.3/go.mod h1:btGJUDvtenYwWdzXGas3guQpDlffHCS710KuQfzWakM= github.com/things-go/x v0.1.1 h1:wuKa3p/KliaD+HugTG64czjaOHakd36FGLN5ZjOngGQ= github.com/things-go/x v0.1.1/go.mod h1:GURI7Q7v27OqDkuOXUc6BFl0FCzF7JEcfciDl70u6Vw= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= diff --git a/middleware/max_bytes.go b/middleware/max_bytes.go deleted file mode 100644 index a7bc814..0000000 --- a/middleware/max_bytes.go +++ /dev/null @@ -1,23 +0,0 @@ -package middleware - -import ( - "net/http" - - "github.com/gin-gonic/gin" -) - -// MaxBytes returns a middleware that limit reading of http request body. -func MaxBytes(n int64) gin.HandlerFunc { - if n <= 0 { - return func(c *gin.Context) { - c.Next() - } - } - return func(c *gin.Context) { - if c.Request.ContentLength > n { - c.AbortWithStatus(http.StatusRequestEntityTooLarge) - } else { - c.Next() - } - } -} diff --git a/middleware/max_conns.go b/middleware/max_conns.go deleted file mode 100644 index f07260b..0000000 --- a/middleware/max_conns.go +++ /dev/null @@ -1,32 +0,0 @@ -package middleware - -import ( - "log" - "net/http" - - "github.com/gin-gonic/gin" - - "github.com/things-go/limiter/limit" -) - -// MaxConns returns a middleware that limit the concurrent connections. -func MaxConns(n int) gin.HandlerFunc { - if n <= 0 { - return func(c *gin.Context) { - c.Next() - } - } - latch := limit.NewLimit(n) - return func(c *gin.Context) { - if latch.TryBorrow() { - defer func() { - if err := latch.Return(); err != nil { - log.Println(err) - } - }() - c.Next() - } else { - c.AbortWithStatus(http.StatusServiceUnavailable) - } - } -}