├── go.mod ├── .idea ├── vcs.xml ├── .gitignore ├── modules.xml └── af.iml ├── defaultSignalHandle.go ├── defaultSignalHandle_darwin.go ├── defaultSignalHandle_freebsd.go ├── defaultSignalHandle_windows.go ├── LICENSE ├── README.md └── af.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/ailncode/af 2 | 3 | go 1.12 4 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/af.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /defaultSignalHandle.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 ailn(ailnindex@qq.com). All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | //Package af provides easy to use graceful restart a http server 6 | 7 | // +build linux 8 | 9 | package af 10 | 11 | import ( 12 | "fmt" 13 | "log" 14 | "os" 15 | "syscall" 16 | ) 17 | 18 | //Give some default signal handler if AF' signalHandlerMap is nil 19 | func (af *AF) defaultSignalHandle() { 20 | af.HandleSignal(func(af *AF) { 21 | log.Println(fmt.Sprintf("AF server is shutdown pid:%d", os.Getpid())) 22 | af.Stop() 23 | }, syscall.SIGINT, syscall.SIGTERM) 24 | af.HandleSignal(func(af *AF) { 25 | log.Println(fmt.Sprintf("AF server is reload pid:%d err:%v", os.Getpid(), af.Reload())) 26 | }, syscall.SIGUSR2) 27 | } 28 | -------------------------------------------------------------------------------- /defaultSignalHandle_darwin.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 ailn(ailnindex@qq.com). All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | //Package af provides easy to use graceful restart a http server 6 | 7 | // +build darwin 8 | 9 | package af 10 | 11 | import ( 12 | "fmt" 13 | "log" 14 | "os" 15 | "syscall" 16 | ) 17 | 18 | //Give some default signal handler if AF' signalHandlerMap is nil 19 | func (af *AF) defaultSignalHandle() { 20 | af.HandleSignal(func(af *AF) { 21 | log.Println(fmt.Sprintf("AF server is shutdown pid:%d", os.Getpid())) 22 | af.Stop() 23 | }, syscall.SIGINT, syscall.SIGTERM) 24 | af.HandleSignal(func(af *AF) { 25 | log.Println(fmt.Sprintf("AF server is reload pid:%d err:%v", os.Getpid(), af.Reload())) 26 | }, syscall.SIGUSR2) 27 | } 28 | -------------------------------------------------------------------------------- /defaultSignalHandle_freebsd.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 ailn(ailnindex@qq.com). All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | //Package af provides easy to use graceful restart a http server 6 | 7 | // +build freebsd 8 | 9 | package af 10 | 11 | import ( 12 | "fmt" 13 | "log" 14 | "os" 15 | "syscall" 16 | ) 17 | 18 | //Give some default signal handler if AF' signalHandlerMap is nil 19 | func (af *AF) defaultSignalHandle() { 20 | af.HandleSignal(func(af *AF) { 21 | log.Println(fmt.Sprintf("AF server is shutdown pid:%d", os.Getpid())) 22 | af.Stop() 23 | }, syscall.SIGINT, syscall.SIGTERM) 24 | af.HandleSignal(func(af *AF) { 25 | log.Println(fmt.Sprintf("AF server is reload pid:%d err:%v", os.Getpid(), af.Reload())) 26 | }, syscall.SIGUSR2) 27 | } 28 | -------------------------------------------------------------------------------- /defaultSignalHandle_windows.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 ailn(ailnindex@qq.com). All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | //Package af provides easy to use graceful restart a http server 6 | 7 | // +build windows 8 | 9 | package af 10 | 11 | import ( 12 | "fmt" 13 | "log" 14 | "os" 15 | "syscall" 16 | ) 17 | 18 | //Give some default signal handler if AF' signalHandlerMap is nil 19 | func (af *AF) defaultSignalHandle() { 20 | af.HandleSignal(func(af *AF) { 21 | log.Println(fmt.Sprintf("AF server is shutdown pid:%d", os.Getpid())) 22 | af.Stop() 23 | }, syscall.SIGINT, syscall.SIGTERM) 24 | af.HandleSignal(func(af *AF) { 25 | log.Println(fmt.Sprintf("AF server is reload pid:%d err:%v", os.Getpid(), af.Reload())) 26 | }, syscall.Signal(31)) 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright <2018> 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 | 5 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 | 7 | 2. 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. 8 | 9 | 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | What is AF 2 | ------------------------------------------------------------------------------------------------- 3 | *The name of AF comes from air refueling.* 4 | 5 | *AF is a package of Go.* 6 | 7 | *You can use the AF write a graceful reload HTTP server easily.* 8 | 9 | Support 10 | ------------------------------------------------------------------------------------------------- 11 | * *nix 12 | * Windows (only build and run) 13 | 14 | Features 15 | ------------------------------------------------------------------------------------------------- 16 | 17 | * graceful reload 18 | * graceful stop 19 | * custom signal handler 20 | 21 | 22 | Install 23 | ------------------------------------------------------------------------------------------------- 24 | 25 | ``` go 26 | go get -u github.com/ailncode/af 27 | #or 28 | go mod edit -require=github.com/ailncode/af@latest 29 | ``` 30 | 31 | Usage 32 | ------------------------------------------------------------------------------------------------- 33 | 34 | 1. Import the AF 35 | 36 | ```go 37 | import "github.com/ailncode/af" 38 | ``` 39 | 40 | 2. Simple use 41 | 42 | ```go 43 | package main 44 | 45 | import( 46 | "fmt" 47 | "net/http" 48 | "github.com/ailncode/af" 49 | ) 50 | 51 | func main(){ 52 | http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request) { 53 | fmt.Fprintln(w, "ok") 54 | }) 55 | refuel := af.Default() 56 | //refuel.Addr = ":8080" 57 | //:8080 is the default Addr 58 | //refuel.Handler = http.DefaultServeMux 59 | //http.DefaultServeMux is the default HTTP handler 60 | //refuel.ShutdownTimeOut = time.Second * 10 61 | //time.Second * 10 is the default ShutdownTimeOut 62 | refuel.Run() 63 | //You can check error in here. 64 | } 65 | ``` 66 | 3. Reload & Stop your server 67 | 68 | ```shell 69 | #Reload 70 | kill -USR2 71 | #Stop 72 | kill -INT #or kill -TERM 73 | ``` 74 | 75 | 4. Use your custom Listen Address Handler ShutdownTimeOut 76 | 77 | ```go 78 | package main 79 | 80 | import( 81 | "net/http" 82 | "time" 83 | "github.com/ailncode/af" 84 | "github.com/gin-gonic/gin" 85 | ) 86 | 87 | func main(){ 88 | //Use handler like *gin.Engine 89 | router :=gin.Default() 90 | router.GET("/", func(context *gin.Context) { 91 | context.String(http.StatusOK,"ok") 92 | }) 93 | refuel := af.Default() 94 | refuel.Handler = router 95 | //You can use the below code also. 96 | //af := af.NewWithHandler(r) 97 | refuel.Addr = ":80" 98 | refuel.ShutdownTimeOut = time.Second * 10 99 | refuel.Run() 100 | } 101 | ``` 102 | 103 | 104 | 5. Use your custom system signal handler 105 | 106 | ```go 107 | package main 108 | 109 | import ( 110 | "github.com/ailncode/af" 111 | "syscall" 112 | "time" 113 | ) 114 | 115 | func main() { 116 | refuel := af.Default() 117 | refuel.ShutdownTimeOut = time.Second * 10 118 | refuel.HandleSignal(func(r *af.AF) { 119 | r.Stop() 120 | }, syscall.SIGINT, syscall.SIGTERM) 121 | refuel.HandleSignal(func(r *af.AF) { 122 | r.Reload() 123 | //You can check error in here 124 | }, syscall.SIGUSR2) 125 | refuel.Run() 126 | } 127 | ``` 128 | 129 | 6. Use your custom server 130 | 131 | ```go 132 | package main 133 | 134 | import ( 135 | "af" 136 | "net/http" 137 | ) 138 | 139 | func main() { 140 | server := &http.Server{ 141 | Addr:"8080", 142 | Handler:http.DefaultServeMux, 143 | //... 144 | } 145 | refuel := af.NewWithServer(server) 146 | refuel.Run() 147 | } 148 | ``` 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /af.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 ailn(ailnindex@qq.com). All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | //Package af provides easy to use graceful restart a http server 6 | 7 | package af 8 | 9 | import ( 10 | "context" 11 | "errors" 12 | "flag" 13 | "fmt" 14 | "log" 15 | "net" 16 | "net/http" 17 | "os" 18 | "os/exec" 19 | "os/signal" 20 | "reflect" 21 | "time" 22 | ) 23 | 24 | var ( 25 | graceful *bool = flag.Bool("graceful", false, "listen on fd open 3 (internal use only)") 26 | ) 27 | 28 | type AF struct { 29 | Addr string 30 | Handler http.Handler 31 | ShutdownTimeOut time.Duration 32 | listener net.Listener 33 | server *http.Server 34 | signalSlice []os.Signal 35 | signalHandlerMap map[os.Signal]func(*AF) 36 | } 37 | 38 | // Default Get default AF 39 | func Default() *AF { 40 | return &AF{ 41 | Addr: ":8080", 42 | ShutdownTimeOut: time.Second * 10, 43 | } 44 | } 45 | 46 | // NewWithHandler Get AF with a http handler 47 | func NewWithHandler(handler http.Handler) *AF { 48 | return &AF{ 49 | Addr: ":8080", 50 | Handler: handler, 51 | ShutdownTimeOut: time.Second * 10, 52 | } 53 | } 54 | 55 | // NewWithServer Get AF with a http server 56 | func NewWithServer(server *http.Server) *AF { 57 | return &AF{ 58 | server: server, 59 | Addr: server.Addr, 60 | Handler: server.Handler, 61 | ShutdownTimeOut: time.Second * 10, 62 | } 63 | } 64 | 65 | //init AF 66 | func (af *AF) init() error { 67 | if af.server == nil { 68 | if af.Handler == nil { 69 | af.Handler = http.DefaultServeMux 70 | } 71 | af.server = &http.Server{ 72 | Addr: af.Addr, 73 | Handler: af.Handler, 74 | ReadTimeout: 10 * time.Second, 75 | WriteTimeout: 10 * time.Second, 76 | } 77 | } 78 | var err error 79 | if *graceful { 80 | f := os.NewFile(3, "") 81 | af.listener, err = net.FileListener(f) 82 | } else { 83 | af.listener, err = net.Listen("tcp", af.server.Addr) 84 | } 85 | return err 86 | } 87 | 88 | //signal handle 89 | func (af *AF) signalHandle() { 90 | signalChan := make(chan os.Signal, 1) 91 | signal.Notify(signalChan, af.signalSlice...) 92 | for { 93 | select { 94 | case s := <-signalChan: 95 | if f, ok := af.signalHandlerMap[s]; ok { 96 | f(af) 97 | } 98 | break 99 | } 100 | } 101 | } 102 | 103 | // HandleSignal Give a handler for signals 104 | func (af *AF) HandleSignal(handler func(*AF), signals ...os.Signal) { 105 | if af.signalHandlerMap == nil { 106 | af.signalHandlerMap = make(map[os.Signal]func(*AF)) 107 | } 108 | for _, s := range signals { 109 | if _, ok := af.signalHandlerMap[s]; !ok { 110 | af.signalSlice = append(af.signalSlice, s) 111 | } 112 | af.signalHandlerMap[s] = handler 113 | } 114 | } 115 | 116 | //Run the AF 117 | func (af *AF) Run() error { 118 | flag.Parse() 119 | var err error 120 | if err = af.init(); err != nil { 121 | return err 122 | } 123 | go func(af *AF) { 124 | log.Println(fmt.Sprintf("AF server is run at pid:%d", os.Getpid())) 125 | err = af.server.Serve(af.listener) 126 | }(af) 127 | if af.signalHandlerMap == nil { 128 | af.defaultSignalHandle() 129 | } 130 | af.signalHandle() 131 | return nil 132 | } 133 | 134 | //Stop the AF 135 | func (af *AF) Stop() { 136 | ctx, _ := context.WithTimeout(context.Background(), af.ShutdownTimeOut) 137 | err := af.server.Shutdown(ctx) 138 | if err == nil { 139 | os.Exit(0) 140 | } 141 | os.Exit(1) 142 | } 143 | 144 | //Reload the AF 145 | func (af *AF) Reload() error { 146 | //listener 147 | listener, ok := af.listener.(*net.TCPListener) 148 | if !ok { 149 | return errors.New("AF.listener type must be *net.TCPListener,but got type " + reflect.TypeOf(af.listener).Name()) 150 | } 151 | file, err := listener.File() 152 | if err != nil { 153 | return err 154 | } 155 | args := os.Args 156 | if !*graceful { 157 | args = append(args, "-graceful") 158 | } 159 | cmd := exec.Command(args[0], args[1:]...) 160 | cmd.Stdin = os.Stdin 161 | cmd.Stdout = os.Stdout 162 | cmd.Stderr = os.Stderr 163 | cmd.ExtraFiles = []*os.File{file} 164 | err = cmd.Start() 165 | if err == nil { 166 | af.Stop() 167 | } 168 | return err 169 | } 170 | --------------------------------------------------------------------------------