feat: limit max connection count (#2701)

This commit is contained in:
BoYanZh
2022-12-14 10:33:58 +08:00
committed by GitHub
parent 33bae52fa1
commit 7947ff1ae4
3 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package middlewares
import (
"github.com/gin-gonic/gin"
)
func MaxAllowed(n int) gin.HandlerFunc {
sem := make(chan struct{}, n)
acquire := func() { sem <- struct{}{} }
release := func() { <-sem }
return func(c *gin.Context) {
acquire()
defer release()
c.Next()
}
}