10 // getWithPipe invokes getter and copies the resulting data into
11 // buf. If ctx is done before all data is copied, getWithPipe closes
12 // the pipe with an error, and returns early with an error.
13 func getWithPipe(ctx context.Context, loc string, buf []byte, br BlockReader) (int, error) {
14 piper, pipew := io.Pipe()
16 pipew.CloseWithError(br.ReadBlock(ctx, loc, pipew))
18 done := make(chan struct{})
22 size, err = io.ReadFull(piper, buf)
23 if err == io.EOF || err == io.ErrUnexpectedEOF {
30 piper.CloseWithError(ctx.Err())
38 // putWithPipe invokes putter with a new pipe, and and copies data
39 // from buf into the pipe. If ctx is done before all data is copied,
40 // putWithPipe closes the pipe with an error, and returns early with
42 func putWithPipe(ctx context.Context, loc string, buf []byte, bw BlockWriter) error {
43 piper, pipew := io.Pipe()
44 copyErr := make(chan error)
46 _, err := io.Copy(pipew, bytes.NewReader(buf))
51 putErr := make(chan error, 1)
53 putErr <- bw.WriteBlock(ctx, loc, piper)
65 // Ensure io.Copy goroutine isn't blocked writing to pipew
66 // (otherwise, io.Copy is still using buf so it isn't safe to
67 // return). This can cause pipew to receive corrupt data if
68 // err came from copyErr or ctx.Done() before the copy
69 // finished. That's OK, though: in that case err != nil, and
70 // CloseWithErr(err) ensures putter() will get an error from
71 // piper.Read() before seeing EOF.
72 go pipew.CloseWithError(err)
73 go io.Copy(ioutil.Discard, piper)
76 // Note: io.Copy() is finished now, but putter() might still
77 // be running. If we encounter an error before putter()
78 // returns, we return right away without waiting for putter().