1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-29 01:55:47 +00:00
Beego/grace/grace.go

159 lines
4.3 KiB
Go
Raw Normal View History

// Copyright 2014 beego Author. All Rights Reserved.
//
// 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-09-10 08:35:40 +00:00
// Package grace use to hot reload
// Description: http://grisha.org/blog/2014/06/03/graceful-restart-in-golang/
//
// Usage:
//
// import(
// "log"
// "net/http"
// "os"
//
// "github.com/astaxie/beego/grace"
// )
//
// func handler(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("WORLD!"))
// }
//
// func main() {
// mux := http.NewServeMux()
// mux.HandleFunc("/hello", handler)
//
2015-08-21 02:53:59 +00:00
// err := grace.ListenAndServe("localhost:8080", mux)
// if err != nil {
// log.Println(err)
// }
// log.Println("Server on 8080 stopped")
// os.Exit(0)
// }
package grace
import (
"flag"
"net/http"
"os"
"strings"
"sync"
"syscall"
"time"
)
const (
2015-12-09 15:47:44 +00:00
// PreSignal is the position to add filter before signal
2015-09-10 08:35:40 +00:00
PreSignal = iota
2015-12-09 15:47:44 +00:00
// PostSignal is the position to add filter after signal
2015-09-10 08:35:40 +00:00
PostSignal
2015-12-09 15:47:44 +00:00
// StateInit represent the application inited
2015-09-10 08:35:40 +00:00
StateInit
2015-12-09 15:47:44 +00:00
// StateRunning represent the application is running
2015-09-10 08:35:40 +00:00
StateRunning
2015-12-09 15:47:44 +00:00
// StateShuttingDown represent the application is shutting down
2015-09-10 08:35:40 +00:00
StateShuttingDown
2015-12-09 15:47:44 +00:00
// StateTerminate represent the application is killed
2015-09-10 08:35:40 +00:00
StateTerminate
)
var (
regLock *sync.Mutex
2015-09-10 08:35:40 +00:00
runningServers map[string]*Server
runningServersOrder []string
socketPtrOffsetMap map[string]uint
runningServersForked bool
2015-12-09 15:47:44 +00:00
// DefaultReadTimeOut is the HTTP read timeout
2015-09-10 08:35:40 +00:00
DefaultReadTimeOut time.Duration
2015-12-09 15:47:44 +00:00
// DefaultWriteTimeOut is the HTTP Write timeout
2015-09-10 08:35:40 +00:00
DefaultWriteTimeOut time.Duration
2015-12-09 15:47:44 +00:00
// DefaultMaxHeaderBytes is the Max HTTP Herder size, default is 0, no limit
DefaultMaxHeaderBytes int
2015-12-09 15:47:44 +00:00
// DefaultTimeout is the shutdown server's timeout. default is 60s
DefaultTimeout = 60 * time.Second
2015-12-09 15:47:44 +00:00
isChild bool
socketOrder string
2015-12-09 15:47:44 +00:00
once sync.Once
)
2015-12-09 01:03:10 +00:00
func onceInit() {
regLock = &sync.Mutex{}
flag.BoolVar(&isChild, "graceful", false, "listen on open fd (after forking)")
flag.StringVar(&socketOrder, "socketorder", "", "previous initialization order - used when more than one listener was started")
2015-09-10 08:35:40 +00:00
runningServers = make(map[string]*Server)
runningServersOrder = []string{}
socketPtrOffsetMap = make(map[string]uint)
}
2015-05-27 15:46:45 +00:00
// NewServer returns a new graceServer.
2015-09-10 08:35:40 +00:00
func NewServer(addr string, handler http.Handler) (srv *Server) {
once.Do(onceInit)
regLock.Lock()
defer regLock.Unlock()
if !flag.Parsed() {
flag.Parse()
}
if len(socketOrder) > 0 {
for i, addr := range strings.Split(socketOrder, ",") {
socketPtrOffsetMap[addr] = uint(i)
}
} else {
socketPtrOffsetMap[addr] = uint(len(runningServersOrder))
}
2015-09-10 08:35:40 +00:00
srv = &Server{
wg: sync.WaitGroup{},
sigChan: make(chan os.Signal),
isChild: isChild,
SignalHooks: map[int]map[os.Signal][]func(){
2015-09-10 08:35:40 +00:00
PreSignal: map[os.Signal][]func(){
syscall.SIGHUP: []func(){},
syscall.SIGINT: []func(){},
syscall.SIGTERM: []func(){},
},
2015-09-10 08:35:40 +00:00
PostSignal: map[os.Signal][]func(){
syscall.SIGHUP: []func(){},
syscall.SIGINT: []func(){},
syscall.SIGTERM: []func(){},
},
},
2015-09-10 08:35:40 +00:00
state: StateInit,
Network: "tcp",
}
2015-05-20 03:07:23 +00:00
srv.Server = &http.Server{}
srv.Server.Addr = addr
srv.Server.ReadTimeout = DefaultReadTimeOut
srv.Server.WriteTimeout = DefaultWriteTimeOut
srv.Server.MaxHeaderBytes = DefaultMaxHeaderBytes
srv.Server.Handler = handler
runningServersOrder = append(runningServersOrder, addr)
runningServers[addr] = srv
return
}
2015-09-10 08:35:40 +00:00
// ListenAndServe refer http.ListenAndServe
func ListenAndServe(addr string, handler http.Handler) error {
server := NewServer(addr, handler)
return server.ListenAndServe()
}
2015-09-10 08:35:40 +00:00
// ListenAndServeTLS refer http.ListenAndServeTLS
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler) error {
server := NewServer(addr, handler)
return server.ListenAndServeTLS(certFile, keyFile)
}