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

Fix leaking FD #1314

Merged
merged 1 commit into from
Sep 16, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions pkg/proxy/proxier.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,21 @@ func (tcp *tcpProxySocket) ProxyLoop(service string, proxier *Proxier) {
continue
}
// Spin up an async copy loop.
proxyTCP(inConn.(*net.TCPConn), outConn.(*net.TCPConn))
go proxyTCP(inConn.(*net.TCPConn), outConn.(*net.TCPConn))
}
}

// proxyTCP proxies data bi-directionally between in and out.
func proxyTCP(in, out *net.TCPConn) {
var wg sync.WaitGroup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewers, @ddysher: Don't forget to consider, what happens if this crashes? Should probably have a "defer util.HandleCrash()" here.

wg.Add(2)
glog.Infof("Creating proxy between %v <-> %v <-> %v <-> %v",
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
go copyBytes(in, out)
go copyBytes(out, in)
go copyBytes(in, out, &wg)
go copyBytes(out, in, &wg)
wg.Wait()
in.Close()
out.Close()
}

// udpProxySocket implements proxySocket. Close() is implemented by net.UDPConn. When Close() is called,
Expand Down Expand Up @@ -302,7 +307,8 @@ func NewProxier(loadBalancer LoadBalancer, address string) *Proxier {
}
}

func copyBytes(in, out *net.TCPConn) {
func copyBytes(in, out *net.TCPConn, wg *sync.WaitGroup) {
defer wg.Done()
glog.Infof("Copying from %v <-> %v <-> %v <-> %v",
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
if _, err := io.Copy(in, out); err != nil {
Expand Down