mirror of
https://github.com/minio/minio.git
synced 2024-12-24 22:25:54 -05:00
Add net.Addr wrapper with IsEqual() and use it.
This commit is contained in:
parent
11b893804c
commit
317096a0c4
@ -35,11 +35,6 @@ import (
|
||||
"github.com/minio/minio/pkg/iodine"
|
||||
)
|
||||
|
||||
var (
|
||||
inheritedListeners = os.Getenv("LISTEN_FDS")
|
||||
ppid = os.Getppid()
|
||||
)
|
||||
|
||||
// An app contains one or more servers and their associated configuration.
|
||||
type app struct {
|
||||
servers []*http.Server
|
||||
@ -126,6 +121,9 @@ func (a *app) trapSignal(wg *sync.WaitGroup) {
|
||||
// ListenAndServe will serve the given http.Servers and will monitor for signals
|
||||
// allowing for graceful termination (SIGTERM) or restart (SIGUSR2/SIGHUP).
|
||||
func ListenAndServe(servers ...*http.Server) error {
|
||||
// get parent process id
|
||||
ppid := os.Getppid()
|
||||
|
||||
a := &app{
|
||||
servers: servers,
|
||||
listeners: make([]net.Listener, 0, len(servers)),
|
||||
@ -143,7 +141,7 @@ func ListenAndServe(servers ...*http.Server) error {
|
||||
a.serve()
|
||||
|
||||
// Close the parent if we inherited and it wasn't init that started us.
|
||||
if inheritedListeners != "" && ppid != 1 {
|
||||
if os.Getenv("LISTEN_FDS") != "" && ppid != 1 {
|
||||
if err := syscall.Kill(ppid, syscall.SIGTERM); err != nil {
|
||||
return iodine.New(err, nil)
|
||||
}
|
||||
|
@ -59,7 +59,17 @@ type nimbleNet struct {
|
||||
inheritOnce sync.Once
|
||||
}
|
||||
|
||||
// inherit - lookg for LISTEN_FDS in environment variables and populate listeners
|
||||
// nimbleAddr simple wrapper over net.Addr interface to implement IsEqual()
|
||||
type nimbleAddr struct {
|
||||
net.Addr
|
||||
}
|
||||
|
||||
// fileListener simple interface to extract file pointers from different types of net.Listener's
|
||||
type fileListener interface {
|
||||
File() (*os.File, error)
|
||||
}
|
||||
|
||||
// getInheritedListeners - look for LISTEN_FDS in environment variables and populate listeners accordingly
|
||||
func (n *nimbleNet) getInheritedListeners() error {
|
||||
var retErr error
|
||||
n.inheritOnce.Do(func() {
|
||||
@ -97,7 +107,7 @@ func (n *nimbleNet) getInheritedListeners() error {
|
||||
// Listen announces on the local network address laddr. The network net must be
|
||||
// a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket". It
|
||||
// returns an inherited net.Listener for the matching network and address, or
|
||||
// creates a new one using net.Listen.
|
||||
// creates a new one using net.Listen()
|
||||
func (n *nimbleNet) Listen(nett, laddr string) (net.Listener, error) {
|
||||
switch nett {
|
||||
default:
|
||||
@ -133,7 +143,8 @@ func (n *nimbleNet) ListenTCP(nett string, laddr *net.TCPAddr) (*net.TCPListener
|
||||
if l == nil { // we nil used inherited listeners
|
||||
continue
|
||||
}
|
||||
if isSameAddr(l.Addr(), laddr) {
|
||||
equal := nimbleAddr{l.Addr()}.IsEqual(laddr)
|
||||
if equal {
|
||||
n.inheritedListeners[i] = nil
|
||||
n.activeListeners = append(n.activeListeners, l)
|
||||
return l.(*net.TCPListener), nil
|
||||
@ -165,7 +176,8 @@ func (n *nimbleNet) ListenUnix(nett string, laddr *net.UnixAddr) (*net.UnixListe
|
||||
if l == nil { // we nil used inherited listeners
|
||||
continue
|
||||
}
|
||||
if isSameAddr(l.Addr(), laddr) {
|
||||
equal := nimbleAddr{l.Addr()}.IsEqual(laddr)
|
||||
if equal {
|
||||
n.inheritedListeners[i] = nil
|
||||
n.activeListeners = append(n.activeListeners, l)
|
||||
return l.(*net.UnixListener), nil
|
||||
@ -190,25 +202,25 @@ func (n *nimbleNet) getActiveListeners() ([]net.Listener, error) {
|
||||
return ls, nil
|
||||
}
|
||||
|
||||
func isSameAddr(a1, a2 net.Addr) bool {
|
||||
if a1.Network() != a2.Network() {
|
||||
// IsEqual is synonymous with IP.IsEqual() method, here IsEqual matches net.Addr instead of net.IP
|
||||
func (n1 nimbleAddr) IsEqual(n2 net.Addr) bool {
|
||||
if n1.Network() != n2.Network() {
|
||||
return false
|
||||
}
|
||||
a1s := a1.String()
|
||||
a2s := a2.String()
|
||||
if a1s == a2s {
|
||||
a1h, a1p, _ := net.SplitHostPort(n1.String())
|
||||
a2h, a2p, _ := net.SplitHostPort(n2.String())
|
||||
// Special cases since Addr() from net.Listener will
|
||||
// add frivolous [::] ipv6 for no ":[PORT]" style addresses
|
||||
if a1h == "::" && a2h == "" && a1p == a2p {
|
||||
return true
|
||||
}
|
||||
|
||||
// This allows for ipv6 vs ipv4 local addresses to compare as equal. This
|
||||
// scenario is common when listening on localhost.
|
||||
const ipv6prefix = "[::]"
|
||||
a1s = strings.TrimPrefix(a1s, ipv6prefix)
|
||||
a2s = strings.TrimPrefix(a2s, ipv6prefix)
|
||||
const ipv4prefix = "0.0.0.0"
|
||||
a1s = strings.TrimPrefix(a1s, ipv4prefix)
|
||||
a2s = strings.TrimPrefix(a2s, ipv4prefix)
|
||||
return a1s == a2s
|
||||
if a2h == "::" && a1h == "" && a1p == a2p {
|
||||
return true
|
||||
}
|
||||
if net.ParseIP(a1h).Equal(net.ParseIP(a2h)) && a1p == a2p {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StartProcess starts a new process passing it the active listeners. It
|
||||
@ -259,7 +271,3 @@ func (n *nimbleNet) StartProcess() (int, error) {
|
||||
}
|
||||
return process.Pid, nil
|
||||
}
|
||||
|
||||
type fileListener interface {
|
||||
File() (*os.File, error)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user