remove recover on stream write

pull/255/merge v2.12
Darien Raymond 8 years ago
parent f1a9647c8a
commit 620561bcaf
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -40,47 +40,73 @@ func (v *directRay) InboundOutput() InputStream {
} }
type Stream struct { type Stream struct {
buffer chan *buf.Buffer buffer chan *buf.Buffer
srcClose chan bool
destClose chan bool
} }
func NewStream() *Stream { func NewStream() *Stream {
return &Stream{ return &Stream{
buffer: make(chan *buf.Buffer, bufferSize), buffer: make(chan *buf.Buffer, bufferSize),
srcClose: make(chan bool),
destClose: make(chan bool),
} }
} }
func (v *Stream) Read() (*buf.Buffer, error) { func (v *Stream) Read() (*buf.Buffer, error) {
buffer, open := <-v.buffer select {
if !open { case <-v.destClose:
return nil, io.EOF return nil, io.ErrClosedPipe
case b := <-v.buffer:
return b, nil
default:
select {
case b := <-v.buffer:
return b, nil
case <-v.srcClose:
return nil, io.EOF
}
} }
return buffer, nil
} }
func (v *Stream) Write(data *buf.Buffer) (err error) { func (v *Stream) Write(data *buf.Buffer) (err error) {
defer func() { select {
if r := recover(); r != nil { case <-v.destClose:
err = io.ErrClosedPipe return io.ErrClosedPipe
case <-v.srcClose:
return io.ErrClosedPipe
default:
select {
case <-v.destClose:
return io.ErrClosedPipe
case <-v.srcClose:
return io.ErrClosedPipe
case v.buffer <- data:
return nil
} }
}() }
v.buffer <- data
return nil
} }
func (v *Stream) Close() { func (v *Stream) Close() {
defer swallowPanic() defer swallowPanic()
close(v.buffer) close(v.srcClose)
} }
func (v *Stream) Release() { func (v *Stream) Release() {
defer swallowPanic() defer swallowPanic()
close(v.buffer) close(v.destClose)
v.Close()
for b := range v.buffer { n := len(v.buffer)
b.Release() for i := 0; i < n; i++ {
select {
case b := <-v.buffer:
b.Release()
default:
return
}
} }
} }

Loading…
Cancel
Save