gpt4 book ai didi

go - 一个 Go 程序可以同时启动 8k+ Goroutines 吗?

转载 作者:数据小太阳 更新时间:2023-10-29 03:41:15 25 4
gpt4 key购买 nike

<分区>

我需要启动多个 goroutines 来发送 http 请求。以下是我的代码:

func InteractWithCheck(clusterId int, tableName string, key string, jobs <-chan int, results chan<- objRes) error {
// the host only test
host := "http://xxx.yyy.com/a/b?method=check&"
for _ = range jobs {
url := fmt.Sprintf("%scluster=%d&table=%s&key=%s", host, clusterId, tableName, key)
for intRetry := 0; intRetry < 3; intRetry++ {
resp, err := http.Get(url)
if err != nil {
continue
}
if resp.StatusCode >= 500 {
continue
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// ...
return nil
}
}
return nil
}

DataCheck会用go调用InteractWithCheck,代码如下:

func DataCheck(clusterId int, tableName string, keyList []string) error {
// ...

for _, sliceKey := range keyList {
// when len(keyList) > 8000, it will be hang
go InteractWithCheck(clusterId, tableName, sliceKey, jobs, results)
}

for j := 0; j < num; j++ {
jobs <- j
}
close(jobs)

for k := 0; k < num; k++ {
select {
case res := <-results:
// check result
// ...
}
}
return nil
}

len(keylist) >= 8000时,程序会挂掉,需要优化吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com