Skip to content

Instantly share code, notes, and snippets.

@scue
Created February 23, 2020 04:02
Show Gist options
  • Save scue/9562d967aba0fed31444308ffee70b1a to your computer and use it in GitHub Desktop.
Save scue/9562d967aba0fed31444308ffee70b1a to your computer and use it in GitHub Desktop.
cloudflare/tableflip Go进程热重启方法
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