主题
文章
项目
资源
图书
Go网址导航
下载
官方文档
英文文档
中文文档
标准库中文版
Go指南
注册
登录
首页
会员
lgy1027的信息
TA的收藏
初级会员
第 36586 位会员
ID:
lgy1027
名字:
liguoyu
注册时间:
2019-05-10 14:03:37
是否在线:
Offline
财富:
19
52
更多>>
最近发布的主题
Go语言
go解析yaml文件如何处理多文档问题
at
2020-09-11 11:46:04
Go性能
Go内存泄漏如何定位?
at
2019-07-24 16:35:18
更多>>
最近发布的文章
go循环发送http请求经常报错超时
at
2019-05-13 18:27:31
更多>>
最近分享的资源
暂无
更多>>
最近发布的项目
暂无
更多>>
最近的评论
2019-05-14 14:58:45
评论了
博文
go循环发送http请求经常报错超时
#3
楼
@polaris
我这边做了个定时器,代码 ```go timeoutDuration, err := time.ParseDuration(target.Timeout) if err != nil { seelog.Error("[func:Start HTTP-PING is duration err ]", err) } pinger = service.NewHttping(strings.ToUpper(target.Method), strings.ToLower(target.Protocol),timeoutDuration) ```
2019-05-14 14:55:26
评论了
博文
go循环发送http请求经常报错超时
```go func (httping *Httping) StartPing(t model.TargetIp, wg *sync.WaitGroup) { times := make([]string, t.Count) stat := &model.PingSt{ Protocol: httping.Protocol, AName: inital.Cfg.Name, } stat.ShortT = -1 speedTemp := 0.0 for i := 0; i < t.Count; i++ { delay, resp, err := httping.Ping(t) if err == nil { defer resp.Body.Close() length, _ := io.Copy(ioutil.Discard, resp.Body) if resp.StatusCode != 200 { stat.Status = resp.Status stat.RespCode = resp.StatusCode seelog.Errorf("[func:HTTP StartPing ] %s - failed: status:%s \n", t.IP, resp.Status) stat.LostN = stat.LostN + 1 times[stat.SendN] = "-1" } else { speed := float64(length) / (delay / 1000) / 1024 stat.Status = resp.Status stat.RespCode = resp.StatusCode stat.SumT += delay speedTemp += speed if stat.LongT < delay { stat.LongT = delay } if stat.ShortT == -1 || stat.ShortT > delay { stat.ShortT = delay } stat.RecvN = stat.RecvN + 1 times[stat.SendN] = fmt.Sprintf("%.3f", delay) } } else { seelog.Errorf("[func:HTTP StartPing ] %s - failed: %s\n", t.IP, err.Error()) stat.LostN = stat.LostN + 1 times[stat.SendN] = "-1" } stat.SendN = stat.SendN + 1 stat.Pat = (float64(stat.LostN) / float64(stat.SendN)) * 100 time.Sleep(2 * time.Second) } pingTimes := "" for i := 0; i < len(times); i++ { if i == len(times)-1 { pingTimes += times[i] } else { pingTimes += times[i] + "," } } stat.Times = pingTimes if stat.RecvN > 0 { stat.MrTime = stat.SumT / float64(stat.RecvN) f, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", speedTemp/float64(stat.RecvN)), 64) stat.Speed = f } else { stat.MrTime = 0.0 stat.Speed = 0.0 } wg.Done() } ```
2019-05-14 09:54:35
评论了
博文
go循环发送http请求经常报错超时
#1
楼
@polaris
没,在每四次请求后会重新调用NewHttping()生成新的client
最新主题
|
最新资源
|
最新评论
X
登录和大家一起探讨吧
用户名
密码
记住登录状态
登录
GitHub 登录
忘记密码?
还不是会员
现在注册
最近的评论