Ai
2 Star 3 Fork 0

newdas/video_server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
limiter.go 561 Bytes
一键复制 编辑 原始数据 按行查看 历史
newdas 提交于 2018-08-02 00:22 +08:00 . all
package main
import (
"log"
)
type ConnLimiter struct {
concurrentConn int
bucket chan int
}
func NewConnLimiter(cc int) *ConnLimiter {
return &ConnLimiter{
concurrentConn: cc,
bucket: make(chan int, cc),
}
}
func (cl *ConnLimiter) GetConn() bool {
if len(cl.bucket) >= cl.concurrentConn {
log.Printf("Reached the rate limitation.")
return false
}
cl.bucket <- 1
log.Printf("Successfully got connection")
return true
}
func (cl *ConnLimiter) ReleaseConn() {
c := <-cl.bucket
log.Printf("New connection coming: %d", c)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/newdas/video_server.git
git@gitee.com:newdas/video_server.git
newdas
video_server
video_server
cbae10443da8

搜索帮助