Created
February 23, 2020 04:02
-
-
Save scue/9562d967aba0fed31444308ffee70b1a to your computer and use it in GitHub Desktop.
cloudflare/tableflip Go进程热重启方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"os" | |
"os/signal" | |
"syscall" | |
"github.com/cloudflare/tableflip" | |
) | |
func init() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(http.StatusOK) | |
w.Write([]byte("hello world!\n")) | |
}) | |
} | |
func main() { | |
upg, _ := tableflip.New(tableflip.Options{}) | |
defer upg.Stop() | |
go func() { | |
sig := make(chan os.Signal, 1) | |
signal.Notify(sig, syscall.SIGHUP) | |
for range sig { | |
upg.Upgrade() | |
} | |
}() | |
// Listen must be called before Ready | |
ln, e := upg.Listen("tcp", "localhost:8081") | |
if e != nil { | |
log.Panic(e) | |
} | |
defer ln.Close() | |
go http.Serve(ln, nil) | |
if err := upg.Ready(); err != nil { | |
panic(err) | |
} | |
<-upg.Exit() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment