gpt4 book ai didi

go - 调用 tcp : socket: too many open files

转载 作者:行者123 更新时间:2023-12-01 20:27:21 24 4
gpt4 key购买 nike

我在做负载测试时遇到拨号 tcp:socket:too many open files 错误。通过设置 ulimit 其工作正常但是有没有其他解决方案而不设置 ulimit?

代码:

package main
import (
"fmt"
"io"
"io/ioutil"
"encoding/json"
"net/http"
)

type Struct_Response struct {
Meta struct {
Requestid string
}
}

var HttpClient = &http.Client{}

func main(){
apiUrl := "http://example.com"
JsonStr :="teststr"
conn_token :="1233333333333"
req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(JsonStr))
if err!=nil{
fmt.Println(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("requestid", "1234")
req.Header.Set("Authorization", "Bearer "+conn_token)
req.Header.Set("Connection", "close")

resp, err := HttpClient.Do(req)
req.Close=true
if resp!=nil && resp.StatusCode==200 {
body, _ := ioutil.ReadAll(resp.Body)
var Responce Struct_Response
err := json.Unmarshal([]byte(string(body)), &Responce)
if err != nil {
fmt.Println(err)
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
}

提前致谢。

最佳答案

您的问题可能是您没有完全关闭连接,这会导致在重用 TCP 端口客户端端口号之前添加延迟。

在上面的代码示例中,响应主体仅在状态为 200 时才被使用和关闭。当响应主体存在时,您应该始终使用/关闭响应主体。

关于go - 调用 tcp : socket: too many open files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525750/

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