Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't catch syscall.SIGINT after upgrade #76

Open
myuid opened this issue Nov 14, 2022 · 1 comment
Open

Can't catch syscall.SIGINT after upgrade #76

myuid opened this issue Nov 14, 2022 · 1 comment

Comments

@myuid
Copy link

myuid commented Nov 14, 2022

I can catch syscall.SIGINT when I call upgrade(), but I can't catch syscall.SIGINT when upgrade() is called. What if I restart and shut down gracefully?

func main() {
upg, _ := tableflip.New(tableflip.Options{})
defer upg.Stop()

go func() {
	sig := make(chan os.Signal, 1)
	//
	signal.Notify(sig, syscall.SIGHUP, os.Interrupt, syscall.SIGTERM)
	for ch := range sig {
		switch ch {
		case syscall.SIGHUP:
			err := upg.Upgrade()
			if err != nil {
				log.Fatal(err)
			}
		default:
			upg.Interrupt()
		}
	}
}()

ln, err := upg.Listen("tcp", ":8080")
if err != nil {
	log.Fatalln("Can't listen:", err)
}
defer func(ln net.Listener) {
	_ = ln.Close()
}(ln)

count := 0
router := gin.Default()
router.GET("/", func(c *gin.Context) {
	time.Sleep(5 * time.Second)
	count++
	c.String(http.StatusOK, strconv.Itoa(count))
})

server := http.Server{
	Handler: router,
}
go func() {
	if err := server.Serve(ln); err != http.ErrServerClosed {
		log.Fatal("listen: ", err)
	}
}()

// Listen must be called before Ready

if err := upg.Ready(); err != nil {
	log.Fatal(err)
}

<-upg.Exit()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
	log.Fatal("Server forced to shutdown:", err)
}
println("going to shutdown")

}

@dayueba
Copy link

dayueba commented Jan 12, 2023

kill -s HUP pid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants