Skip to content

Commit

Permalink
Merge pull request ipfs#1038 from ipfs/reuseport-disable
Browse files Browse the repository at this point in the history
reuseport: env var to turn it off
  • Loading branch information
jbenet committed Apr 8, 2015
2 parents 20dbea5 + f1566e2 commit 6036b04
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion p2p/net/conn/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (d *Dialer) rawConnDial(ctx context.Context, raddr ma.Multiaddr, remote pee
// make a copy of the manet.Dialer, we may need to change its timeout.
madialer := d.Dialer

if laddr != nil && reuseport.Available() {
if laddr != nil && reuseportIsAvailable() {
// we're perhaps going to dial twice. half the timeout, so we can afford to.
// otherwise our context would expire right after the first dial.
madialer.Dialer.Timeout = (madialer.Dialer.Timeout / 2)
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/conn/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func manetListen(addr ma.Multiaddr) (manet.Listener, error) {
return nil, err
}

if reuseport.Available() {
if reuseportIsAvailable() {
nl, err := reuseport.Listen(network, naddr)
if err == nil {
// hey, it worked!
Expand Down
35 changes: 35 additions & 0 deletions p2p/net/conn/reuseport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package conn

import (
"os"
"strings"

reuseport "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-reuseport"
)

// envReuseport is the env variable name used to turn off reuse port.
// It default to true.
const envReuseport = "IPFS_REUSEPORT"

// envReuseportVal stores the value of envReuseport. defaults to true.
var envReuseportVal = true

func init() {
v := strings.ToLower(os.Getenv(envReuseport))
if v == "false" || v == "f" || v == "0" {
envReuseportVal = false
log.Infof("REUSEPORT disabled (IPFS_REUSEPORT=%s)", v)
}
}

// reuseportIsAvailable returns whether reuseport is available to be used. This
// is here because we want to be able to turn reuseport on and off selectively.
// For now we use an ENV variable, as this handles our pressing need:
//
// IPFS_REUSEPORT=false ipfs daemon
//
// If this becomes a sought after feature, we could add this to the config.
// In the end, reuseport is a stop-gap.
func reuseportIsAvailable() bool {
return envReuseportVal && reuseport.Available()
}

0 comments on commit 6036b04

Please sign in to comment.