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

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

内容简介:代码如下:报错信息如下:我这边设置的超时时间是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不至于这样吧,麻烦大神帮我看看问题出在什么地方了吗


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

查看所有标签

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

Node.js硬实战:115个核心技巧

Node.js硬实战:115个核心技巧

【美】Alex R. Young、【美】Marc Harter / 承竹、慕陶、邱娟、达峰 / 电子工业出版社 / 2017-1 / 109.9

《Node.js 硬实战:115 个核心技巧》是一本面向实战的Node.js 开发进阶指南。作为资深专家,《Node.js 硬实战:115 个核心技巧》作者独辟蹊径,将着眼点放在Node.js 的核心模块和网络应用,通过精心组织的丰富实例,向读者充分展示了Node.js 强大的并发处理能力,读者从中可真正掌握Node 的核心基础与高级技巧。《Node.js 硬实战:115 个核心技巧》总共有三部分......一起来看看 《Node.js硬实战:115个核心技巧》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具