mirror of
https://github.com/criyle/go-sandbox.git
synced 2025-11-04 14:49:53 +08:00
Do not close read pipe after limit
This commit is contained in:
parent
687b9bf6f9
commit
744db3ff88
@ -28,9 +28,11 @@ func NewPipe(writer io.Writer, n int64) (<-chan struct{}, *os.File, error) {
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
defer r.Close()
|
||||
io.CopyN(writer, r, int64(n))
|
||||
close(done)
|
||||
// ensure no blocking / SIGPIPE on the other end
|
||||
discardRead(r)
|
||||
r.Close()
|
||||
}()
|
||||
return done, w, nil
|
||||
}
|
||||
|
||||
19
pkg/pipe/discard.go
Normal file
19
pkg/pipe/discard.go
Normal file
@ -0,0 +1,19 @@
|
||||
package pipe
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var _ io.Writer = discardWriter{}
|
||||
|
||||
type discardWriter struct {
|
||||
}
|
||||
|
||||
func (w discardWriter) Write(b []byte) (int, error) {
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func discardRead(f *os.File) (int64, error) {
|
||||
return io.Copy(discardWriter{}, f)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user