You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
215 lines
5.2 KiB
215 lines
5.2 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
10 years ago
|
// Package minhttp provides easy to use graceful restart for a set of HTTP services
|
||
10 years ago
|
//
|
||
10 years ago
|
// This package is a fork from https://github.com/facebookgo/grace
|
||
10 years ago
|
//
|
||
|
// Re-licensing with Apache License 2.0, with code modifications
|
||
10 years ago
|
package minhttp
|
||
10 years ago
|
|
||
|
import (
|
||
|
"crypto/tls"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
"sync"
|
||
|
"syscall"
|
||
10 years ago
|
"time"
|
||
10 years ago
|
|
||
9 years ago
|
"github.com/facebookgo/httpdown"
|
||
10 years ago
|
"github.com/minio/minio/pkg/probe"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// An app contains one or more servers and their associated configuration.
|
||
10 years ago
|
type app struct {
|
||
|
servers []*http.Server
|
||
|
listeners []net.Listener
|
||
|
sds []httpdown.Server
|
||
10 years ago
|
net *minNet
|
||
10 years ago
|
errors chan *probe.Error
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// listen initailize listeners
|
||
10 years ago
|
func (a *app) listen() *probe.Error {
|
||
10 years ago
|
for _, s := range a.servers {
|
||
|
l, err := a.net.Listen("tcp", s.Addr)
|
||
|
if err != nil {
|
||
10 years ago
|
return err.Trace()
|
||
10 years ago
|
}
|
||
|
if s.TLSConfig != nil {
|
||
|
l = tls.NewListener(l, s.TLSConfig)
|
||
|
}
|
||
|
a.listeners = append(a.listeners, l)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
10 years ago
|
// serve start serving all listeners
|
||
10 years ago
|
func (a *app) serve() {
|
||
10 years ago
|
h := &httpdown.HTTP{
|
||
|
StopTimeout: 10 * time.Second,
|
||
|
KillTimeout: 1 * time.Second,
|
||
|
}
|
||
10 years ago
|
for i, s := range a.servers {
|
||
|
a.sds = append(a.sds, h.Serve(s, a.listeners[i]))
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// wait for http server to signal all requests that have been served
|
||
10 years ago
|
func (a *app) wait() {
|
||
|
var wg sync.WaitGroup
|
||
|
wg.Add(len(a.sds) * 2) // Wait & Stop
|
||
10 years ago
|
go a.trapSignal(&wg)
|
||
10 years ago
|
for _, s := range a.sds {
|
||
|
go func(s httpdown.Server) {
|
||
|
defer wg.Done()
|
||
|
if err := s.Wait(); err != nil {
|
||
10 years ago
|
a.errors <- probe.NewError(err)
|
||
10 years ago
|
}
|
||
|
}(s)
|
||
|
}
|
||
|
wg.Wait()
|
||
|
}
|
||
|
|
||
10 years ago
|
// trapSignal wait on listed signals for pre-defined behaviors
|
||
|
func (a *app) trapSignal(wg *sync.WaitGroup) {
|
||
10 years ago
|
ch := make(chan os.Signal, 10)
|
||
10 years ago
|
signal.Notify(ch, syscall.SIGTERM, syscall.SIGUSR2, syscall.SIGHUP)
|
||
10 years ago
|
for {
|
||
|
sig := <-ch
|
||
|
switch sig {
|
||
|
case syscall.SIGTERM:
|
||
10 years ago
|
// this ensures a subsequent TERM will trigger standard go behaviour of terminating
|
||
10 years ago
|
signal.Stop(ch)
|
||
10 years ago
|
// roll through all initialized http servers and stop them
|
||
|
for _, s := range a.sds {
|
||
|
go func(s httpdown.Server) {
|
||
|
defer wg.Done()
|
||
|
if err := s.Stop(); err != nil {
|
||
10 years ago
|
a.errors <- probe.NewError(err)
|
||
10 years ago
|
}
|
||
|
}(s)
|
||
|
}
|
||
10 years ago
|
return
|
||
10 years ago
|
case syscall.SIGUSR2:
|
||
|
fallthrough
|
||
|
case syscall.SIGHUP:
|
||
10 years ago
|
// 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 {
|
||
10 years ago
|
a.errors <- err.Trace()
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ListenAndServe will serve the given http.Servers and will monitor for signals
|
||
10 years ago
|
// allowing for graceful termination (SIGTERM) or restart (SIGUSR2/SIGHUP).
|
||
10 years ago
|
func ListenAndServe(servers ...*http.Server) *probe.Error {
|
||
10 years ago
|
// get parent process id
|
||
|
ppid := os.Getppid()
|
||
|
|
||
10 years ago
|
a := &app{
|
||
|
servers: servers,
|
||
|
listeners: make([]net.Listener, 0, len(servers)),
|
||
|
sds: make([]httpdown.Server, 0, len(servers)),
|
||
10 years ago
|
net: &minNet{},
|
||
10 years ago
|
errors: make(chan *probe.Error, 1+(len(servers)*2)),
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
// Acquire Listeners
|
||
|
if err := a.listen(); err != nil {
|
||
10 years ago
|
return err.Trace()
|
||
10 years ago
|
}
|
||
|
|
||
|
// Start serving.
|
||
|
a.serve()
|
||
|
|
||
10 years ago
|
// Close the parent if we inherited and it wasn't init that started us.
|
||
10 years ago
|
if os.Getenv("LISTEN_FDS") != "" && ppid != 1 {
|
||
10 years ago
|
if err := syscall.Kill(ppid, syscall.SIGTERM); err != nil {
|
||
10 years ago
|
return probe.NewError(err)
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
waitdone := make(chan struct{})
|
||
10 years ago
|
go func() {
|
||
10 years ago
|
defer close(waitdone)
|
||
10 years ago
|
a.wait()
|
||
10 years ago
|
// communicate by sending not by closing a channel
|
||
|
waitdone <- struct{}{}
|
||
10 years ago
|
}()
|
||
|
|
||
|
select {
|
||
|
case err := <-a.errors:
|
||
|
if err == nil {
|
||
|
panic("unexpected nil error")
|
||
|
}
|
||
10 years ago
|
return err.Trace()
|
||
10 years ago
|
case <-waitdone:
|
||
10 years ago
|
return nil
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
// ListenAndServeLimited is similar to ListenAndServe but ratelimited with connLimit value
|
||
10 years ago
|
func ListenAndServeLimited(connLimit int, servers ...*http.Server) *probe.Error {
|
||
10 years ago
|
// get parent process id
|
||
|
ppid := os.Getppid()
|
||
|
|
||
|
a := &app{
|
||
|
servers: servers,
|
||
|
listeners: make([]net.Listener, 0, len(servers)),
|
||
|
sds: make([]httpdown.Server, 0, len(servers)),
|
||
|
net: &minNet{connLimit: connLimit},
|
||
10 years ago
|
errors: make(chan *probe.Error, 1+(len(servers)*2)),
|
||
10 years ago
|
}
|
||
|
|
||
|
// Acquire Listeners
|
||
|
if err := a.listen(); err != nil {
|
||
10 years ago
|
return err.Trace()
|
||
10 years ago
|
}
|
||
|
|
||
|
// Start serving.
|
||
|
a.serve()
|
||
|
|
||
|
// 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 {
|
||
10 years ago
|
return probe.NewError(err)
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
waitdone := make(chan struct{})
|
||
|
go func() {
|
||
|
defer close(waitdone)
|
||
|
a.wait()
|
||
|
// communicate by sending not by closing a channel
|
||
|
waitdone <- struct{}{}
|
||
|
}()
|
||
|
|
||
|
select {
|
||
|
case err := <-a.errors:
|
||
|
if err == nil {
|
||
|
panic("unexpected nil error")
|
||
|
}
|
||
10 years ago
|
return err.Trace()
|
||
10 years ago
|
case <-waitdone:
|
||
|
return nil
|
||
|
}
|
||
|
}
|