fix(all): introduce and use iox.ReadAllContext (#379)
* fix(all): introduce and use iox.ReadAllContext This improvement over the ioutil.ReadAll utility returns early if the context expires. This enables us to unblock stuck code in case there's censorship confounding the TCP stack. See https://github.com/ooni/probe/issues/1417. Compared to the functionality postulated in the above mentioned issue, I choose to be more generic and separate limiting the maximum body size (not implemented here) from using the context to return early when reading a body (or any other reader). After implementing iox.ReadAllContext, I made sure we always use it everywhere in the tree instead of ioutil.ReadAll. This includes many parts of the codebase where in theory we don't need iox.ReadAllContext. Though, changing all the places makes checking whether we're not using ioutil.ReadAll where we should not be using it easy: `git grep` should return no lines. * Update internal/iox/iox_test.go * fix(ndt7): treat context errors as non-errors The rationale is explained by the comment documenting reduceErr. * Update internal/engine/experiment/ndt7/download.go
This commit is contained in:
@@ -2,11 +2,12 @@ package ndt7
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/ooni/probe-cli/v3/internal/iox"
|
||||
)
|
||||
|
||||
type downloadManager struct {
|
||||
@@ -33,6 +34,30 @@ func newDownloadManager(
|
||||
}
|
||||
|
||||
func (mgr downloadManager) run(ctx context.Context) error {
|
||||
return mgr.reduceErr(mgr.doRun(ctx))
|
||||
}
|
||||
|
||||
// reduceErr treats as non-errors the errors caused by the context
|
||||
// so that we can focus instead on network errors.
|
||||
//
|
||||
// This function was introduced by https://github.com/ooni/probe-cli/pull/379
|
||||
// since before such a PR we did not see context interrupting
|
||||
// errors when we were reading messages. Since before such a PR
|
||||
// we used to return `nil` on context errors, this function is
|
||||
// here to keep the previous behavior by filtering the error
|
||||
// returned when reading messages, given that now reading messages
|
||||
// can fail midway because we use iox.ReadAllContext.
|
||||
func (mgr downloadManager) reduceErr(err error) error {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (mgr downloadManager) doRun(ctx context.Context) error {
|
||||
var total int64
|
||||
start := time.Now()
|
||||
if err := mgr.conn.SetReadDeadline(start.Add(mgr.maxRuntime)); err != nil {
|
||||
@@ -47,7 +72,7 @@ func (mgr downloadManager) run(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
if kind == websocket.TextMessage {
|
||||
data, err := ioutil.ReadAll(reader)
|
||||
data, err := iox.ReadAllContext(ctx, reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -57,7 +82,7 @@ func (mgr downloadManager) run(ctx context.Context) error {
|
||||
}
|
||||
continue
|
||||
}
|
||||
n, err := io.Copy(ioutil.Discard, reader)
|
||||
n, err := io.Copy(io.Discard, reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -143,3 +143,20 @@ type goodJSONReader struct{}
|
||||
func (r *goodJSONReader) Read(p []byte) (int, error) {
|
||||
return copy(p, []byte(`{}`)), io.EOF
|
||||
}
|
||||
|
||||
func TestDownloadReduceErr(t *testing.T) {
|
||||
mgr := downloadManager{}
|
||||
if mgr.reduceErr(context.Canceled) != nil {
|
||||
t.Fatal("reduceErr not working as intended for context.Canceled")
|
||||
}
|
||||
if mgr.reduceErr(context.DeadlineExceeded) != nil {
|
||||
t.Fatal("reduceErr not working as intended for context.DeadlineExceeded")
|
||||
}
|
||||
if mgr.reduceErr(nil) != nil {
|
||||
t.Fatal("reduceErr not working as intended for nil")
|
||||
}
|
||||
expected := errors.New("mocked error")
|
||||
if mgr.reduceErr(expected) != expected {
|
||||
t.Fatal("reduceErr not working as intended for other errors")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user