Skip to content

Commit

Permalink
Attempt to fix leaking conns
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Jun 8, 2016
1 parent 3ca77ec commit c0972ef
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/github.com/getlantern/flashlight/client/chained.go
Expand Up @@ -15,9 +15,10 @@ import (
)

// Close connections idle for a period to avoid dangling connections.
// 1 hour is long enough to avoid interrupt normal connections but short enough
// to eliminate "too many open files" error.
var idleTimeout = 1 * time.Hour
// 45 seconds is long enough to avoid interrupt normal connections but shor
// shorter than the server-side timeout and short enough to eliminate
// "too many open files" error.
var idleTimeout = 45 * time.Second

// Lantern internal sites won't be used as check target.
var internalSiteSuffixes = []string{"getlantern.org", "getiantem.org", "lantern.io"}
Expand Down
10 changes: 4 additions & 6 deletions src/github.com/getlantern/flashlight/client/handler.go
Expand Up @@ -114,22 +114,20 @@ func (client *Client) intercept(resp http.ResponseWriter, req *http.Request, op
// pipeData pipes data between the client and proxy connections. It's also
// responsible for responding to the initial CONNECT request with a 200 OK.
func pipeData(clientConn net.Conn, connOut net.Conn, op *ops.Op, closeFunc func()) {
writeErrCh := make(chan error)
writeErrCh := make(chan error, 1)
// Start piping from client to proxy
op.Go(func() {
_, writeErr := io.Copy(connOut, clientConn)
if writeErr != nil {
writeErrCh <- writeErr
}
writeErrCh <- writeErr
})

// Then start copying from proxy to client.
_, readErr := io.Copy(clientConn, connOut)
writeErr := <-writeErrCh
if readErr != nil {
op.FailIf(log.Errorf("Error piping data from proxy to client: %v", readErr))
log.Tracef("Error piping data from proxy to client: %v", readErr)
} else if writeErr != nil {
log.Error(errors.New("Error piping data from client to proxy: %v", writeErr))
log.Tracef("Error piping data from client to proxy: %v", writeErr)
}

closeFunc()
Expand Down
2 changes: 2 additions & 0 deletions src/github.com/getlantern/flashlight/client/reverseproxy.go
Expand Up @@ -18,7 +18,9 @@ import (
func (client *Client) newReverseProxy(bal *balancer.Balancer) *httputil.ReverseProxy {
transport := &http.Transport{
TLSHandshakeTimeout: 40 * time.Second,
MaxIdleTime: 30 * time.Second,
}
transport.EnforceMaxIdleTime()

// TODO: would be good to make this sensitive to QOS, which
// right now is only respected for HTTPS connections. The
Expand Down
4 changes: 4 additions & 0 deletions src/github.com/getlantern/flashlight/proxied/proxied.go
Expand Up @@ -395,6 +395,10 @@ func chained(rootCA string, persistent bool) (http.RoundTripper, error) {
}
tr.TLSClientConfig.RootCAs = caCert.PoolContainingCert()
}
if persistent {
tr.MaxIdleTime = 30 * time.Second
tr.EnforceMaxIdleTime()
}

tr.Proxy = func(req *http.Request) (*url.URL, error) {
proxyAddr, ok := getProxyAddr()
Expand Down

0 comments on commit c0972ef

Please sign in to comment.