1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-01 01:20:56 +00:00
Beego/grace/server.go

354 lines
8.5 KiB
Go
Raw Normal View History

2015-05-27 15:46:45 +00:00
package grace
import (
2019-01-17 12:17:57 +00:00
"context"
2015-05-27 15:46:45 +00:00
"crypto/tls"
2017-11-15 14:42:30 +00:00
"crypto/x509"
2015-05-27 15:46:45 +00:00
"fmt"
2017-11-15 14:42:30 +00:00
"io/ioutil"
2015-05-27 15:46:45 +00:00
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
2019-01-20 03:17:10 +00:00
"time"
2015-05-27 15:46:45 +00:00
)
2015-09-10 08:35:40 +00:00
// Server embedded http.Server
type Server struct {
2015-05-27 15:46:45 +00:00
*http.Server
2019-01-17 12:17:57 +00:00
ln net.Listener
SignalHooks map[int]map[os.Signal][]func()
sigChan chan os.Signal
isChild bool
state uint8
Network string
terminalChan chan error
2015-05-27 15:46:45 +00:00
}
// Serve accepts incoming connections on the Listener l,
// creating a new service goroutine for each.
// The service goroutines read requests and then call srv.Handler to reply to them.
2015-09-10 08:35:40 +00:00
func (srv *Server) Serve() (err error) {
srv.state = StateRunning
2019-01-17 12:17:57 +00:00
defer func() { srv.state = StateTerminate }()
// When Shutdown is called, Serve, ListenAndServe, and ListenAndServeTLS
// immediately return ErrServerClosed. Make sure the program doesn't exit
// and waits instead for Shutdown to return.
if err = srv.Server.Serve(srv.ln); err != nil && err != http.ErrServerClosed {
2019-01-18 11:50:22 +00:00
log.Println(syscall.Getpid(), "Server.Serve() error:", err)
2019-01-17 12:17:57 +00:00
return err
}
2019-01-18 11:50:22 +00:00
log.Println(syscall.Getpid(), srv.ln.Addr(), "Listener closed.")
2019-01-17 12:17:57 +00:00
// wait for Shutdown to return
return <-srv.terminalChan
2015-05-27 15:46:45 +00:00
}
// ListenAndServe listens on the TCP network address srv.Addr and then calls Serve
// to handle requests on incoming connections. If srv.Addr is blank, ":http" is
// used.
2015-09-10 08:35:40 +00:00
func (srv *Server) ListenAndServe() (err error) {
2015-05-27 15:46:45 +00:00
addr := srv.Addr
if addr == "" {
addr = ":http"
}
go srv.handleSignals()
2019-01-17 12:17:57 +00:00
srv.ln, err = srv.getListener(addr)
2015-05-27 15:46:45 +00:00
if err != nil {
log.Println(err)
return err
}
if srv.isChild {
process, err := os.FindProcess(os.Getppid())
if err != nil {
log.Println(err)
return err
}
err = process.Signal(syscall.SIGTERM)
2015-05-27 15:46:45 +00:00
if err != nil {
return err
}
}
log.Println(os.Getpid(), srv.Addr)
return srv.Serve()
}
// ListenAndServeTLS listens on the TCP network address srv.Addr and then calls
// Serve to handle requests on incoming TLS connections.
//
// Filenames containing a certificate and matching private key for the server must
// be provided. If the certificate is signed by a certificate authority, the
// certFile should be the concatenation of the server's certificate followed by the
// CA's certificate.
//
// If srv.Addr is blank, ":https" is used.
2015-09-10 08:35:40 +00:00
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) (err error) {
2015-05-27 15:46:45 +00:00
addr := srv.Addr
if addr == "" {
addr = ":https"
}
2016-01-29 05:11:11 +00:00
if srv.TLSConfig == nil {
srv.TLSConfig = &tls.Config{}
2015-05-27 15:46:45 +00:00
}
2016-01-29 05:11:11 +00:00
if srv.TLSConfig.NextProtos == nil {
srv.TLSConfig.NextProtos = []string{"http/1.1"}
2015-05-27 15:46:45 +00:00
}
2016-01-29 05:11:11 +00:00
srv.TLSConfig.Certificates = make([]tls.Certificate, 1)
srv.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
2015-05-27 15:46:45 +00:00
if err != nil {
return
}
go srv.handleSignals()
2019-01-17 12:17:57 +00:00
ln, err := srv.getListener(addr)
2015-05-27 15:46:45 +00:00
if err != nil {
log.Println(err)
return err
}
2019-01-20 03:17:10 +00:00
srv.ln = tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, srv.TLSConfig)
2015-05-27 15:46:45 +00:00
if srv.isChild {
process, err := os.FindProcess(os.Getppid())
if err != nil {
log.Println(err)
return err
}
err = process.Signal(syscall.SIGTERM)
2015-05-27 15:46:45 +00:00
if err != nil {
return err
}
}
2019-01-17 12:17:57 +00:00
2015-05-27 15:46:45 +00:00
log.Println(os.Getpid(), srv.Addr)
return srv.Serve()
}
2017-11-25 17:16:37 +00:00
// ListenAndServeMutualTLS listens on the TCP network address srv.Addr and then calls
// Serve to handle requests on incoming mutual TLS connections.
2017-11-15 14:42:30 +00:00
func (srv *Server) ListenAndServeMutualTLS(certFile, keyFile, trustFile string) (err error) {
addr := srv.Addr
if addr == "" {
addr = ":https"
}
if srv.TLSConfig == nil {
srv.TLSConfig = &tls.Config{}
}
if srv.TLSConfig.NextProtos == nil {
srv.TLSConfig.NextProtos = []string{"http/1.1"}
}
srv.TLSConfig.Certificates = make([]tls.Certificate, 1)
srv.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return
}
srv.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
pool := x509.NewCertPool()
data, err := ioutil.ReadFile(trustFile)
if err != nil {
log.Println(err)
return err
}
pool.AppendCertsFromPEM(data)
srv.TLSConfig.ClientCAs = pool
log.Println("Mutual HTTPS")
go srv.handleSignals()
2019-01-17 12:17:57 +00:00
ln, err := srv.getListener(addr)
2017-11-15 14:42:30 +00:00
if err != nil {
log.Println(err)
return err
}
2019-01-20 03:17:10 +00:00
srv.ln = tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, srv.TLSConfig)
2017-11-15 14:42:30 +00:00
if srv.isChild {
process, err := os.FindProcess(os.Getppid())
if err != nil {
log.Println(err)
return err
}
err = process.Kill()
if err != nil {
return err
}
}
2019-01-17 12:17:57 +00:00
2017-11-15 14:42:30 +00:00
log.Println(os.Getpid(), srv.Addr)
return srv.Serve()
}
2015-05-27 15:46:45 +00:00
// getListener either opens a new socket to listen on, or takes the acceptor socket
// it got passed when restarted.
2015-09-10 08:35:40 +00:00
func (srv *Server) getListener(laddr string) (l net.Listener, err error) {
2015-05-27 15:46:45 +00:00
if srv.isChild {
2015-09-10 08:35:40 +00:00
var ptrOffset uint
2015-05-27 15:46:45 +00:00
if len(socketPtrOffsetMap) > 0 {
ptrOffset = socketPtrOffsetMap[laddr]
log.Println("laddr", laddr, "ptr offset", socketPtrOffsetMap[laddr])
}
f := os.NewFile(uintptr(3+ptrOffset), "")
l, err = net.FileListener(f)
if err != nil {
err = fmt.Errorf("net.FileListener error: %v", err)
return
}
} else {
l, err = net.Listen(srv.Network, laddr)
if err != nil {
err = fmt.Errorf("net.Listen error: %v", err)
return
}
}
return
}
2019-01-20 03:17:10 +00:00
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}
2015-05-27 15:46:45 +00:00
// handleSignals listens for os Signals and calls any hooked in function that the
// user had registered with the signal.
2015-09-10 08:35:40 +00:00
func (srv *Server) handleSignals() {
2015-05-27 15:46:45 +00:00
var sig os.Signal
signal.Notify(
srv.sigChan,
hookableSignals...,
2015-05-27 15:46:45 +00:00
)
pid := syscall.Getpid()
for {
sig = <-srv.sigChan
2015-09-10 08:35:40 +00:00
srv.signalHooks(PreSignal, sig)
2015-05-27 15:46:45 +00:00
switch sig {
case syscall.SIGHUP:
log.Println(pid, "Received SIGHUP. forking.")
err := srv.fork()
if err != nil {
log.Println("Fork err:", err)
}
case syscall.SIGINT:
log.Println(pid, "Received SIGINT.")
srv.shutdown()
case syscall.SIGTERM:
log.Println(pid, "Received SIGTERM.")
srv.shutdown()
default:
log.Printf("Received %v: nothing i care about...\n", sig)
}
2015-09-10 08:35:40 +00:00
srv.signalHooks(PostSignal, sig)
2015-05-27 15:46:45 +00:00
}
}
2015-09-10 08:35:40 +00:00
func (srv *Server) signalHooks(ppFlag int, sig os.Signal) {
2015-05-27 15:46:45 +00:00
if _, notSet := srv.SignalHooks[ppFlag][sig]; !notSet {
return
}
for _, f := range srv.SignalHooks[ppFlag][sig] {
f()
}
}
// shutdown closes the listener so that no new connections are accepted. it also
// starts a goroutine that will serverTimeout (stop all running requests) the server
// after DefaultTimeout.
2015-09-10 08:35:40 +00:00
func (srv *Server) shutdown() {
if srv.state != StateRunning {
2015-05-27 15:46:45 +00:00
return
}
2015-09-10 08:35:40 +00:00
srv.state = StateShuttingDown
2019-01-17 12:17:57 +00:00
log.Println(syscall.Getpid(), "Waiting for connections to finish...")
ctx := context.Background()
2015-05-27 15:46:45 +00:00
if DefaultTimeout >= 0 {
2019-01-18 11:33:45 +00:00
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), DefaultTimeout)
defer cancel()
2015-05-27 15:46:45 +00:00
}
2019-01-17 12:17:57 +00:00
srv.terminalChan <- srv.Server.Shutdown(ctx)
2015-05-27 15:46:45 +00:00
}
2015-09-10 08:35:40 +00:00
func (srv *Server) fork() (err error) {
2015-05-27 15:46:45 +00:00
regLock.Lock()
defer regLock.Unlock()
if runningServersForked {
return
}
runningServersForked = true
var files = make([]*os.File, len(runningServers))
var orderArgs = make([]string, len(runningServers))
for _, srvPtr := range runningServers {
2019-01-17 12:17:57 +00:00
f, _ := srvPtr.ln.(*net.TCPListener).File()
files[socketPtrOffsetMap[srvPtr.Server.Addr]] = f
2015-05-27 15:46:45 +00:00
orderArgs[socketPtrOffsetMap[srvPtr.Server.Addr]] = srvPtr.Server.Addr
}
log.Println(files)
path := os.Args[0]
var args []string
if len(os.Args) > 1 {
for _, arg := range os.Args[1:] {
if arg == "-graceful" {
break
}
args = append(args, arg)
}
}
args = append(args, "-graceful")
if len(runningServers) > 1 {
args = append(args, fmt.Sprintf(`-socketorder=%s`, strings.Join(orderArgs, ",")))
log.Println(args)
}
cmd := exec.Command(path, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.ExtraFiles = files
err = cmd.Start()
if err != nil {
log.Fatalf("Restart: Failed to launch, error: %v", err)
}
return
}
// RegisterSignalHook registers a function to be run PreSignal or PostSignal for a given signal.
func (srv *Server) RegisterSignalHook(ppFlag int, sig os.Signal, f func()) (err error) {
if ppFlag != PreSignal && ppFlag != PostSignal {
2017-04-30 14:41:23 +00:00
err = fmt.Errorf("Invalid ppFlag argument. Must be either grace.PreSignal or grace.PostSignal")
return
}
for _, s := range hookableSignals {
if s == sig {
srv.SignalHooks[ppFlag][sig] = append(srv.SignalHooks[ppFlag][sig], f)
return
}
}
2017-04-30 14:41:23 +00:00
err = fmt.Errorf("Signal '%v' is not supported", sig)
return
}