Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker-proxy-close-dangling-sockets #2465

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions cmd/proxy/tcp_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io"
"log"
"net"
"sync"
"time"
)

// TCPProxy is a proxy for TCP connections. It implements the Proxy interface to
Expand Down Expand Up @@ -38,21 +38,36 @@ func (proxy *TCPProxy) clientLoop(client *net.TCPConn, quit chan bool) {
return
}

var wg sync.WaitGroup
// Use this channel to follow the execution status
// of our goroutines :D
done := make(chan bool)

var broker = func(to, from *net.TCPConn) {
io.Copy(to, from)
from.CloseRead()
to.CloseWrite()
wg.Done()
done <- true

}

wg.Add(2)
go broker(client, backend)
go broker(backend, client)

finish := make(chan struct{})
go func() {
wg.Wait()

//Wait until at least one function exits
<-done

//After one end of the connection is closed wait for max 30 sec
//until the other end is closed as well
//This is to prevent that CLOSE_WAIT and SYN_WAIT2 socket states
//accumulate the open sockets by docker-proxy
select {
case <-done:
case <-time.After(30 * time.Second):
}

close(finish)
}()

Expand Down