diff options
author | Wim <wim@42.be> | 2018-08-06 21:47:05 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2018-08-06 21:47:05 +0200 |
commit | 51062863a5c34d81e296cf15c61140911037cf3b (patch) | |
tree | 9b5e044672486326c7a0ca8fb26430f37bf4d83c /vendor/github.com/facebookgo/grace | |
parent | 4fb4b7aa6c02a54db8ad8dd98e4d321396926c0d (diff) | |
download | matterbridge-msglm-51062863a5c34d81e296cf15c61140911037cf3b.tar.gz matterbridge-msglm-51062863a5c34d81e296cf15c61140911037cf3b.tar.bz2 matterbridge-msglm-51062863a5c34d81e296cf15c61140911037cf3b.zip |
Use mod vendor for vendored directory (backwards compatible)
Diffstat (limited to 'vendor/github.com/facebookgo/grace')
-rw-r--r-- | vendor/github.com/facebookgo/grace/gracehttp/http.go | 190 | ||||
-rw-r--r-- | vendor/github.com/facebookgo/grace/gracehttp/license | 30 | ||||
-rw-r--r-- | vendor/github.com/facebookgo/grace/gracenet/license | 30 | ||||
-rw-r--r-- | vendor/github.com/facebookgo/grace/gracenet/net.go | 252 |
4 files changed, 0 insertions, 502 deletions
diff --git a/vendor/github.com/facebookgo/grace/gracehttp/http.go b/vendor/github.com/facebookgo/grace/gracehttp/http.go deleted file mode 100644 index fa3ac883..00000000 --- a/vendor/github.com/facebookgo/grace/gracehttp/http.go +++ /dev/null @@ -1,190 +0,0 @@ -// Package gracehttp provides easy to use graceful restart -// functionality for HTTP server. -package gracehttp - -import ( - "bytes" - "crypto/tls" - "fmt" - "log" - "net" - "net/http" - "os" - "os/signal" - "sync" - "syscall" - - "github.com/facebookgo/grace/gracenet" - "github.com/facebookgo/httpdown" -) - -var ( - logger *log.Logger - didInherit = os.Getenv("LISTEN_FDS") != "" - ppid = os.Getppid() -) - -// An app contains one or more servers and associated configuration. -type app struct { - servers []*http.Server - http *httpdown.HTTP - net *gracenet.Net - listeners []net.Listener - sds []httpdown.Server - errors chan error -} - -func newApp(servers []*http.Server) *app { - return &app{ - servers: servers, - http: &httpdown.HTTP{}, - net: &gracenet.Net{}, - listeners: make([]net.Listener, 0, len(servers)), - sds: make([]httpdown.Server, 0, len(servers)), - - // 2x num servers for possible Close or Stop errors + 1 for possible - // StartProcess error. - errors: make(chan error, 1+(len(servers)*2)), - } -} - -func (a *app) listen() error { - for _, s := range a.servers { - // TODO: default addresses - l, err := a.net.Listen("tcp", s.Addr) - if err != nil { - return err - } - if s.TLSConfig != nil { - l = tls.NewListener(l, s.TLSConfig) - } - a.listeners = append(a.listeners, l) - } - return nil -} - -func (a *app) serve() { - for i, s := range a.servers { - a.sds = append(a.sds, a.http.Serve(s, a.listeners[i])) - } -} - -func (a *app) wait() { - var wg sync.WaitGroup - wg.Add(len(a.sds) * 2) // Wait & Stop - go a.signalHandler(&wg) - for _, s := range a.sds { - go func(s httpdown.Server) { - defer wg.Done() - if err := s.Wait(); err != nil { - a.errors <- err - } - }(s) - } - wg.Wait() -} - -func (a *app) term(wg *sync.WaitGroup) { - for _, s := range a.sds { - go func(s httpdown.Server) { - defer wg.Done() - if err := s.Stop(); err != nil { - a.errors <- err - } - }(s) - } -} - -func (a *app) signalHandler(wg *sync.WaitGroup) { - ch := make(chan os.Signal, 10) - signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR2) - for { - sig := <-ch - switch sig { - case syscall.SIGINT, syscall.SIGTERM: - // this ensures a subsequent INT/TERM will trigger standard go behaviour of - // terminating. - signal.Stop(ch) - a.term(wg) - return - case syscall.SIGUSR2: - // 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 <- err - } - } - } -} - -// Serve will serve the given http.Servers and will monitor for signals -// allowing for graceful termination (SIGTERM) or restart (SIGUSR2). -func Serve(servers ...*http.Server) error { - a := newApp(servers) - - // Acquire Listeners - if err := a.listen(); err != nil { - return err - } - - // Some useful logging. - if logger != nil { - if didInherit { - if ppid == 1 { - logger.Printf("Listening on init activated %s", pprintAddr(a.listeners)) - } else { - const msg = "Graceful handoff of %s with new pid %d and old pid %d" - logger.Printf(msg, pprintAddr(a.listeners), os.Getpid(), ppid) - } - } else { - const msg = "Serving %s with pid %d" - logger.Printf(msg, pprintAddr(a.listeners), os.Getpid()) - } - } - - // Start serving. - a.serve() - - // Close the parent if we inherited and it wasn't init that started us. - if didInherit && ppid != 1 { - if err := syscall.Kill(ppid, syscall.SIGTERM); err != nil { - return fmt.Errorf("failed to close parent: %s", err) - } - } - - waitdone := make(chan struct{}) - go func() { - defer close(waitdone) - a.wait() - }() - - select { - case err := <-a.errors: - if err == nil { - panic("unexpected nil error") - } - return err - case <-waitdone: - if logger != nil { - logger.Printf("Exiting pid %d.", os.Getpid()) - } - return nil - } -} - -// Used for pretty printing addresses. -func pprintAddr(listeners []net.Listener) []byte { - var out bytes.Buffer - for i, l := range listeners { - if i != 0 { - fmt.Fprint(&out, ", ") - } - fmt.Fprint(&out, l.Addr()) - } - return out.Bytes() -} - -// SetLogger sets logger to be able to grab some useful logs -func SetLogger(l *log.Logger) { - logger = l -} diff --git a/vendor/github.com/facebookgo/grace/gracehttp/license b/vendor/github.com/facebookgo/grace/gracehttp/license deleted file mode 100644 index 3aea8753..00000000 --- a/vendor/github.com/facebookgo/grace/gracehttp/license +++ /dev/null @@ -1,30 +0,0 @@ -BSD License - -For grace software - -Copyright (c) 2015, Facebook, Inc. All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name Facebook nor the names of its contributors may be used to - endorse or promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/facebookgo/grace/gracenet/license b/vendor/github.com/facebookgo/grace/gracenet/license deleted file mode 100644 index 3aea8753..00000000 --- a/vendor/github.com/facebookgo/grace/gracenet/license +++ /dev/null @@ -1,30 +0,0 @@ -BSD License - -For grace software - -Copyright (c) 2015, Facebook, Inc. All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name Facebook nor the names of its contributors may be used to - endorse or promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/facebookgo/grace/gracenet/net.go b/vendor/github.com/facebookgo/grace/gracenet/net.go deleted file mode 100644 index a980954a..00000000 --- a/vendor/github.com/facebookgo/grace/gracenet/net.go +++ /dev/null @@ -1,252 +0,0 @@ -// Package gracenet provides a family of Listen functions that either open a -// fresh connection or provide an inherited connection from when the process -// was started. The behave like their counterparts in the net package, but -// transparently provide support for graceful restarts without dropping -// connections. This is provided in a systemd socket activation compatible form -// to allow using socket activation. -// -// BUG: Doesn't handle closing of listeners. -package gracenet - -import ( - "fmt" - "net" - "os" - "os/exec" - "strconv" - "strings" - "sync" -) - -const ( - // Used to indicate a graceful restart in the new process. - envCountKey = "LISTEN_FDS" - envCountKeyPrefix = envCountKey + "=" -) - -// In order to keep the working directory the same as when we started we record -// it at startup. -var originalWD, _ = os.Getwd() - -// Net provides the family of Listen functions and maintains the associated -// state. Typically you will have only once instance of Net per application. -type Net struct { - inherited []net.Listener - active []net.Listener - mutex sync.Mutex - inheritOnce sync.Once - - // used in tests to override the default behavior of starting from fd 3. - fdStart int -} - -func (n *Net) inherit() error { - var retErr error - n.inheritOnce.Do(func() { - n.mutex.Lock() - defer n.mutex.Unlock() - countStr := os.Getenv(envCountKey) - if countStr == "" { - return - } - count, err := strconv.Atoi(countStr) - if err != nil { - retErr = fmt.Errorf("found invalid count value: %s=%s", envCountKey, countStr) - return - } - - // In tests this may be overridden. - fdStart := n.fdStart - if fdStart == 0 { - // In normal operations if we are inheriting, the listeners will begin at - // fd 3. - fdStart = 3 - } - - for i := fdStart; i < fdStart+count; i++ { - file := os.NewFile(uintptr(i), "listener") - l, err := net.FileListener(file) - if err != nil { - file.Close() - retErr = fmt.Errorf("error inheriting socket fd %d: %s", i, err) - return - } - if err := file.Close(); err != nil { - retErr = fmt.Errorf("error closing inherited socket fd %d: %s", i, err) - return - } - n.inherited = append(n.inherited, l) - } - }) - return retErr -} - -// 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 *Net) Listen(nett, laddr string) (net.Listener, error) { - switch nett { - default: - return nil, net.UnknownNetworkError(nett) - case "tcp", "tcp4", "tcp6": - addr, err := net.ResolveTCPAddr(nett, laddr) - if err != nil { - return nil, err - } - return n.ListenTCP(nett, addr) - case "unix", "unixpacket", "invalid_unix_net_for_test": - addr, err := net.ResolveUnixAddr(nett, laddr) - if err != nil { - return nil, err - } - return n.ListenUnix(nett, addr) - } -} - -// 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 *Net) ListenTCP(nett string, laddr *net.TCPAddr) (*net.TCPListener, error) { - if err := n.inherit(); err != nil { - return nil, err - } - - n.mutex.Lock() - defer n.mutex.Unlock() - - // look for an inherited listener - for i, l := range n.inherited { - if l == nil { // we nil used inherited listeners - continue - } - if isSameAddr(l.Addr(), laddr) { - n.inherited[i] = nil - n.active = append(n.active, l) - return l.(*net.TCPListener), nil - } - } - - // make a fresh listener - l, err := net.ListenTCP(nett, laddr) - if err != nil { - return nil, err - } - n.active = append(n.active, l) - return l, nil -} - -// 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 *Net) ListenUnix(nett string, laddr *net.UnixAddr) (*net.UnixListener, error) { - if err := n.inherit(); err != nil { - return nil, err - } - - n.mutex.Lock() - defer n.mutex.Unlock() - - // look for an inherited listener - for i, l := range n.inherited { - if l == nil { // we nil used inherited listeners - continue - } - if isSameAddr(l.Addr(), laddr) { - n.inherited[i] = nil - n.active = append(n.active, l) - return l.(*net.UnixListener), nil - } - } - - // make a fresh listener - l, err := net.ListenUnix(nett, laddr) - if err != nil { - return nil, err - } - n.active = append(n.active, l) - return l, nil -} - -// activeListeners returns a snapshot copy of the active listeners. -func (n *Net) activeListeners() ([]net.Listener, error) { - n.mutex.Lock() - defer n.mutex.Unlock() - ls := make([]net.Listener, len(n.active)) - copy(ls, n.active) - return ls, nil -} - -func isSameAddr(a1, a2 net.Addr) bool { - if a1.Network() != a2.Network() { - return false - } - a1s := a1.String() - a2s := a2.String() - if a1s == a2s { - 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 -} - -// StartProcess starts a new process passing it the active listeners. It -// doesn't fork, but starts a new process using the same environment and -// 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 *Net) StartProcess() (int, error) { - listeners, err := n.activeListeners() - if err != nil { - return 0, err - } - - // Extract the fds from the listeners. - files := make([]*os.File, len(listeners)) - for i, l := range listeners { - files[i], err = l.(filer).File() - if err != nil { - return 0, err - } - defer files[i].Close() - } - - // Use the original binary location. This works with symlinks such that if - // 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, err - } - - // Pass on the environment and replace the old count key with the new one. - var env []string - for _, v := range os.Environ() { - if !strings.HasPrefix(v, envCountKeyPrefix) { - env = append(env, v) - } - } - env = append(env, fmt.Sprintf("%s%d", envCountKeyPrefix, len(listeners))) - - allFiles := append([]*os.File{os.Stdin, os.Stdout, os.Stderr}, files...) - process, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{ - Dir: originalWD, - Env: env, - Files: allFiles, - }) - if err != nil { - return 0, err - } - return process.Pid, nil -} - -type filer interface { - File() (*os.File, error) -} |