2018-07-04 10:51:47 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
"github.com/libp2p/go-reuseport"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// envReuseport is the env variable name used to turn off reuse port.
|
|
|
|
// It default to true.
|
2019-06-09 07:24:20 +00:00
|
|
|
const envReuseport = "LIBP2P_TCP_REUSEPORT"
|
|
|
|
const deprecatedEnvReuseport = "IPFS_REUSEPORT"
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
// 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
|
2019-06-09 07:24:20 +00:00
|
|
|
log.Infof("REUSEPORT disabled (LIBP2P_TCP_REUSEPORT=%s)", v)
|
|
|
|
}
|
|
|
|
v, exist := os.LookupEnv(deprecatedEnvReuseport)
|
|
|
|
if exist {
|
|
|
|
log.Warning("IPFS_REUSEPORT is deprecated, use LIBP2P_TCP_REUSEPORT instead")
|
|
|
|
if v == "false" || v == "f" || v == "0" {
|
|
|
|
envReuseportVal = false
|
|
|
|
log.Infof("REUSEPORT disabled (IPFS_REUSEPORT=%s)", v)
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
//
|
2019-06-09 07:24:20 +00:00
|
|
|
// LIBP2P_TCP_REUSEPORT=false ipfs daemon
|
2018-07-04 10:51:47 +00:00
|
|
|
//
|
|
|
|
// 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()
|
|
|
|
}
|