Skip to content

Commit

Permalink
Merge pull request #3 from paskal/CancelableReaderExit
Browse files Browse the repository at this point in the history
Shut down CancelableReader.begin() on context exit
  • Loading branch information
quenbyako authored Jun 21, 2021
2 parents 22c31ca + 99c3b87 commit 21c7782
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions ioutil/cancel_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ type CancelableReader struct {

func (c *CancelableReader) begin() {
for {
sizewant := <-c.sizeWant
buf := make([]byte, sizewant)
// readFull, cause some readers like net.TCPConn returns size smaller than buf size
n, err := io.ReadFull(c.r, buf)
if err != nil {
c.err = err
select {
case sizeWant := <-c.sizeWant:
buf := make([]byte, sizeWant)
// readFull, cause some readers like net.TCPConn returns size smaller than buf size
n, err := io.ReadFull(c.r, buf)
if err != nil {
c.err = err
close(c.data)
return
}
if n != sizeWant {
panic("read " + strconv.Itoa(n) + ", want " + strconv.Itoa(sizeWant))
}
c.data <- buf
case <-c.ctx.Done():
close(c.data)
close(c.sizeWant)
return
}
if n != sizewant {
panic("read " + strconv.Itoa(n) + ", want " + strconv.Itoa(sizewant))
}
c.data <- buf
}
}

Expand Down

0 comments on commit 21c7782

Please sign in to comment.