-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
cancelreader_select.go
136 lines (108 loc) · 3.01 KB
/
cancelreader_select.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//go:build solaris || darwin || freebsd || netbsd || openbsd || dragonfly
// +build solaris darwin freebsd netbsd openbsd dragonfly
package cancelreader
import (
"errors"
"fmt"
"io"
"os"
"strings"
"golang.org/x/sys/unix"
)
// newSelectCancelReader returns a reader and a cancel function. If the input
// reader is a File, the cancel function can be used to interrupt a
// blocking call read call. In this case, the cancel function returns true if
// the call was canceled successfully. If the input reader is not a File or
// the file descriptor is 1024 or larger, the cancel function does nothing and
// always returns false. The generic unix implementation is based on the posix
// select syscall.
func newSelectCancelReader(reader io.Reader) (CancelReader, error) {
file, ok := reader.(File)
if !ok || file.Fd() >= unix.FD_SETSIZE {
return newFallbackCancelReader(reader)
}
r := &selectCancelReader{file: file}
var err error
r.cancelSignalReader, r.cancelSignalWriter, err = os.Pipe()
if err != nil {
return nil, err
}
return r, nil
}
type selectCancelReader struct {
file File
cancelSignalReader File
cancelSignalWriter File
cancelMixin
}
func (r *selectCancelReader) Read(data []byte) (int, error) {
if r.isCanceled() {
return 0, ErrCanceled
}
for {
err := waitForRead(r.file, r.cancelSignalReader)
if err != nil {
if errors.Is(err, unix.EINTR) {
continue // try again if the syscall was interrupted
}
if errors.Is(err, ErrCanceled) {
// remove signal from pipe
var b [1]byte
_, readErr := r.cancelSignalReader.Read(b[:])
if readErr != nil {
return 0, fmt.Errorf("reading cancel signal: %w", readErr)
}
}
return 0, err
}
return r.file.Read(data)
}
}
func (r *selectCancelReader) Cancel() bool {
r.setCanceled()
// send cancel signal
_, err := r.cancelSignalWriter.Write([]byte{'c'})
return err == nil
}
func (r *selectCancelReader) Close() error {
var errMsgs []string
// close pipe
err := r.cancelSignalWriter.Close()
if err != nil {
errMsgs = append(errMsgs, fmt.Sprintf("closing cancel signal writer: %v", err))
}
err = r.cancelSignalReader.Close()
if err != nil {
errMsgs = append(errMsgs, fmt.Sprintf("closing cancel signal reader: %v", err))
}
if len(errMsgs) > 0 {
return fmt.Errorf(strings.Join(errMsgs, ", "))
}
return nil
}
func waitForRead(reader, abort File) error {
readerFd := int(reader.Fd())
abortFd := int(abort.Fd())
maxFd := readerFd
if abortFd > maxFd {
maxFd = abortFd
}
// this is a limitation of the select syscall
if maxFd >= unix.FD_SETSIZE {
return fmt.Errorf("cannot select on file descriptor %d which is larger than 1024", maxFd)
}
fdSet := &unix.FdSet{}
fdSet.Set(int(reader.Fd()))
fdSet.Set(int(abort.Fd()))
_, err := unix.Select(maxFd+1, fdSet, nil, nil, nil)
if err != nil {
return fmt.Errorf("select: %w", err)
}
if fdSet.IsSet(abortFd) {
return ErrCanceled
}
if fdSet.IsSet(readerFd) {
return nil
}
return fmt.Errorf("select returned without setting a file descriptor")
}