1
// Copyright 2014 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
13
// pipe is a goroutine-safe io.Reader/io.Writer pair. It's like
14
// io.Pipe except there are no PipeReader/PipeWriter halves, and the
15
// underlying buffer is an interface. (io.Pipe is always unbuffered)
18
c sync.Cond // c.L lazily initialized to &p.mu
19
b pipeBuffer // nil when done reading
20
unread int // bytes unread when done
21
err error // read error once empty. non-nil means closed.
22
breakErr error // immediate read error (caller doesn't see rest of b)
23
donec chan struct{} // closed on error
24
readFn func() // optional code to run in Read before error
27
type pipeBuffer interface {
33
// setBuffer initializes the pipe buffer.
34
// It has no effect if the pipe is already closed.
35
func (p *pipe) setBuffer(b pipeBuffer) {
38
if p.err != nil || p.breakErr != nil {
44
func (p *pipe) Len() int {
53
// Read waits until data is available and copies bytes
54
// from the buffer into p.
55
func (p *pipe) Read(d []byte) (n int, err error) {
62
if p.breakErr != nil {
65
if p.b != nil && p.b.Len() > 0 {
70
p.readFn() // e.g. copy trailers
71
p.readFn = nil // not sticky like p.err
80
var errClosedPipeWrite = errors.New("write on closed buffer")
82
// Write copies bytes from p into the buffer and wakes a reader.
83
// It is an error to write more data than the buffer can hold.
84
func (p *pipe) Write(d []byte) (n int, err error) {
92
return 0, errClosedPipeWrite
94
if p.breakErr != nil {
96
return len(d), nil // discard when there is no reader
101
// CloseWithError causes the next Read (waking up a current blocked
102
// Read if needed) to return the provided err after all data has been
105
// The error must be non-nil.
106
func (p *pipe) CloseWithError(err error) { p.closeWithError(&p.err, err, nil) }
108
// BreakWithError causes the next Read (waking up a current blocked
109
// Read if needed) to return the provided err immediately, without
110
// waiting for unread data.
111
func (p *pipe) BreakWithError(err error) { p.closeWithError(&p.breakErr, err, nil) }
113
// closeWithErrorAndCode is like CloseWithError but also sets some code to run
114
// in the caller's goroutine before returning the error.
115
func (p *pipe) closeWithErrorAndCode(err error, fn func()) { p.closeWithError(&p.err, err, fn) }
117
func (p *pipe) closeWithError(dst *error, err error, fn func()) {
119
panic("err must be non-nil")
128
// Already been done.
132
if dst == &p.breakErr {
134
p.unread += p.b.Len()
142
// requires p.mu be held.
143
func (p *pipe) closeDoneLocked() {
147
// Close if unclosed. This isn't racy since we always
148
// hold p.mu while closing.
156
// Err returns the error (if any) first set by BreakWithError or CloseWithError.
157
func (p *pipe) Err() error {
160
if p.breakErr != nil {
166
// Done returns a channel which is closed if and when this pipe is closed
167
// with CloseWithError.
168
func (p *pipe) Done() <-chan struct{} {
172
p.donec = make(chan struct{})
173
if p.err != nil || p.breakErr != nil {
174
// Already hit an error.