代码如下:
```
func NewHttping(method, protocol string, timeoutDuration time.Duration) *Httping {
return &Httping{
Method: method,
Protocol: protocol,
Client: &http.Client{
Timeout: timeoutDuration,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
},
}
}
func (httping *Httping) Ping(t model.TargetIp) (float64, *http.Response, error) {
var resp *http.Response
var body io.Reader
if httping.Method == "POST" {
body = bytes.NewBufferString("{}")
}
req, err := http.NewRequest(httping.Method, fmt.Sprintf("%s://%s:%d", httping.Protocol, t.IP, t.Port), body)
if t.Header != "" {
head := map[string]interface{}{}
err := json.Unmarshal([]byte(t.Header), &head)
if err != nil {
seelog.Error("[func:httping is err]", err.Error())
}
for key, val := range head {
req.Header.Add(key, val.(string))
}
}
if err != nil {
return 0, nil, err
}
duration, errIfce := utils.TimeIt(func() interface{} {
resp, err = httping.Client.Do(req)
return err
})
if errIfce != nil {
err := errIfce.(error)
return 0, nil, err
}
return duration, resp, nil
}
```
报错信息如下:
```
2019-05-13/18:15:03 [Error] httping.go [func:HTTP StartPing ] www.youku.com - failed: Get http://www.youku.com:80: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
```
我这边设置的超时时间是10s,程序以间隔2秒的时间差循环发送求情,但是经常报错超时,我想着youku不至于这样吧,麻烦大神帮我看看问题出在什么地方了吗
有疑问加站长微信联系(非本文作者))