2015-10-11 09:48:25 +02:00
|
|
|
// +build windows
|
|
|
|
|
2015-07-07 06:54:00 +02:00
|
|
|
/*
|
2015-07-25 02:51:40 +02:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-07-07 06:54:00 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-07-13 02:58:47 +02:00
|
|
|
// Package minhttp provides easy to use graceful restart for a set of HTTP services
|
2015-07-07 00:03:13 +02:00
|
|
|
//
|
2015-07-13 02:58:47 +02:00
|
|
|
// This package is a fork from https://github.com/facebookgo/grace
|
2015-07-07 01:26:54 +02:00
|
|
|
//
|
|
|
|
// Re-licensing with Apache License 2.0, with code modifications
|
2015-07-13 02:58:47 +02:00
|
|
|
package minhttp
|
2015-07-07 00:03:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2015-07-07 03:11:25 +02:00
|
|
|
"time"
|
2015-07-07 00:03:13 +02:00
|
|
|
|
2015-08-23 03:34:00 +02:00
|
|
|
"github.com/facebookgo/httpdown"
|
2016-02-11 01:40:09 +01:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2015-07-07 00:03:13 +02:00
|
|
|
)
|
|
|
|
|
2015-07-07 03:11:25 +02:00
|
|
|
// An app contains one or more servers and their associated configuration.
|
2015-07-07 00:03:13 +02:00
|
|
|
type app struct {
|
|
|
|
servers []*http.Server
|
|
|
|
listeners []net.Listener
|
|
|
|
sds []httpdown.Server
|
2015-07-13 02:58:47 +02:00
|
|
|
net *minNet
|
2015-08-08 08:47:22 +02:00
|
|
|
errors chan *probe.Error
|
2015-07-07 00:03:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-07 03:11:25 +02:00
|
|
|
// listen initailize listeners
|
2015-08-04 01:17:21 +02:00
|
|
|
func (a *app) listen() *probe.Error {
|
2015-07-07 00:03:13 +02:00
|
|
|
for _, s := range a.servers {
|
|
|
|
l, err := a.net.Listen("tcp", s.Addr)
|
|
|
|
if err != nil {
|
2015-08-04 03:01:18 +02:00
|
|
|
return err.Trace()
|
2015-07-07 00:03:13 +02:00
|
|
|
}
|
|
|
|
if s.TLSConfig != nil {
|
|
|
|
l = tls.NewListener(l, s.TLSConfig)
|
|
|
|
}
|
|
|
|
a.listeners = append(a.listeners, l)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-07 03:11:25 +02:00
|
|
|
// serve start serving all listeners
|
2015-07-07 00:03:13 +02:00
|
|
|
func (a *app) serve() {
|
2015-07-07 03:11:25 +02:00
|
|
|
h := &httpdown.HTTP{
|
|
|
|
StopTimeout: 10 * time.Second,
|
|
|
|
KillTimeout: 1 * time.Second,
|
|
|
|
}
|
2015-07-07 00:03:13 +02:00
|
|
|
for i, s := range a.servers {
|
|
|
|
a.sds = append(a.sds, h.Serve(s, a.listeners[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 03:11:25 +02:00
|
|
|
// wait for http server to signal all requests that have been served
|
2015-07-07 00:03:13 +02:00
|
|
|
func (a *app) wait() {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(a.sds) * 2) // Wait & Stop
|
2015-07-07 03:11:25 +02:00
|
|
|
go a.trapSignal(&wg)
|
2015-07-07 00:03:13 +02:00
|
|
|
for _, s := range a.sds {
|
|
|
|
go func(s httpdown.Server) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := s.Wait(); err != nil {
|
2015-08-08 08:47:22 +02:00
|
|
|
a.errors <- probe.NewError(err)
|
2015-07-07 00:03:13 +02:00
|
|
|
}
|
|
|
|
}(s)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2015-07-07 03:11:25 +02:00
|
|
|
// trapSignal wait on listed signals for pre-defined behaviors
|
|
|
|
func (a *app) trapSignal(wg *sync.WaitGroup) {
|
2015-07-07 00:03:13 +02:00
|
|
|
ch := make(chan os.Signal, 10)
|
2015-10-11 09:48:25 +02:00
|
|
|
signal.Notify(ch, syscall.SIGTERM, syscall.SIGHUP)
|
2015-07-07 00:03:13 +02:00
|
|
|
for {
|
|
|
|
sig := <-ch
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGTERM:
|
2015-07-07 03:11:25 +02:00
|
|
|
// this ensures a subsequent TERM will trigger standard go behaviour of terminating
|
2015-07-07 00:03:13 +02:00
|
|
|
signal.Stop(ch)
|
2015-07-07 03:11:25 +02:00
|
|
|
// 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 {
|
2015-08-08 08:47:22 +02:00
|
|
|
a.errors <- probe.NewError(err)
|
2015-07-07 03:11:25 +02:00
|
|
|
}
|
|
|
|
}(s)
|
|
|
|
}
|
2015-07-07 00:03:13 +02:00
|
|
|
return
|
2015-07-07 01:26:54 +02:00
|
|
|
case syscall.SIGHUP:
|
2015-07-07 00:03:13 +02:00
|
|
|
// 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 {
|
2015-08-08 08:47:22 +02:00
|
|
|
a.errors <- err.Trace()
|
2015-07-07 00:03:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenAndServe will serve the given http.Servers and will monitor for signals
|
2015-07-07 01:26:54 +02:00
|
|
|
// allowing for graceful termination (SIGTERM) or restart (SIGUSR2/SIGHUP).
|
2015-08-04 01:17:21 +02:00
|
|
|
func ListenAndServe(servers ...*http.Server) *probe.Error {
|
2015-07-08 00:33:08 +02:00
|
|
|
// get parent process id
|
|
|
|
ppid := os.Getppid()
|
|
|
|
|
2015-07-07 03:11:25 +02:00
|
|
|
a := &app{
|
|
|
|
servers: servers,
|
|
|
|
listeners: make([]net.Listener, 0, len(servers)),
|
|
|
|
sds: make([]httpdown.Server, 0, len(servers)),
|
2015-07-13 02:58:47 +02:00
|
|
|
net: &minNet{},
|
2015-08-08 08:47:22 +02:00
|
|
|
errors: make(chan *probe.Error, 1+(len(servers)*2)),
|
2015-07-07 03:11:25 +02:00
|
|
|
}
|
2015-07-07 00:03:13 +02:00
|
|
|
|
|
|
|
// Acquire Listeners
|
|
|
|
if err := a.listen(); err != nil {
|
2015-08-04 01:17:21 +02:00
|
|
|
return err.Trace()
|
2015-07-07 00:03:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start serving.
|
|
|
|
a.serve()
|
|
|
|
|
2015-07-07 01:26:54 +02:00
|
|
|
// Close the parent if we inherited and it wasn't init that started us.
|
2015-07-08 00:33:08 +02:00
|
|
|
if os.Getenv("LISTEN_FDS") != "" && ppid != 1 {
|
2015-10-11 09:48:25 +02:00
|
|
|
if err := terminateProcess(ppid, 1); err != nil {
|
2015-08-08 08:47:22 +02:00
|
|
|
return probe.NewError(err)
|
2015-07-07 01:26:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
waitdone := make(chan struct{})
|
2015-07-07 00:03:13 +02:00
|
|
|
go func() {
|
2015-07-07 01:26:54 +02:00
|
|
|
defer close(waitdone)
|
2015-07-07 00:03:13 +02:00
|
|
|
a.wait()
|
2015-07-07 03:11:25 +02:00
|
|
|
// communicate by sending not by closing a channel
|
|
|
|
waitdone <- struct{}{}
|
2015-07-07 00:03:13 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-a.errors:
|
|
|
|
if err == nil {
|
|
|
|
panic("unexpected nil error")
|
|
|
|
}
|
2015-08-08 08:47:22 +02:00
|
|
|
return err.Trace()
|
2015-07-07 01:26:54 +02:00
|
|
|
case <-waitdone:
|
2015-07-07 00:03:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 19:26:04 +02:00
|
|
|
|
|
|
|
// ListenAndServeLimited is similar to ListenAndServe but ratelimited with connLimit value
|
2015-08-04 01:17:21 +02:00
|
|
|
func ListenAndServeLimited(connLimit int, servers ...*http.Server) *probe.Error {
|
2015-07-13 19:26:04 +02:00
|
|
|
// 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},
|
2015-08-08 08:47:22 +02:00
|
|
|
errors: make(chan *probe.Error, 1+(len(servers)*2)),
|
2015-07-13 19:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Acquire Listeners
|
|
|
|
if err := a.listen(); err != nil {
|
2015-08-04 01:17:21 +02:00
|
|
|
return err.Trace()
|
2015-07-13 19:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2015-10-11 09:48:25 +02:00
|
|
|
if err := terminateProcess(ppid, 1); err != nil {
|
2015-08-08 08:47:22 +02:00
|
|
|
return probe.NewError(err)
|
2015-07-13 19:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2015-08-08 08:47:22 +02:00
|
|
|
return err.Trace()
|
2015-07-13 19:26:04 +02:00
|
|
|
case <-waitdone:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|