-
Notifications
You must be signed in to change notification settings - Fork 147
/
io.go
43 lines (38 loc) · 1022 Bytes
/
io.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
package core
import (
"errors"
"io"
)
var (
// errWritePreempted indicates that a write was preempted in a
// preemptableWriter instance.
errWritePreempted = errors.New("write preempted")
)
// preemptableWriter is an io.Writer implementation that checks for preemption
// every N writes.
type preemptableWriter struct {
// cancelled is the channel that, when closed, indicates preemption.
cancelled <-chan struct{}
// writer is the underlying writer.
writer io.Writer
// checkInterval is the number of writes to allow between preemption checks.
checkInterval uint
// writeCount is the number of writes since the last preemption check.
writeCount uint
}
// Write implements io.Writer.Write.
func (w *preemptableWriter) Write(data []byte) (int, error) {
// Handle preemption checking.
if w.writeCount >= w.checkInterval {
select {
case <-w.cancelled:
return 0, errWritePreempted
default:
}
w.writeCount = 0
} else {
w.writeCount++
}
// Perform the write.
return w.writer.Write(data)
}