-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Daily Debug #4
Comments
WorkloadSender (MSRA Project)1. Golang Error: umarshal后的结构体内各字段的value全为空Reason:结构体必须是以大写字母开头的成员才会被json处理到,小写字母开头的成员不会有影响。Marshal时,结构体的成员变量名将会直接作为json object的key打包成json;Unmarshal时,会自动匹配对应的变量名进行赋值,大小写不敏感。 2. Golang net/http Error: too many open filesReason:程序打开的文件/socket连接数(即所有的文件描述符数量)超过系统限制值。改大系统文件描述符个数即可。 查看每个用户最大允许打开文件数量:
修改系统文件描述符个数
单独获取进程文件数限制:
查看进程打开的文件数 :
3. Golang net/http Error: cannot assign requested addressReason:原因是linux分配的客户端连接端口用尽,无法建立socket连接所致,虽然socket正常关闭,但是端口不是立即释放,而是处于TIME_WAIT状态,默认等60s后才释放。可通过修改内核参数解决这一问题。 查看TIME_WAIT数量:
执行命令修改如下内核参数(需要root权限) 调低端口释放后的等待时间,默认为60s,修改为15s:
修改tcp/ip协议配置,修改为1,释放TIME_WAIT端口给新连接使用:
允许端口重用:
4. Golang net/http Error: connection reset & EOFReason:当客户端连续发出请求时可能发生这个错误,原因是server已经关闭了连接 Golang默认允许连接重用。Golang实现connection的方式是使用两个goroutine(net/http/transport.go),一个响应写请求,一个响应读请求(readLoop和writeLoop),一般情况下readLoop会检测到socket的关闭并关闭connection。如果一个旧的请求断开,新的请求在readLoop检测到关闭之前就到来,就会产生EOF错误并中断执行,而不是去关闭前一个请求。
可以解决这个问题,因为可以避免连接重用。 reader := bytes.NewReader(data)
request, err := http.NewRequest("POST", url, reader)
request.Header.Set("Content-Type", "application/json")
request.Close = true
client := &http.Client{}
response, err := client.Do(request)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
fmt.Printf(string(body)) 5. Golang net/http Error: http: ContentLength=xxx with bodu length 0Reason:当POST请求发送失败时重复发送请求会产生这个问题,问题原因是req创建不能放到for循环之外,因为Do方法会关闭req中的Body读取,因此同一个req再次传参给Do时,Body的读方法已经关闭,不能读到Body内容。 解决方法是:每次调用Do前创建一个req client := &http.Client{}
req, err := http.NewRequest("POST", entry.Url, bytes.NewReader(sendBytes))
req.Close = true //
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
for err != nil {
log.Printf("Error: %s\n", err)
time.Sleep(time.Duration(10) * time.Millisecond)
client = &http.Client{}
req, err = http.NewRequest("POST", entry.Url, bytes.NewReader(sendBytes))
req.Close = true //
req.Header.Set("Content-Type", "application/json")
resp, err = client.Do(req)
}
ret, _ := ioutil.ReadAll(resp.Body)
log.Println("Node -- " + string(ret))
resp.Body.Close() |
No description provided.
The text was updated successfully, but these errors were encountered: