初级会员
  • 第 36586 位会员
  • lgy1027
  • liguoyu
  • 2019-05-10 14:03:37
  • Offline
  • 19 52

最近分享的资源

    暂无

最近发布的项目

    暂无

最近的评论

  • #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) ```
  • ```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() } ```
  • #1 @polaris 没,在每四次请求后会重新调用NewHttping()生成新的client