我正在使用 Listenandserve 启动 Go API 来接受 HTTP 请求。
我怎样才能实现以下目标?
允许最多 100 个并发 HTTP 请求
第 101 个请求(以及任何其他请求)应等待 10 分钟,以尝试落入“100 个同时”限制(即希望前 100 个请求中的一些请求应该完成)
如果 10 分钟过去了并且没有打开可用的请求“槽”,则为一直在等待的请求返回错误
接下来运行请求 101...102...x 的顺序并不重要
当前版本完全不可用:
timeout := time.After(10 * time.Minute)
tick := time.Tick(15 * time.Second)
fullcmdfirst := fmt.Sprintf("netstat -anp | grep procname | grep ESTABLISHED | grep -v grep | wc -l")
outputfirst, err1first := exec.Command("/bin/sh", "-c", fullcmdfirst).CombinedOutput()
if strconv.ParseFloat(string(outputfirst)) < 100 {
return nil
}
// Keep trying until we're timed out or lock acquired
for {
select {
// Got a timeout! fail with a timeout error
case <-timeout:
return errors.New("Error: timed out ")
// Got a tick, we should check if we can acquire
case <-tick:
fullcmd := fmt.Sprintf("netstat -anp | grep procname | grep ESTABLISHED | grep -v grep | wc -l")
output, err1 := exec.Command("/bin/sh", "-c", fullcmd).CombinedOutput()
if strconv.ParseFloat(string(outputfirst)) < 100 {
l.Printf("start req")
return nil
}
}
}
HUH函数
相关分类