Migrate from iodine to probe

This commit is contained in:
Harshavardhana
2015-08-03 16:17:21 -07:00
parent 7f13095260
commit d09fd8b0a1
38 changed files with 917 additions and 1339 deletions

View File

@@ -32,7 +32,7 @@ import (
"time"
"github.com/facebookgo/httpdown"
"github.com/minio/minio/pkg/iodine"
"github.com/minio/minio/pkg/probe"
)
// An app contains one or more servers and their associated configuration.
@@ -45,11 +45,11 @@ type app struct {
}
// listen initailize listeners
func (a *app) listen() error {
func (a *app) listen() *probe.Error {
for _, s := range a.servers {
l, err := a.net.Listen("tcp", s.Addr)
if err != nil {
return iodine.New(err, nil)
return probe.New(err)
}
if s.TLSConfig != nil {
l = tls.NewListener(l, s.TLSConfig)
@@ -79,7 +79,7 @@ func (a *app) wait() {
go func(s httpdown.Server) {
defer wg.Done()
if err := s.Wait(); err != nil {
a.errors <- iodine.New(err, nil)
a.errors <- probe.New(err)
}
}(s)
}
@@ -101,7 +101,7 @@ func (a *app) trapSignal(wg *sync.WaitGroup) {
go func(s httpdown.Server) {
defer wg.Done()
if err := s.Stop(); err != nil {
a.errors <- iodine.New(err, nil)
a.errors <- probe.New(err)
}
}(s)
}
@@ -112,7 +112,7 @@ func (a *app) trapSignal(wg *sync.WaitGroup) {
// we only return here if there's an error, otherwise the new process
// will send us a TERM when it's ready to trigger the actual shutdown.
if _, err := a.net.StartProcess(); err != nil {
a.errors <- iodine.New(err, nil)
a.errors <- probe.New(err)
}
}
}
@@ -120,7 +120,7 @@ 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 {
func ListenAndServe(servers ...*http.Server) *probe.Error {
// get parent process id
ppid := os.Getppid()
@@ -134,7 +134,7 @@ func ListenAndServe(servers ...*http.Server) error {
// Acquire Listeners
if err := a.listen(); err != nil {
return iodine.New(err, nil)
return err.Trace()
}
// Start serving.
@@ -143,7 +143,7 @@ func ListenAndServe(servers ...*http.Server) error {
// Close the parent if we inherited and it wasn't init that started us.
if os.Getenv("LISTEN_FDS") != "" && ppid != 1 {
if err := syscall.Kill(ppid, syscall.SIGTERM); err != nil {
return iodine.New(err, nil)
return probe.New(err)
}
}
@@ -160,14 +160,14 @@ func ListenAndServe(servers ...*http.Server) error {
if err == nil {
panic("unexpected nil error")
}
return iodine.New(err, nil)
return probe.New(err)
case <-waitdone:
return nil
}
}
// ListenAndServeLimited is similar to ListenAndServe but ratelimited with connLimit value
func ListenAndServeLimited(connLimit int, servers ...*http.Server) error {
func ListenAndServeLimited(connLimit int, servers ...*http.Server) *probe.Error {
// get parent process id
ppid := os.Getppid()
@@ -181,7 +181,7 @@ func ListenAndServeLimited(connLimit int, servers ...*http.Server) error {
// Acquire Listeners
if err := a.listen(); err != nil {
return iodine.New(err, nil)
return err.Trace()
}
// Start serving.
@@ -190,7 +190,7 @@ func ListenAndServeLimited(connLimit int, servers ...*http.Server) error {
// Close the parent if we inherited and it wasn't init that started us.
if os.Getenv("LISTEN_FDS") != "" && ppid != 1 {
if err := syscall.Kill(ppid, syscall.SIGTERM); err != nil {
return iodine.New(err, nil)
return probe.New(err)
}
}
@@ -207,7 +207,7 @@ func ListenAndServeLimited(connLimit int, servers ...*http.Server) error {
if err == nil {
panic("unexpected nil error")
}
return iodine.New(err, nil)
return probe.New(err)
case <-waitdone:
return nil
}

View File

@@ -20,8 +20,6 @@ import (
"net"
"os"
"sync"
"github.com/minio/minio/pkg/iodine"
)
// rateLimitedListener returns a Listener that accepts at most n simultaneous
@@ -53,7 +51,7 @@ func (l *rateLimitListener) Accept() (net.Conn, error) {
c, err := l.Listener.Accept()
if err != nil {
l.release()
return nil, iodine.New(err, nil)
return nil, err
}
return &rateLimitListenerConn{Conn: c, release: l.release}, nil
}
@@ -67,5 +65,5 @@ type rateLimitListenerConn struct {
func (l *rateLimitListenerConn) Close() error {
err := l.Conn.Close()
l.releaseOnce.Do(l.release)
return iodine.New(err, nil)
return err
}

View File

@@ -25,7 +25,7 @@ import (
"strings"
"sync"
"github.com/minio/minio/pkg/iodine"
"github.com/minio/minio/pkg/probe"
)
// This package is a fork https://github.com/facebookgo/grace
@@ -71,8 +71,8 @@ type fileListener interface {
}
// getInheritedListeners - look for LISTEN_FDS in environment variables and populate listeners accordingly
func (n *minNet) getInheritedListeners() error {
var retErr error
func (n *minNet) getInheritedListeners() *probe.Error {
var retErr *probe.Error
n.inheritOnce.Do(func() {
n.mutex.Lock()
defer n.mutex.Unlock()
@@ -82,7 +82,7 @@ func (n *minNet) getInheritedListeners() error {
}
count, err := strconv.Atoi(countStr)
if err != nil {
retErr = fmt.Errorf("found invalid count value: %s=%s", envCountKey, countStr)
retErr = probe.New(fmt.Errorf("found invalid count value: %s=%s", envCountKey, countStr))
return
}
@@ -92,37 +92,40 @@ func (n *minNet) getInheritedListeners() error {
l, err := net.FileListener(file)
if err != nil {
file.Close()
retErr = iodine.New(fmt.Errorf("error inheriting socket fd %d: %s", i, err), nil)
retErr = probe.New(err)
return
}
if err := file.Close(); err != nil {
retErr = iodine.New(fmt.Errorf("error closing inherited socket fd %d: %s", i, err), nil)
retErr = probe.New(err)
return
}
n.inheritedListeners = append(n.inheritedListeners, l)
}
})
return iodine.New(retErr, nil)
if retErr != nil {
return probe.New(retErr)
}
return nil
}
// 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()
func (n *minNet) Listen(nett, laddr string) (net.Listener, error) {
func (n *minNet) Listen(nett, laddr string) (net.Listener, *probe.Error) {
switch nett {
default:
return nil, net.UnknownNetworkError(nett)
return nil, probe.New(net.UnknownNetworkError(nett))
case "tcp", "tcp4", "tcp6":
addr, err := net.ResolveTCPAddr(nett, laddr)
if err != nil {
return nil, iodine.New(err, nil)
return nil, probe.New(err)
}
return n.ListenTCP(nett, addr)
case "unix", "unixpacket":
addr, err := net.ResolveUnixAddr(nett, laddr)
if err != nil {
return nil, iodine.New(err, nil)
return nil, probe.New(err)
}
return n.ListenUnix(nett, addr)
}
@@ -131,10 +134,9 @@ func (n *minNet) Listen(nett, laddr string) (net.Listener, error) {
// ListenTCP announces on the local network address laddr. The network net must
// be: "tcp", "tcp4" or "tcp6". It returns an inherited net.Listener for the
// matching network and address, or creates a new one using net.ListenTCP.
func (n *minNet) ListenTCP(nett string, laddr *net.TCPAddr) (net.Listener, error) {
var err error
func (n *minNet) ListenTCP(nett string, laddr *net.TCPAddr) (net.Listener, *probe.Error) {
if err := n.getInheritedListeners(); err != nil {
return nil, iodine.New(err, nil)
return nil, err.Trace()
}
n.mutex.Lock()
@@ -153,11 +155,10 @@ func (n *minNet) ListenTCP(nett string, laddr *net.TCPAddr) (net.Listener, error
}
}
var l net.Listener
// make a fresh listener
l, err = net.ListenTCP(nett, laddr)
l, err := net.ListenTCP(nett, laddr)
if err != nil {
return nil, iodine.New(err, nil)
return nil, probe.New(err)
}
n.activeListeners = append(n.activeListeners, rateLimitedListener(l, n.connLimit))
return l, nil
@@ -166,10 +167,9 @@ func (n *minNet) ListenTCP(nett string, laddr *net.TCPAddr) (net.Listener, error
// ListenUnix announces on the local network address laddr. The network net
// must be a: "unix" or "unixpacket". It returns an inherited net.Listener for
// the matching network and address, or creates a new one using net.ListenUnix.
func (n *minNet) ListenUnix(nett string, laddr *net.UnixAddr) (net.Listener, error) {
var err error
func (n *minNet) ListenUnix(nett string, laddr *net.UnixAddr) (net.Listener, *probe.Error) {
if err := n.getInheritedListeners(); err != nil {
return nil, iodine.New(err, nil)
return nil, err.Trace()
}
n.mutex.Lock()
@@ -188,23 +188,22 @@ func (n *minNet) ListenUnix(nett string, laddr *net.UnixAddr) (net.Listener, err
}
}
var l net.Listener
// make a fresh listener
l, err = net.ListenUnix(nett, laddr)
l, err := net.ListenUnix(nett, laddr)
if err != nil {
return nil, iodine.New(err, nil)
return nil, probe.New(err)
}
n.activeListeners = append(n.activeListeners, rateLimitedListener(l, n.connLimit))
return l, nil
}
// activeListeners returns a snapshot copy of the active listeners.
func (n *minNet) getActiveListeners() ([]net.Listener, error) {
func (n *minNet) getActiveListeners() []net.Listener {
n.mutex.Lock()
defer n.mutex.Unlock()
ls := make([]net.Listener, len(n.activeListeners))
copy(ls, n.activeListeners)
return ls, nil
return ls
}
// IsEqual is synonymous with IP.IsEqual() method, here IsEqual matches net.Addr instead of net.IP
@@ -233,18 +232,15 @@ func (n1 minAddr) IsEqual(n2 net.Addr) bool {
// arguments as when it was originally started. This allows for a newly
// deployed binary to be started. It returns the pid of the newly started
// process when successful.
func (n *minNet) StartProcess() (int, error) {
listeners, err := n.getActiveListeners()
if err != nil {
return 0, iodine.New(err, nil)
}
func (n *minNet) StartProcess() (int, *probe.Error) {
listeners := n.getActiveListeners()
// Extract the fds from the listeners.
files := make([]*os.File, len(listeners))
for i, l := range listeners {
var err error
files[i], err = l.(fileListener).File()
if err != nil {
return 0, iodine.New(err, nil)
return 0, probe.New(err)
}
defer files[i].Close()
}
@@ -253,7 +249,7 @@ func (n *minNet) StartProcess() (int, error) {
// the file it points to has been changed we will use the updated symlink.
argv0, err := exec.LookPath(os.Args[0])
if err != nil {
return 0, iodine.New(err, nil)
return 0, probe.New(err)
}
// Pass on the environment and replace the old count key with the new one.
@@ -272,7 +268,7 @@ func (n *minNet) StartProcess() (int, error) {
Files: allFiles,
})
if err != nil {
return 0, iodine.New(err, nil)
return 0, probe.New(err)
}
return process.Pid, nil
}

View File

@@ -1,78 +0,0 @@
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package minhttp
import (
"os"
"regexp"
"testing"
"github.com/minio/minio/pkg/iodine"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestEmptyCountEnvVariable(c *C) {
os.Setenv(envCountKey, "")
n := &minNet{}
c.Assert(n.getInheritedListeners(), IsNil)
}
func (s *MySuite) TestZeroCountEnvVariable(c *C) {
os.Setenv(envCountKey, "0")
n := &minNet{}
c.Assert(n.getInheritedListeners(), IsNil)
}
func (s *MySuite) TestInvalidCountEnvVariable(c *C) {
os.Setenv(envCountKey, "a")
n := &minNet{}
expected := regexp.MustCompile("^found invalid count value: LISTEN_FDS=a$")
err := n.getInheritedListeners()
c.Assert(err, Not(IsNil))
c.Assert(expected.MatchString(iodine.ToError(err).Error()), Equals, true)
}
func (s *MySuite) TestInheritErrorOnListenTCPWithInvalidCount(c *C) {
os.Setenv(envCountKey, "a")
n := &minNet{}
expected := regexp.MustCompile("^found invalid count value: LISTEN_FDS=a$")
_, err := n.Listen("tcp", ":0")
c.Assert(err, Not(IsNil))
c.Assert(expected.MatchString(iodine.ToError(err).Error()), Equals, true)
}
func (s *MySuite) TestInvalidNetwork(c *C) {
os.Setenv(envCountKey, "")
n := &minNet{}
_, err := n.Listen("foo", "")
c.Assert(err, Not(IsNil))
c.Assert(regexp.MustCompile("^unknown network foo$").MatchString(iodine.ToError(err).Error()), Equals, true)
}
func (s *MySuite) TestInvalidTcpAddr(c *C) {
os.Setenv(envCountKey, "")
n := &minNet{}
_, err := n.Listen("tcp", "abc")
c.Assert(err, Not(IsNil))
c.Assert(regexp.MustCompile("^missing port in address abc$").MatchString(iodine.ToError(err).Error()), Equals, true)
}