【发布时间】:2019-12-30 13:29:57
【问题描述】:
当我进行负载测试时,我得到 dial 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()
}
}
提前致谢。
【问题讨论】:
-
不,没有。如果你达到了打开文件描述符的限制,唯一的办法就是增加限制。
-
@hobbs 你能建议我限制更少套接字的最佳方法吗?
-
从错误来看,这听起来像是您的进程,或者在一般操作系统中(linux?)-打开了太多套接字。您可以通过查看
ls /proc/$pid/fd.. 来验证这一点。跨度>
标签: go