mirror of
https://github.com/minio/minio.git
synced 2025-04-26 21:12:28 -04: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"
|
"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.
|
// An app contains one or more servers and their associated configuration.
|
||||||
type app struct {
|
type app struct {
|
||||||
servers []*http.Server
|
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
|
// ListenAndServe will serve the given http.Servers and will monitor for signals
|
||||||
// allowing for graceful termination (SIGTERM) or restart (SIGUSR2/SIGHUP).
|
// allowing for graceful termination (SIGTERM) or restart (SIGUSR2/SIGHUP).
|
||||||
func ListenAndServe(servers ...*http.Server) error {
|
func ListenAndServe(servers ...*http.Server) error {
|
||||||
|
// get parent process id
|
||||||
|
ppid := os.Getppid()
|
||||||
|
|
||||||
a := &app{
|
a := &app{
|
||||||
servers: servers,
|
servers: servers,
|
||||||
listeners: make([]net.Listener, 0, len(servers)),
|
listeners: make([]net.Listener, 0, len(servers)),
|
||||||
@ -143,7 +141,7 @@ func ListenAndServe(servers ...*http.Server) error {
|
|||||||
a.serve()
|
a.serve()
|
||||||
|
|
||||||
// Close the parent if we inherited and it wasn't init that started us.
|
// 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 {
|
if err := syscall.Kill(ppid, syscall.SIGTERM); err != nil {
|
||||||
return iodine.New(err, nil)
|
return iodine.New(err, nil)
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,17 @@ type nimbleNet struct {
|
|||||||
inheritOnce sync.Once
|
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 {
|
func (n *nimbleNet) getInheritedListeners() error {
|
||||||
var retErr error
|
var retErr error
|
||||||
n.inheritOnce.Do(func() {
|
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
|
// Listen announces on the local network address laddr. The network net must be
|
||||||
// a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket". It
|
// a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket". It
|
||||||
// returns an inherited net.Listener for the matching network and address, or
|
// 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) {
|
func (n *nimbleNet) Listen(nett, laddr string) (net.Listener, error) {
|
||||||
switch nett {
|
switch nett {
|
||||||
default:
|
default:
|
||||||
@ -133,7 +143,8 @@ func (n *nimbleNet) ListenTCP(nett string, laddr *net.TCPAddr) (*net.TCPListener
|
|||||||
if l == nil { // we nil used inherited listeners
|
if l == nil { // we nil used inherited listeners
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if isSameAddr(l.Addr(), laddr) {
|
equal := nimbleAddr{l.Addr()}.IsEqual(laddr)
|
||||||
|
if equal {
|
||||||
n.inheritedListeners[i] = nil
|
n.inheritedListeners[i] = nil
|
||||||
n.activeListeners = append(n.activeListeners, l)
|
n.activeListeners = append(n.activeListeners, l)
|
||||||
return l.(*net.TCPListener), nil
|
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
|
if l == nil { // we nil used inherited listeners
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if isSameAddr(l.Addr(), laddr) {
|
equal := nimbleAddr{l.Addr()}.IsEqual(laddr)
|
||||||
|
if equal {
|
||||||
n.inheritedListeners[i] = nil
|
n.inheritedListeners[i] = nil
|
||||||
n.activeListeners = append(n.activeListeners, l)
|
n.activeListeners = append(n.activeListeners, l)
|
||||||
return l.(*net.UnixListener), nil
|
return l.(*net.UnixListener), nil
|
||||||
@ -190,25 +202,25 @@ func (n *nimbleNet) getActiveListeners() ([]net.Listener, error) {
|
|||||||
return ls, nil
|
return ls, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isSameAddr(a1, a2 net.Addr) bool {
|
// IsEqual is synonymous with IP.IsEqual() method, here IsEqual matches net.Addr instead of net.IP
|
||||||
if a1.Network() != a2.Network() {
|
func (n1 nimbleAddr) IsEqual(n2 net.Addr) bool {
|
||||||
|
if n1.Network() != n2.Network() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
a1s := a1.String()
|
a1h, a1p, _ := net.SplitHostPort(n1.String())
|
||||||
a2s := a2.String()
|
a2h, a2p, _ := net.SplitHostPort(n2.String())
|
||||||
if a1s == a2s {
|
// Special cases since Addr() from net.Listener will
|
||||||
|
// add frivolous [::] ipv6 for no ":[PORT]" style addresses
|
||||||
|
if a1h == "::" && a2h == "" && a1p == a2p {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
if a2h == "::" && a1h == "" && a1p == a2p {
|
||||||
// This allows for ipv6 vs ipv4 local addresses to compare as equal. This
|
return true
|
||||||
// scenario is common when listening on localhost.
|
}
|
||||||
const ipv6prefix = "[::]"
|
if net.ParseIP(a1h).Equal(net.ParseIP(a2h)) && a1p == a2p {
|
||||||
a1s = strings.TrimPrefix(a1s, ipv6prefix)
|
return true
|
||||||
a2s = strings.TrimPrefix(a2s, ipv6prefix)
|
}
|
||||||
const ipv4prefix = "0.0.0.0"
|
return false
|
||||||
a1s = strings.TrimPrefix(a1s, ipv4prefix)
|
|
||||||
a2s = strings.TrimPrefix(a2s, ipv4prefix)
|
|
||||||
return a1s == a2s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartProcess starts a new process passing it the active listeners. It
|
// 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
|
return process.Pid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type fileListener interface {
|
|
||||||
File() (*os.File, error)
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user