Skip to content

Commit

Permalink
custom dialTimeout to sort of avoid ephemeral ports limits
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasen committed Dec 9, 2015
1 parent 2cbc5ad commit e7aca2f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ func cacheBackendAddr(key string, val []byte) {
_BackendAddrCache.Store(m2) // atomically replace the current object with the new one
}

func dialTimeout(network, address string, timeout time.Duration) (conn net.Conn, err error) {
m := int(timeout / time.Second)
for i := 0; i < m; i++ {
conn, err = net.DialTimeout(network, address, timeout)
if err == nil || !strings.HasSuffix(err.Error(), "can't assign requested address") {
break
}
time.Sleep(time.Second)
}
return
}

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
os.Setenv("GOTRACEBACK", "crash")
Expand Down Expand Up @@ -294,7 +306,7 @@ func handleConn(c net.Conn) {
// TODO: check if addr is allowed

// Build tunnel
backend, err := net.DialTimeout("tcp", string(addr), time.Second*time.Duration(_BackendDialTimeout))
backend, err := dialTimeout("tcp", string(addr), time.Second*time.Duration(_BackendDialTimeout))
if err != nil {
// handle error
switch err := err.(type) {
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func testProtocol(cipherAddr, expected []byte) {
if *reuseTest {
conn, err = reuseport.Dial("tcp", "127.0.0.1:0", _defaultFrontdAddr)
} else {
conn, err = net.Dial("tcp", _defaultFrontdAddr)
conn, err = dialTimeout("tcp", _defaultFrontdAddr, time.Second*time.Duration(_BackendDialTimeout))
}

if err != nil {
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestEchoServer(t *testing.T) {
if *reuseTest {
conn, err = reuseport.Dial("tcp", "127.0.0.1:0", string(_echoServerAddr))
} else {
conn, err = net.Dial("tcp", string(_echoServerAddr))
conn, err = dialTimeout("tcp", string(_echoServerAddr), time.Second*time.Duration(_BackendDialTimeout))
}
if err != nil {
panic(err)
Expand Down

0 comments on commit e7aca2f

Please sign in to comment.