Merge pull request #715 from harshavardhana/pr_out_add_net_addr_wrapper_with_isequal_and_use_it

master
Harshavardhana 9 years ago
commit aea83a5fe4
  1. 8
      pkg/donut/disk/disk.go
  2. 10
      pkg/server/nimble/http.go
  3. 54
      pkg/server/nimble/net.go
  4. 7
      pkg/utils/atomic/atomic.go

@ -153,13 +153,7 @@ func (disk Disk) CreateFile(filename string) (*atomic.File, error) {
return nil, iodine.New(InvalidArgument{}, nil)
}
filePath := filepath.Join(disk.path, filename)
// Create directories if they don't exist
if err := os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
return nil, iodine.New(err, nil)
}
f, err := atomic.FileCreate(filePath)
f, err := atomic.FileCreate(filepath.Join(disk.path, filename))
if err != nil {
return nil, iodine.New(err, nil)
}

@ -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)
}

@ -58,8 +58,13 @@ func (f *File) CloseAndPurge() error {
return nil
}
// FileCreate creates a new file at filePath for atomic writes
// FileCreate creates a new file at filePath for atomic writes, it also creates parent directories if they don't exist
func FileCreate(filePath string) (*File, error) {
// if parent directories do not exist, ioutil.TempFile doesn't create them
// handle such a case with os.MkdirAll()
if err := os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
return nil, iodine.New(err, nil)
}
f, err := ioutil.TempFile(filepath.Dir(filePath), filepath.Base(filePath))
if err != nil {
return nil, iodine.New(err, nil)

Loading…
Cancel
Save