diff --git a/servlet.go b/servlet.go index 1f3bfce..6c6d6f6 100644 --- a/servlet.go +++ b/servlet.go @@ -81,21 +81,7 @@ func (s *rpcServlet) Start(parentCTX cuc.Context, conn interface{}, doneChan cha } func (s *rpcServlet) Stop() { - if s.stopChan == nil { - panic("Server: server must be started before stopping it") - } - - close(s.stopChan) - s.stopWg.Wait() - - s.sh.Destroy(s.ctx) - - s.stopChan = nil - s.responseQueueChan = nil - s.conn = nil - s.serverCodec = nil - - logging.Logger().Info(fmt.Sprintf("Servlet is stopped")) + s.destroy(nil) } func (s *rpcServlet) Send(method string, args ...interface{}) (err error) { @@ -116,6 +102,26 @@ func (s *rpcServlet) Context() ServletContext { return s.ctx } +func (s *rpcServlet) destroy(err error) { + if s.stopChan == nil { + panic("Server: server must be started before stopping it") + } + + close(s.stopChan) + s.stopWg.Wait() + + s.sh.Destroy(s.ctx) + + s.stopChan = nil + s.responseQueueChan = nil + s.conn = nil + s.serverCodec = nil + + logging.Logger().Info(fmt.Sprintf("Servlet is stopped")) + + s.doneChan <- err +} + func handleServlet(s *rpcServlet) { var err error @@ -123,8 +129,7 @@ func handleServlet(s *rpcServlet) { defer func() { s.stopWg.Done() - s.Stop() - s.doneChan <- err + s.destroy(err) }() subStopChan := make(chan struct{})