-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcopyctx.go
More file actions
29 lines (24 loc) · 784 Bytes
/
Copy pathcopyctx.go
File metadata and controls
29 lines (24 loc) · 784 Bytes
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
package main
import (
"context"
"io"
)
// Code below from http://ixday.github.io/post/golang-cancel-copy/
type readerFunc func(p []byte) (n int, err error)
func (rf readerFunc) Read(p []byte) (n int, err error) { return rf(p) }
// Note that we cancel on the reader; ensuring that any consumed byte is
// attempted to be written, but it also means that:
// - we reduce the granularity of the the cancellation window
// - anyway, if any of read or write blocks, it blocks cancellation, but this
// is out of scope
func CopyCtx(ctx context.Context, dst io.Writer, src io.Reader) (int64, error) {
n, err := io.Copy(dst, readerFunc(func(p []byte) (int, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
return src.Read(p)
}
}))
return n, err
}