reuseport: env var to turn it off
reuseport is a hack. It is necessary for us to do certain kinds of tcp nat traversal. Ideally, reuseport would be available in go: https://github.com/golang/go/issues/9661 But until that issue is fixed, we're stuck with this. In some cases, reuseport is strictly a detriment: nodes are not NATed. This commit introduces an ENV var IPFS_REUSEPORT that can be set to false to avoid using reuseport entirely: IPFS_REUSEPORT=false ipfs daemon This approach addresses our current need. It could become a config var if necessary. If reuseport continues to give problems, we should look into improving it.
Juan Batiz-Benet committed
Apr 8, 2015 at 00:04 UTC
f1566e232723d14b042a88be82234150ee8104c0
3 files changed
+37
-2
p2p/net/conn/dial.go
+1
-1
@@ -118,7 +118,7 @@ func (d *Dialer) rawConnDial(ctx context.Context, raddr ma.Multiaddr, remote pee
118
// make a copy of the manet.Dialer, we may need to change its timeout.
119
madialer := d.Dialer
120
121
- if laddr != nil && reuseport.Available() {
121
+ if laddr != nil && reuseportIsAvailable() {
122
// we're perhaps going to dial twice. half the timeout, so we can afford to.
123
// otherwise our context would expire right after the first dial.
124
madialer.Dialer.Timeout = (madialer.Dialer.Timeout / 2)
p2p/net/conn/listen.go
+1
-1
@@ -169,7 +169,7 @@ func manetListen(addr ma.Multiaddr) (manet.Listener, error) {
169
return nil, err
170
}
171
172
- if reuseport.Available() {
172
+ if reuseportIsAvailable() {
173
nl, err := reuseport.Listen(network, naddr)
174
if err == nil {
175
// hey, it worked!
p2p/net/conn/reuseport.go
new
+35
@@ -0,0 +1,35 @@
1
+package conn
2
+
3
+import (
4
+ "os"
5
+ "strings"
6
+
7
+ reuseport "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-reuseport"
8
+)
9
+
10
+// envReuseport is the env variable name used to turn off reuse port.
11
+// It default to true.
12
+const envReuseport = "IPFS_REUSEPORT"
13
+
14
+// envReuseportVal stores the value of envReuseport. defaults to true.
15
+var envReuseportVal = true
16
+
17
+func init() {
18
+ v := strings.ToLower(os.Getenv(envReuseport))
19
+ if v == "false" || v == "f" || v == "0" {
20
+ envReuseportVal = false
21
+ log.Infof("REUSEPORT disabled (IPFS_REUSEPORT=%s)", v)
22
+ }
23
+}
24
+
25
+// reuseportIsAvailable returns whether reuseport is available to be used. This
26
+// is here because we want to be able to turn reuseport on and off selectively.
27
+// For now we use an ENV variable, as this handles our pressing need:
28
+//
29
+// IPFS_REUSEPORT=false ipfs daemon
30
+//
31
+// If this becomes a sought after feature, we could add this to the config.
32
+// In the end, reuseport is a stop-gap.
33
+func reuseportIsAvailable() bool {
34
+ return envReuseportVal && reuseport.Available()
35
+}