Guarantee of closing sockets after context is done

This commit is contained in:
9seconds
2018-07-28 15:36:40 +03:00
parent 243a89a68c
commit 1ee34fd502
+7
View File
@@ -47,6 +47,7 @@ func (p *Proxy) accept(conn net.Conn) {
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
conn.Close() // nolint: errcheck
if err := recover(); err != nil {
@@ -74,6 +75,12 @@ func (p *Proxy) accept(conn net.Conn) {
}
defer serverConn.(io.Closer).Close() // nolint: errcheck
go func() {
<-ctx.Done()
serverConn.(io.Closer).Close()
clientConn.(io.Closer).Close()
}()
wait := &sync.WaitGroup{}
wait.Add(2)