zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.38k stars 3.97k forks source link

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

Closed answergong closed 5 months ago

answergong commented 2 years ago

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()
}
kevwan commented 2 years ago

What behaviors do you expect on graceful shutdown?

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

answergong commented 2 years ago

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.

kevwan commented 2 years ago

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.

answergong commented 2 years ago

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 ;

kevwan commented 2 years ago

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

answergong commented 2 years ago

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!

chenquan commented 2 years ago

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

yiGmMk commented 2 years ago

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

chenquan commented 2 years ago

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

yiGmMk commented 2 years ago

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

github-actions[bot] commented 8 months ago

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

github-actions[bot] commented 5 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.