Skip to content

Commit

Permalink
change: use http ping
Browse files Browse the repository at this point in the history
  • Loading branch information
r3inbowari committed Feb 22, 2023
1 parent 3ba1c54 commit 4a107ff
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
55 changes: 45 additions & 10 deletions speedtest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -33,7 +32,7 @@ func (s *Server) DownloadTestContext(ctx context.Context) error {

func (s *Server) downloadTestContext(ctx context.Context, downloadRequest downloadFunc) error {
s.Context.DownloadRateCaptureHandler(func() {
_ = downloadRequest(ctx, s, 5)
_ = downloadRequest(ctx, s, 3)
})
s.DLSpeed = s.Context.GetAvgDownloadRate()
return nil
Expand All @@ -51,7 +50,7 @@ func (s *Server) UploadTestContext(ctx context.Context, savingMode bool) error {

func (s *Server) uploadTestContext(ctx context.Context, uploadRequest uploadFunc) error {
s.Context.UploadRateCaptureHandler(func() {
_ = uploadRequest(ctx, s, 5)
_ = uploadRequest(ctx, s, 4)
})
s.ULSpeed = s.Context.GetAvgUploadRate()
return nil
Expand Down Expand Up @@ -99,12 +98,10 @@ func (s *Server) PingTest() error {

// PingTestContext executes test to measure latency, observing the given context.
func (s *Server) PingTestContext(ctx context.Context) error {
URL, err := url.ParseRequestURI(s.URL)
if err != nil {
return err
}
pingURL := strings.Split(URL.Host, ":")[0]
vectorPingResult, err := s.StdPing(ctx, pingURL, 6666, 32, 10, time.Millisecond*200, nil)

pingURL := strings.Split(s.URL, "/upload.php")[0] + "/latency.txt"

vectorPingResult, err := s.HTTPPing(ctx, pingURL, 10, time.Millisecond*200, nil)
if err != nil || len(vectorPingResult) == 0 {
return err
}
Expand All @@ -117,7 +114,45 @@ func (s *Server) PingTestContext(ctx context.Context) error {
return nil
}

func (s *Server) StdPing(
func (s *Server) HTTPPing(
ctx context.Context,
dst string,
echoTimes int,
echoFreq time.Duration,
callback func(latency time.Duration),
) ([]int64, error) {

failTimes := 0
var latencies []int64

for i := 0; i < echoTimes; i++ {
sTime := time.Now()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, dst, nil)
if err != nil {
return nil, err
}

resp, err := s.Context.doer.Do(req)

endTime := time.Since(sTime)
if err != nil {
failTimes++
continue
}
resp.Body.Close()
latencies = append(latencies, endTime.Nanoseconds()/2)
if callback != nil {
callback(endTime)
}
time.Sleep(echoFreq)
}
if failTimes == echoTimes {
return nil, errors.New("server connect timeout")
}
return latencies, nil
}

func (s *Server) ICMPPing(
ctx context.Context,
dst string,
readTimeout int,
Expand Down
12 changes: 4 additions & 8 deletions speedtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,12 @@ func (s *Speedtest) FetchServerListContext(ctx context.Context, user *User) (Ser
}

var wg sync.WaitGroup
pCtx, fc := context.WithTimeout(context.Background(), time.Second*5)
for _, server := range servers {
URL, err1 := url.ParseRequestURI(server.URL)
if err1 != nil {
server.Latency = -1
continue
}

pingURL := strings.Split(URL.Host, ":")[0]
wg.Add(1)
go func(gs *Server) {
if latency, err2 := gs.StdPing(ctx, pingURL, 2000, 32, 1, time.Millisecond*100, nil); err2 != nil || len(latency) != 1 {
pingURL := strings.Split(gs.URL, "/upload.php")[0] + "/latency.txt"
if latency, err2 := gs.HTTPPing(pCtx, pingURL, 1, time.Millisecond, nil); err2 != nil || len(latency) != 1 {
gs.Latency = -1
} else {
gs.Latency = time.Duration(latency[0]) * time.Nanosecond
Expand All @@ -216,6 +211,7 @@ func (s *Speedtest) FetchServerListContext(ctx context.Context, user *User) (Ser
}

wg.Wait()
fc()
return servers, nil
}

Expand Down
2 changes: 1 addition & 1 deletion speedtest/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
version = "1.4.1"
version = "1.4.2"
DefaultUserAgent = fmt.Sprintf("showwin/speedtest-go %s", version)
)

Expand Down

0 comments on commit 4a107ff

Please sign in to comment.