Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 137 additions & 18 deletions defs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"slices"
"strconv"
"sync"
"sync/atomic"
"time"

"github.com/briandowns/spinner"
Expand Down Expand Up @@ -401,23 +402,56 @@ func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chu
downloadDone := make(chan struct{}, requests)

var wg sync.WaitGroup
// whether any request was answered with data, and why the last one that
// was not failed
var succeeded atomic.Bool
var lastErr atomic.Pointer[error]
var failed bool

// fetch makes one download request. An error means it brought no test
// data: the request failed, or the answer was not a success.
fetch := func() error {
resp, err := http.DefaultClient.Do(req.Clone(ctx))
if err != nil {
return err
}
defer resp.Body.Close()

doDownload := func() {
defer wg.Done()
if err := refusal("download", resp); err != nil {
// an error page is not test data, so it is not counted
return err
}

reqClone := req.Clone(ctx)
resp, err := http.DefaultClient.Do(reqClone)
if err != nil {
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
output.WriteDebug("Failed when making HTTP request: %s\n", err)
n, err := io.Copy(io.Discard, io.TeeReader(resp.Body, counter))
if n == 0 {
// success without a single byte is not test data either
if err == nil {
err = errors.New("the download request returned no data")
}
return
return err
}
defer resp.Body.Close()
// A body that was cut short counts all the same, whoever cut it:
// the end of the test, --timeout or the server. What arrived did
// cross the link, and on a slow link no request gets its whole body.
succeeded.Store(true)
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
output.WriteDebug("Failed when reading HTTP response: %s\n", err)
}
return nil
}

if _, err = io.Copy(io.Discard, io.TeeReader(resp.Body, counter)); err != nil {
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
output.WriteDebug("Failed when reading HTTP response: %s\n", err)
doDownload := func() {
defer wg.Done()

if err := fetch(); err != nil {
// cancellation is how the test ends, so it is not a failure
if ctx.Err() != nil {
return
}
output.WriteDebug("Download request failed: %s\n", err)
lastErr.Store(&err)
if !pauseBeforeRetry(ctx) {
return
}
}

Expand Down Expand Up @@ -452,6 +486,9 @@ func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chu
// not a terminal
defer func() {
pb.Stop()
if failed {
return
}
if useBytes {
output.WriteUI("Download rate:\t%s\n", counter.AvgHumanize())
} else {
Expand Down Expand Up @@ -485,6 +522,15 @@ Loop:
// result doesn't change under us while it's being reported
wg.Wait()

// No request answered with data is not a result of 0 Mbps. Without an
// error either, they were all still waiting when the test ended.
if !succeeded.Load() {
failed = true
if err := lastErr.Load(); err != nil {
return 0, 0, *err
}
return 0, 0, errors.New("no download request was answered before the test ended")
}
return counter.AvgMbps(), counter.Total(), nil
}

Expand Down Expand Up @@ -520,6 +566,11 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int
uploadDone := make(chan struct{}, requests)

var wg sync.WaitGroup
// whether any request was accepted, and why the last one that was not
// was refused
var succeeded atomic.Bool
var lastErr atomic.Pointer[error]
var failed bool

doUpload := func() {
defer wg.Done()
Expand Down Expand Up @@ -555,18 +606,39 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int
}

resp, err := http.DefaultClient.Do(uploadReq)
if err != nil {
if err != nil && resp == nil {
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
output.WriteDebug("Failed when making HTTP request: %s\n", err)
}
return
}
defer resp.Body.Close()

if _, err := io.Copy(io.Discard, resp.Body); err != nil {
// cancellation is how the test ends, so it is not a failure
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
output.WriteDebug("Failed when reading HTTP response: %s\n", err)
// An error that comes with a response is a redirect the client did
// not accept. That is a refusal too, and the body is closed already.
if err == nil {
// A request is accepted once the server answers it with success:
// it has the payload then, whatever becomes of the rest of its
// answer.
if err = refusal("upload", resp); err == nil {
succeeded.Store(true)
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
// cancellation is how the test ends, so it is not a failure
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
output.WriteDebug("Failed when reading HTTP response: %s\n", err)
}
}
}
// closed before the pause below, not after it
resp.Body.Close()
}

if err != nil {
// What was sent of the body stays counted: it did cross the
// link. A test where no request is accepted fails all the same.
output.WriteDebug("Upload request failed: %s\n", err)
lastErr.Store(&err)
if !pauseBeforeRetry(ctx) {
return
}
}

Expand Down Expand Up @@ -601,6 +673,9 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int
// not a terminal
defer func() {
pb.Stop()
if failed {
return
}
if useBytes {
output.WriteUI("Upload rate:\t%s\n", counter.AvgHumanize())
} else {
Expand Down Expand Up @@ -634,9 +709,53 @@ Loop:
// result doesn't change under us while it's being reported
wg.Wait()

// nothing but refused requests is not a result
if err := lastErr.Load(); err != nil && !succeeded.Load() {
failed = true
return 0, 0, *err
}
return counter.AvgMbps(), counter.Total(), nil
}

// retryDelay spaces the requests that follow a failed one, so that a wrong
// URL is not asked for as fast as the server can refuse it.
const retryDelay = 200 * time.Millisecond

// errorPageLimit is how much of a refused answer is read before it is closed.
const errorPageLimit = 4 << 10

// refusal reports a transfer request that was not answered with success.
// Any 2xx status is success. Everything else comes with an error page or
// nothing, not with test data, and that includes a redirect the client did
// not follow.
//
// A short error page is read to its end, so that the retry can use the
// connection again and does not open a new one each time. Only newer Go
// versions do that on their own. A page that does not say how long it is
// is left alone: reading it could wait for a server that never ends it.
func refusal(phase string, resp *http.Response) error {
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
return nil
}
if n := resp.ContentLength; n > 0 && n <= errorPageLimit {
io.CopyN(io.Discard, resp.Body, n)
}
if text := http.StatusText(resp.StatusCode); text != "" {
return fmt.Errorf("the %s request returned HTTP %d %s", phase, resp.StatusCode, text)
}
return fmt.Errorf("the %s request returned HTTP %d", phase, resp.StatusCode)
}

// pauseBeforeRetry waits retryDelay and reports whether the test is still on.
func pauseBeforeRetry(ctx context.Context) bool {
select {
case <-time.After(retryDelay):
return true
case <-ctx.Done():
return false
}
}

// GetIPInfo accesses the backend's getIP.php endpoint and get current client's IP information
func (s *Server) GetIPInfo(distanceUnit string) (*GetIPResult, error) {
t := time.Now()
Expand Down
Loading