restconf web server is not shutdown when reconfigured
HRogge opened this issue · 3 comments
the ApplyOptions() function in stock/web.go creates a new http server every times new parameters are set via restconf.
I think it should use Shutdown()/Close() on the old server before opening the new one.
Maybe with something like the following change (this one has a hardcoded 10 second wait for graceful shutdown):
diff --git a/stock/web.go b/stock/web.go
index 6cbdf4b..192f38d 100644
--- a/stock/web.go
+++ b/stock/web.go
@@ -42,6 +42,14 @@ func (service *HttpServer) ApplyOptions(options HttpServerOptions) {
if options == service.options {
return
}
+ if service.Server != nil {
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ go func() {
+ _ = service.Server.Shutdown(ctx)
+ cancel()
+ }()
+ _ = service.Server.Close()
+ }
service.options = options
service.Server = &http.Server{
Addr: options.Port,
This does leave a race condition on closing and shutdown. any reason this cannot be inline?
The problem I tried to workaround was that "Shutdown()" might take a while but is a nicer option for open connections... but maybe you are right and its better just to forcefully close the server directly before reopening the new one.
Maybe time to revisit or close this Issue? I could make a PR without the go function for this.