go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

the problem of grace shutdown(it can't shutdown gracefully)

Open answergong opened this issue 3 years ago • 11 comments
trafficstars

I tested the http service myself, use ctrl_c and kill pid to cancel the program, but it also exited directly, and did not wait for the coroutine processing of the processing program to end before exiting;

I read the documentation and said that the server.Start() method of go zero will automatically handle graceful restarts, but I didn't find any problems.

the code is automatic generation by (goctl api new greet)

the code is

func main() {
	flag.Parse()
	var c config.Config
	conf.MustLoad(*configFile, &c)
	c.Timeout = 10000

	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}

answergong avatar Aug 10 '22 14:08 answergong

What behaviors do you expect on graceful shutdown?

It serves all the ongoing requests and exit? Do you have ongoing requests?

kevwan avatar Aug 13 '22 02:08 kevwan

What behaviors do you expect on graceful shutdown?

It serves all the ongoing requests and exit? Do you have ongoing requests?

thanks for you reply! i have two api func ;in one api ,i let it sleep 60s ;another is qiuckly return the response; and the main.go's code is on the above(the code is from the test code "greet",generate by "goctl api new greet"); when i request the two api.one is quickly return response; another is in procing(wait the sleep) ;in this time ,i opeate ctrl +c(or kill) the main func ,the program shutdown Immediately(i hope the program should hold on 60s to wait the running api.but it isn't ),so i think it has something wrong; I looked up zero's documentation and he said server.start() has implemented elegant shutdown.

answergong avatar Aug 13 '22 15:08 answergong

That's not graceful shutdown.

If your program stuck, how to stop it?

Graceful shutdown means finishing ongoing requests, not accepting new requests, then quit.

kevwan avatar Aug 14 '22 02:08 kevwan

thanks, I understand what you mean, but I do have a ongoing request(sleep 60s). when I was released to terminate main.go, I thought he should wait for 60 s before the program quit. this is elegant closure, but the fact is that he did not wait, but directly quit. do you think this situation should not wait? i think graceful showdown should wait the 60s ;

answergong avatar Aug 14 '22 02:08 answergong

There is a method SetTimeToForceQuit, but if too long, rolling update will take too much time.

kevwan avatar Aug 14 '22 02:08 kevwan

There is a method SetTimeToForceQuit, but if too long, rolling update will take too much time.

I don't think my problem is that I have reached the maximum forced exit time. My phenomenon is: when I send a termination request, the program immediately returns and does not wait for the end of my processing request; If you have some time ,i hope you can run the test code locally (the code is generate by "goctl api new greet", then add a new handler func,in the func sleep some time ),i think you will get the some result as me ;thanks you very much!

answergong avatar Aug 14 '22 09:08 answergong

When receiving SIGTERM in Linux or Darwin, it will close gracefully. code: https://github.com/zeromicro/go-zero/blob/5208def65ae88c70b87f547696a804ee9a3e0bc1/core/proc/signals.go#L1-L2 https://github.com/zeromicro/go-zero/blob/5208def65ae88c70b87f547696a804ee9a3e0bc1/core/proc/signals.go#L38-L46

chenquan avatar Aug 19 '22 00:08 chenquan

http.Server provide func RegisterOnShutdown to clean resources when it shutdown ,is there any ways in gozero to do things like so ? I think this also part of gracefully exit stuffs

yiGmMk avatar Nov 21 '22 10:11 yiGmMk

http.Server provide func RegisterOnShutdown to clean resources when it shutdown ,is there any ways in gozero to do things like so ? I think this also part of gracefully exit stuffs

Reference here: https://github.com/zeromicro/go-zero/blob/95a5f6449334f67d835fe176e361f0745f1a0961/rest/internal/starter.go#L41-L45

chenquan avatar Nov 22 '22 01:11 chenquan

http.Server provide func RegisterOnShutdown to clean resources when it shutdown ,is there any ways in gozero to do things like so ? I think this also part of gracefully exit stuffs

Reference here:

https://github.com/zeromicro/go-zero/blob/95a5f6449334f67d835fe176e361f0745f1a0961/rest/internal/starter.go#L41-L45

138E87E0 thanks

yiGmMk avatar Nov 22 '22 03:11 yiGmMk

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Mar 18 '24 01:03 github-actions[bot]