forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iox.go
82 lines (77 loc) · 2.25 KB
/
iox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package netxlite
//
// I/O extensions
//
import (
"context"
"errors"
"io"
)
// TODO(bassosimone): consider integrating StreamAllContext from
// internal/experiment/webconnectivity/iox.go
// ReadAllContext is like io.ReadAll but reads r in a
// background goroutine. This function will return
// earlier if the context is cancelled. In which case
// we will continue reading from the reader in the background
// goroutine, and we will discard the result. To stop
// the long-running goroutine, close the connection
// bound to the reader. Until such a connection is closed,
// you're leaking the backround goroutine and doing I/O.
//
// As of Go 1.17.6, ReadAllContext additionally deals
// with wrapped io.EOF correctly, while io.ReadAll does
// not. See https://github.com/ooni/probe/issues/1965.
func ReadAllContext(ctx context.Context, r io.Reader) ([]byte, error) {
datach, errch := make(chan []byte, 1), make(chan error, 1) // buffers
go func() {
data, err := io.ReadAll(r)
if errors.Is(err, io.EOF) {
// See https://github.com/ooni/probe/issues/1965
err = nil
}
if err != nil {
errch <- err
return
}
datach <- data
}()
select {
case data := <-datach:
return data, nil
case <-ctx.Done():
return nil, NewTopLevelGenericErrWrapper(ctx.Err())
case err := <-errch:
return nil, NewTopLevelGenericErrWrapper(err)
}
}
// CopyContext is like io.Copy but may terminate earlier
// when the context expires. This function has the same
// caveats of ReadAllContext regarding the temporary leaking
// of the background I/O goroutine.
//
// As of Go 1.17.6, CopyContext additionally deals
// with wrapped io.EOF correctly, while io.Copy does
// not. See https://github.com/ooni/probe/issues/1965.
func CopyContext(ctx context.Context, dst io.Writer, src io.Reader) (int64, error) {
countch, errch := make(chan int64, 1), make(chan error, 1) // buffers
go func() {
count, err := io.Copy(dst, src)
if errors.Is(err, io.EOF) {
// See https://github.com/ooni/probe/issues/1965
err = nil
}
if err != nil {
errch <- err
return
}
countch <- count
}()
select {
case count := <-countch:
return count, nil
case <-ctx.Done():
return 0, NewTopLevelGenericErrWrapper(ctx.Err())
case err := <-errch:
return 0, NewTopLevelGenericErrWrapper(err)
}
}