lesismal / nbio

Pure Go 1000k+ connections solution, support tls/http1.x/websocket and basically compatible with net/http, with high-performance and low memory cost, non-blocking, event-driven, easy-to-use.
MIT License
2.17k stars 153 forks source link

refactor:remove time.sleep() in Engine.stop() #426

Closed byene0923 closed 5 months ago

byene0923 commented 5 months ago

hello, I don't know the use of time.Sleep(time.Second / 5) in Engine.Stop(), maybe for test but forget to delete it?

lesismal commented 5 months ago

The engine closes all the conns in Stop(), but OnClose callback is executed async. The sleep here is for the OnClose to do some clean work. It's not a perfect duration, it's just an estimated duration. Most of the time we don't need to wait for that cleaning work, but waiting for a little moment may be a little better, and 1/5 s doesn't matter too much.

byene0923 commented 5 months ago

yeah, i understand , that's good, is there a better way, such as graceful shutdown?give a way for user to choose whther wait for OnClose()? i dont know how other network frame to deal with this

lesismal commented 5 months ago

Here is engine shutdown: https://github.com/lesismal/nbio/blob/master/engine.go#L239

Users can wg.Add() in OnOpen and wg.Done() in OnClose and wg.Wait() after engine.Shutdown().

So, removing the sleep is also fine.