Skip to content

Commit 64dc012

Browse files
committed
Merge pull request #2 from bnagy/patch-1
fix goroutine leak
2 parents 7dcbde1 + e39fd75 commit 64dc012

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

shovel.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@ import (
66

77
// proxy between two sockets
88
func Shovel(local, remote io.ReadWriteCloser) error {
9-
err := make(chan error)
9+
errch := make(chan error, 1)
1010

11-
go chanCopy(err, local, remote)
12-
go chanCopy(err, remote, local)
11+
go chanCopy(errch, local, remote)
12+
go chanCopy(errch, remote, local)
1313

14-
return <-err
14+
for i := 0; i < 2; i++ {
15+
if err := <-errch; err != nil {
16+
// If this returns early the second func will push into the
17+
// buffer, and the GC will clean up
18+
return err
19+
}
20+
}
21+
return nil
1522
}
1623

1724
// copy between pipes, sending errors to channel
1825
func chanCopy(e chan error, dst, src io.ReadWriter) {
1926
_, err := io.Copy(dst, src)
20-
if err != nil {
21-
e <- err
22-
}
27+
e <- err
2328
}

0 commit comments

Comments
 (0)