From d934c8360ab326d230e4dc7d24a67296a7bb77db Mon Sep 17 00:00:00 2001 From: Omar Date: Mon, 25 Dec 2023 11:50:57 +0300 Subject: [PATCH 1/3] close ping response body --- speedtest/request.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/speedtest/request.go b/speedtest/request.go index 17823a3..474a8fb 100644 --- a/speedtest/request.go +++ b/speedtest/request.go @@ -4,13 +4,15 @@ import ( "context" "errors" "fmt" - "github.com/showwin/speedtest-go/speedtest/tcp" + "io" "math" "net/http" "net/url" "strconv" "strings" "time" + + "github.com/showwin/speedtest-go/speedtest/tcp" ) type ( @@ -274,9 +276,11 @@ func (s *Server) HTTPPing( } for i := 0; i < echoTimes; i++ { sTime := time.Now() - _, err = s.Context.doer.Do(req) + resp, err := s.Context.doer.Do(req) endTime := time.Since(sTime) if err != nil { + io.Copy(io.Discard, resp.Body) + resp.Body.Close() failTimes++ continue } From 0c5e9c270b015a34339f499c66e83f7749e3d005 Mon Sep 17 00:00:00 2001 From: Omar Date: Mon, 25 Dec 2023 23:10:43 +0300 Subject: [PATCH 2/3] fix-test --- speedtest/request.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/speedtest/request.go b/speedtest/request.go index 474a8fb..3171559 100644 --- a/speedtest/request.go +++ b/speedtest/request.go @@ -279,11 +279,11 @@ func (s *Server) HTTPPing( resp, err := s.Context.doer.Do(req) endTime := time.Since(sTime) if err != nil { - io.Copy(io.Discard, resp.Body) - resp.Body.Close() failTimes++ continue } + io.Copy(io.Discard, resp.Body) + resp.Body.Close() latencies = append(latencies, endTime.Nanoseconds()/2) dbg.Printf("2RTT: %s\n", endTime) if callback != nil { From 7236c5b99613a493cd795ace5c0ce0f34c6b604a Mon Sep 17 00:00:00 2001 From: r3inbowari Date: Tue, 26 Dec 2023 04:35:27 +0800 Subject: [PATCH 3/3] fix lint error --- speedtest/request.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/speedtest/request.go b/speedtest/request.go index 3171559..332bb63 100644 --- a/speedtest/request.go +++ b/speedtest/request.go @@ -282,8 +282,8 @@ func (s *Server) HTTPPing( failTimes++ continue } - io.Copy(io.Discard, resp.Body) - resp.Body.Close() + _, _ = io.Copy(io.Discard, resp.Body) + _ = resp.Body.Close() latencies = append(latencies, endTime.Nanoseconds()/2) dbg.Printf("2RTT: %s\n", endTime) if callback != nil {