diff --git a/server.go b/server.go index 9a46b80..0dcb8a7 100644 --- a/server.go +++ b/server.go @@ -69,12 +69,13 @@ func (s *server) Stop() { logging.Logger().Warn("Server: server must be started before stopping it") return } + + s.sh.Destroy(s.ctx) + close(s.stopChan) s.stopWg.Wait() s.stopChan = nil - s.sh.OnStop(s.ctx) - logging.Logger().Infof("Server[%s] is stopped", s.sh.GetName()) } @@ -83,7 +84,10 @@ func (s *server) Context() ServerContext { } func handleServer(s *server) { - defer s.stopWg.Done() + defer func() { + s.sh.OnStop(s.ctx) + s.stopWg.Done() + }() logging.Logger().Infof("Server[%s] is started", s.sh.GetName()) s.sh.OnStart(s.ctx) diff --git a/server_handler.go b/server_handler.go index d040fac..39fd6de 100644 --- a/server_handler.go +++ b/server_handler.go @@ -12,14 +12,16 @@ type ServerHandler interface { OnError(serverCTX ServerContext, conn net.Conn, status int, reason error) - // OnStop invoked when server is stopped + OnStop(serverCTX ServerContext) + + // Destroy invoked when server will destroy // If you override ths method, must call // - // func (sh *ServerHandler) OnStop() { + // func (sh *ServerHandler) Destroy() { // ... - // sh.ServerHandler.OnStop() + // sh.ServerHandler.Destroy() // } - OnStop(serverCTX ServerContext) + Destroy(serverCTX ServerContext) GetName() string GetSocketHandler() SocketHandler diff --git a/server_handlers.go b/server_handlers.go index 6c902d0..5389634 100644 --- a/server_handlers.go +++ b/server_handlers.go @@ -45,9 +45,13 @@ func (sh *ServerHandlers) OnError(serverCTX ServerContext, conn net.Conn, status logging.Logger().Errorf("Server: error status: %d, reason: %v, [%v]", status, reason, conn) } -// OnStop invoked when server is stopped -// If you override ths method, must call func (sh *ServerHandlers) OnStop(serverCTX ServerContext) { + // no op +} + +// Destroy invoked when server is stopped +// If you override ths method, must call +func (sh *ServerHandlers) Destroy(serverCTX ServerContext) { sh.SocketHandler.Destroy() }