go循环发送http请求经常报错超时

栏目: Go · 发布时间: 6年前

内容简介:代码如下:报错信息如下:我这边设置的超时时间是10s,程序以间隔2秒的时间差循环发送求情,但是经常报错超时,我想着youku不至于这样吧,麻烦大神帮我看看问题出在什么地方了吗

代码如下:

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不至于这样吧,麻烦大神帮我看看问题出在什么地方了吗


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Little Schemer - 4th Edition

The Little Schemer - 4th Edition

Daniel P. Friedman、Matthias Felleisen / The MIT Press / 1995-12-21 / USD 40.00

This delightful book leads you through the basic elements of programming in Scheme (a Lisp dialect) via a series of dialogues with well-chosen questions and exercises. Besides teaching Scheme, The Lit......一起来看看 《The Little Schemer - 4th Edition》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具