This commit is contained in:
crusader 2018-04-12 10:43:43 +09:00
parent 89a2ffcef0
commit 17ef0e1833
3 changed files with 30 additions and 30 deletions

View File

@ -25,7 +25,7 @@ type Server struct {
func (s *Server) ListenAndServe() error {
if s.stopChan != nil {
return fmt.Errorf(s.serverMessage("already running. Stop it before starting it again"))
return fmt.Errorf("%s already running. Stop it before starting it again", s.logHeader())
}
var (
@ -33,7 +33,7 @@ func (s *Server) ListenAndServe() error {
listener net.Listener
)
if nil == s.ServerHandler {
return fmt.Errorf(s.serverMessage("server handler must be specified"))
return fmt.Errorf("%s server handler must be specified", s.logHeader())
}
if err = s.ServerHandler.Validate(); nil != err {
return err
@ -41,7 +41,7 @@ func (s *Server) ListenAndServe() error {
s.ctx = s.ServerHandler.ServerCtx()
if nil == s.ctx {
return fmt.Errorf(s.serverMessage("ServerCtx is nil"))
return fmt.Errorf("%s ServerCtx is nil", s.logHeader())
}
if err = s.ServerHandler.Init(s.ctx); nil != err {
@ -64,7 +64,7 @@ func (s *Server) ListenAndServe() error {
func (s *Server) Shutdown(ctx context.Context) error {
if s.stopChan == nil {
return fmt.Errorf("server must be started before stopping it")
return fmt.Errorf("%s must be started before stopping it", s.logHeader())
}
close(s.stopChan)
s.stopWg.Wait()
@ -76,8 +76,8 @@ func (s *Server) Shutdown(ctx context.Context) error {
return nil
}
func (s *Server) serverMessage(msg string) string {
return fmt.Sprintf("Server[%s]: %s", s.ServerHandler.GetName(), msg)
func (s *Server) logHeader() string {
return fmt.Sprintf("Server[%s]:", s.ServerHandler.GetName())
}
func (s *Server) handleServer(listener net.Listener) error {
@ -92,7 +92,7 @@ func (s *Server) handleServer(listener net.Listener) error {
listener.Close()
}
s.ServerHandler.OnStop(s.ctx)
logging.Logger().Infof(s.serverMessage("Stopped"))
logging.Logger().Infof("%s Stopped", s.logHeader())
s.stopWg.Done()
}()
@ -100,7 +100,7 @@ func (s *Server) handleServer(listener net.Listener) error {
return err
}
logging.Logger().Infof(s.serverMessage("Started"))
logging.Logger().Infof("%s Started", s.logHeader())
for {
acceptChan := make(chan struct{})
@ -108,7 +108,7 @@ func (s *Server) handleServer(listener net.Listener) error {
go func() {
if netConn, err = listener.Accept(); err != nil {
if nil == stopping.Load() {
logging.Logger().Errorf(s.serverMessage(fmt.Sprintf("%v", err)))
logging.Logger().Errorf("%s %v", s.logHeader(), err)
}
}
close(acceptChan)
@ -136,7 +136,7 @@ func (s *Server) handleServer(listener net.Listener) error {
if 0 < s.ServerHandler.GetConcurrency() {
sz := s.srw.ConnectionSize()
if sz >= s.ServerHandler.GetConcurrency() {
logging.Logger().Warnf(s.serverMessage(fmt.Sprintf("max connections size %d, refuse", sz)))
logging.Logger().Warnf("%s max connections size %d, refuse", s.logHeader(), sz)
netConn.Close()
continue
}
@ -144,18 +144,18 @@ func (s *Server) handleServer(listener net.Listener) error {
servlet := s.ServerHandler.(ServerHandler).Servlet(s.ctx, netConn)
if nil == servlet {
logging.Logger().Errorf(s.serverMessage("Servlet is nil"))
logging.Logger().Errorf("%s Servlet is nil", s.logHeader())
continue
}
servletCtx := servlet.ServletCtx(s.ctx)
if nil == servletCtx {
logging.Logger().Errorf(s.serverMessage("ServletCtx is nil"))
logging.Logger().Errorf("%s ServletCtx is nil", s.logHeader())
continue
}
if err := servlet.Handshake(servletCtx, netConn); nil != err {
logging.Logger().Infof(s.serverMessage(fmt.Sprintf("Handshaking of Client[%s] has been failed %v", netConn.RemoteAddr(), err)))
logging.Logger().Infof("%s Handshaking of Client[%s] has been failed %v", s.logHeader(), netConn.RemoteAddr(), err)
continue
}

View File

@ -33,17 +33,17 @@ func (s *Server) ListenAndServe() error {
listener net.Listener
)
if nil == s.ServerHandler {
return fmt.Errorf("Server: server handler must be specified")
return fmt.Errorf("%s server handler must be specified", s.logHeader())
}
s.ServerHandler.Validate()
if s.stopChan != nil {
return fmt.Errorf(s.serverMessage("already running. Stop it before starting it again"))
return fmt.Errorf("%s already running. Stop it before starting it again", s.logHeader())
}
s.ctx = s.ServerHandler.ServerCtx()
if nil == s.ctx {
return fmt.Errorf(s.serverMessage("ServerCtx is nil"))
return fmt.Errorf("%s ServerCtx is nil", s.logHeader())
}
s.hs = &fasthttp.Server{
@ -85,7 +85,7 @@ func (s *Server) ListenAndServe() error {
func (s *Server) Shutdown(ctx context.Context) error {
if s.stopChan == nil {
return fmt.Errorf("server must be started before stopping it")
return fmt.Errorf("%s must be started before stopping it", s.logHeader())
}
close(s.stopChan)
s.stopWg.Wait()
@ -97,8 +97,8 @@ func (s *Server) Shutdown(ctx context.Context) error {
return nil
}
func (s *Server) serverMessage(msg string) string {
return fmt.Sprintf("Server[%s]: %s", s.ServerHandler.GetName(), msg)
func (s *Server) logHeader() string {
return fmt.Sprintf("Server[%s]:", s.ServerHandler.GetName())
}
func (s *Server) handleServer(listener net.Listener) error {
@ -113,7 +113,7 @@ func (s *Server) handleServer(listener net.Listener) error {
}
s.ServerHandler.OnStop(s.ctx)
logging.Logger().Infof(s.serverMessage("Stopped"))
logging.Logger().Infof("%s Stopped", s.logHeader())
s.stopWg.Done()
}()
@ -133,7 +133,7 @@ func (s *Server) handleServer(listener net.Listener) error {
hsCloseChan <- nil
}()
logging.Logger().Infof(s.serverMessage("Started"))
logging.Logger().Infof("%s Started", s.logHeader())
select {
case err, _ := <-hsCloseChan:
@ -159,7 +159,7 @@ func (s *Server) httpHandler(ctx *fasthttp.RequestCtx) {
if 0 < s.ServerHandler.GetConcurrency() {
sz := s.srw.ConnectionSize()
if sz >= s.ServerHandler.GetConcurrency() {
logging.Logger().Warnf(s.serverMessage(fmt.Sprintf("max connections size %d, refuse", sz)))
logging.Logger().Warnf("%s max connections size %d, refuse", s.logHeader(), sz)
s.onError(ctx, fasthttp.StatusServiceUnavailable, err)
return
}

View File

@ -29,17 +29,17 @@ func (s *Server) ListenAndServe() error {
listener net.Listener
)
if nil == s.ServerHandler {
return fmt.Errorf("Server: server handler must be specified")
return fmt.Errorf("%s server handler must be specified", s.logHeader())
}
s.ServerHandler.Validate()
if s.stopChan != nil {
return fmt.Errorf(s.serverMessage("already running. Stop it before starting it again"))
return fmt.Errorf("%s already running. Stop it before starting it again", s.logHeader())
}
s.ctx = s.ServerHandler.ServerCtx()
if nil == s.ctx {
return fmt.Errorf(s.serverMessage("ServerCtx is nil"))
return fmt.Errorf("%s ServerCtx is nil", s.logHeader())
}
s.hs = &fasthttp.Server{
@ -68,7 +68,7 @@ func (s *Server) ListenAndServe() error {
func (s *Server) Shutdown(ctx context.Context) error {
if s.stopChan == nil {
return fmt.Errorf("server must be started before stopping it")
return fmt.Errorf("%s must be started before stopping it", s.logHeader())
}
close(s.stopChan)
s.stopWg.Wait()
@ -80,8 +80,8 @@ func (s *Server) Shutdown(ctx context.Context) error {
return nil
}
func (s *Server) serverMessage(msg string) string {
return fmt.Sprintf("Server[%s]: %s", s.ServerHandler.GetName(), msg)
func (s *Server) logHeader() string {
return fmt.Sprintf("Server[%s]:", s.ServerHandler.GetName())
}
func (s *Server) handleServer(listener net.Listener) error {
@ -96,7 +96,7 @@ func (s *Server) handleServer(listener net.Listener) error {
}
s.ServerHandler.OnStop(s.ctx)
logging.Logger().Infof(s.serverMessage("Stopped"))
logging.Logger().Infof("%s Stopped", s.logHeader())
s.stopWg.Done()
}()
@ -116,7 +116,7 @@ func (s *Server) handleServer(listener net.Listener) error {
hsCloseChan <- nil
}()
logging.Logger().Infof(s.serverMessage("Started"))
logging.Logger().Infof("%s Started", s.logHeader())
select {
case err, _ := <-hsCloseChan: