This commit is contained in:
crusader 2018-03-27 15:43:21 +09:00
parent 4f607dd971
commit a1cf7963b5
3 changed files with 19 additions and 9 deletions

View File

@ -69,12 +69,13 @@ func (s *server) Stop() {
logging.Logger().Warn("Server: server must be started before stopping it") logging.Logger().Warn("Server: server must be started before stopping it")
return return
} }
s.sh.Destroy(s.ctx)
close(s.stopChan) close(s.stopChan)
s.stopWg.Wait() s.stopWg.Wait()
s.stopChan = nil s.stopChan = nil
s.sh.OnStop(s.ctx)
logging.Logger().Infof("Server[%s] is stopped", s.sh.GetName()) logging.Logger().Infof("Server[%s] is stopped", s.sh.GetName())
} }
@ -83,7 +84,10 @@ func (s *server) Context() ServerContext {
} }
func handleServer(s *server) { 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()) logging.Logger().Infof("Server[%s] is started", s.sh.GetName())
s.sh.OnStart(s.ctx) s.sh.OnStart(s.ctx)

View File

@ -12,14 +12,16 @@ type ServerHandler interface {
OnError(serverCTX ServerContext, conn net.Conn, status int, reason error) 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 // 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 GetName() string
GetSocketHandler() SocketHandler GetSocketHandler() SocketHandler

View File

@ -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) 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) { 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() sh.SocketHandler.Destroy()
} }