2 Star 0 Fork 1

web-bird / bird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
limit.go 411 Bytes
一键复制 编辑 原始数据 按行查看 历史
haitgo 提交于 2020-02-02 17:40 . 调整结构
package middleware
import (
"github.com/gin-gonic/gin"
)
//限制最大允许连接数的中间件,最低255并发
func MaxAllowed(n int) gin.HandlerFunc {
if n == 0 {
n = 255
}
type s struct{}
sem := make(chan s, n)
acquire := func() {
sem <- s{}
}
release := func() {
<-sem
}
return func(c *gin.Context) {
acquire() //before request
defer release() //after request
c.Next()
}
}
1
https://gitee.com/web-bird/bird.git
git@gitee.com:web-bird/bird.git
web-bird
bird
bird
eddb2f01d39e

搜索帮助