ing
This commit is contained in:
parent
89a2ffcef0
commit
17ef0e1833
|
@ -25,7 +25,7 @@ type Server struct {
|
||||||
|
|
||||||
func (s *Server) ListenAndServe() error {
|
func (s *Server) ListenAndServe() error {
|
||||||
if s.stopChan != nil {
|
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 (
|
var (
|
||||||
|
@ -33,7 +33,7 @@ func (s *Server) ListenAndServe() error {
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
)
|
)
|
||||||
if nil == s.ServerHandler {
|
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 {
|
if err = s.ServerHandler.Validate(); nil != err {
|
||||||
return err
|
return err
|
||||||
|
@ -41,7 +41,7 @@ func (s *Server) ListenAndServe() error {
|
||||||
|
|
||||||
s.ctx = s.ServerHandler.ServerCtx()
|
s.ctx = s.ServerHandler.ServerCtx()
|
||||||
if nil == s.ctx {
|
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 {
|
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 {
|
func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
if s.stopChan == nil {
|
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)
|
close(s.stopChan)
|
||||||
s.stopWg.Wait()
|
s.stopWg.Wait()
|
||||||
|
@ -76,8 +76,8 @@ func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) serverMessage(msg string) string {
|
func (s *Server) logHeader() string {
|
||||||
return fmt.Sprintf("Server[%s]: %s", s.ServerHandler.GetName(), msg)
|
return fmt.Sprintf("Server[%s]:", s.ServerHandler.GetName())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleServer(listener net.Listener) error {
|
func (s *Server) handleServer(listener net.Listener) error {
|
||||||
|
@ -92,7 +92,7 @@ func (s *Server) handleServer(listener net.Listener) error {
|
||||||
listener.Close()
|
listener.Close()
|
||||||
}
|
}
|
||||||
s.ServerHandler.OnStop(s.ctx)
|
s.ServerHandler.OnStop(s.ctx)
|
||||||
logging.Logger().Infof(s.serverMessage("Stopped"))
|
logging.Logger().Infof("%s Stopped", s.logHeader())
|
||||||
s.stopWg.Done()
|
s.stopWg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ func (s *Server) handleServer(listener net.Listener) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logging.Logger().Infof(s.serverMessage("Started"))
|
logging.Logger().Infof("%s Started", s.logHeader())
|
||||||
|
|
||||||
for {
|
for {
|
||||||
acceptChan := make(chan struct{})
|
acceptChan := make(chan struct{})
|
||||||
|
@ -108,7 +108,7 @@ func (s *Server) handleServer(listener net.Listener) error {
|
||||||
go func() {
|
go func() {
|
||||||
if netConn, err = listener.Accept(); err != nil {
|
if netConn, err = listener.Accept(); err != nil {
|
||||||
if nil == stopping.Load() {
|
if nil == stopping.Load() {
|
||||||
logging.Logger().Errorf(s.serverMessage(fmt.Sprintf("%v", err)))
|
logging.Logger().Errorf("%s %v", s.logHeader(), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(acceptChan)
|
close(acceptChan)
|
||||||
|
@ -136,7 +136,7 @@ func (s *Server) handleServer(listener net.Listener) error {
|
||||||
if 0 < s.ServerHandler.GetConcurrency() {
|
if 0 < s.ServerHandler.GetConcurrency() {
|
||||||
sz := s.srw.ConnectionSize()
|
sz := s.srw.ConnectionSize()
|
||||||
if sz >= s.ServerHandler.GetConcurrency() {
|
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()
|
netConn.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -144,18 +144,18 @@ func (s *Server) handleServer(listener net.Listener) error {
|
||||||
|
|
||||||
servlet := s.ServerHandler.(ServerHandler).Servlet(s.ctx, netConn)
|
servlet := s.ServerHandler.(ServerHandler).Servlet(s.ctx, netConn)
|
||||||
if nil == servlet {
|
if nil == servlet {
|
||||||
logging.Logger().Errorf(s.serverMessage("Servlet is nil"))
|
logging.Logger().Errorf("%s Servlet is nil", s.logHeader())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
servletCtx := servlet.ServletCtx(s.ctx)
|
servletCtx := servlet.ServletCtx(s.ctx)
|
||||||
if nil == servletCtx {
|
if nil == servletCtx {
|
||||||
logging.Logger().Errorf(s.serverMessage("ServletCtx is nil"))
|
logging.Logger().Errorf("%s ServletCtx is nil", s.logHeader())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := servlet.Handshake(servletCtx, netConn); nil != err {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,17 +33,17 @@ func (s *Server) ListenAndServe() error {
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
)
|
)
|
||||||
if nil == s.ServerHandler {
|
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()
|
s.ServerHandler.Validate()
|
||||||
|
|
||||||
if s.stopChan != nil {
|
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()
|
s.ctx = s.ServerHandler.ServerCtx()
|
||||||
if nil == s.ctx {
|
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{
|
s.hs = &fasthttp.Server{
|
||||||
|
@ -85,7 +85,7 @@ func (s *Server) ListenAndServe() error {
|
||||||
|
|
||||||
func (s *Server) Shutdown(ctx context.Context) error {
|
func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
if s.stopChan == nil {
|
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)
|
close(s.stopChan)
|
||||||
s.stopWg.Wait()
|
s.stopWg.Wait()
|
||||||
|
@ -97,8 +97,8 @@ func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) serverMessage(msg string) string {
|
func (s *Server) logHeader() string {
|
||||||
return fmt.Sprintf("Server[%s]: %s", s.ServerHandler.GetName(), msg)
|
return fmt.Sprintf("Server[%s]:", s.ServerHandler.GetName())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleServer(listener net.Listener) error {
|
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)
|
s.ServerHandler.OnStop(s.ctx)
|
||||||
|
|
||||||
logging.Logger().Infof(s.serverMessage("Stopped"))
|
logging.Logger().Infof("%s Stopped", s.logHeader())
|
||||||
|
|
||||||
s.stopWg.Done()
|
s.stopWg.Done()
|
||||||
}()
|
}()
|
||||||
|
@ -133,7 +133,7 @@ func (s *Server) handleServer(listener net.Listener) error {
|
||||||
hsCloseChan <- nil
|
hsCloseChan <- nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
logging.Logger().Infof(s.serverMessage("Started"))
|
logging.Logger().Infof("%s Started", s.logHeader())
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case err, _ := <-hsCloseChan:
|
case err, _ := <-hsCloseChan:
|
||||||
|
@ -159,7 +159,7 @@ func (s *Server) httpHandler(ctx *fasthttp.RequestCtx) {
|
||||||
if 0 < s.ServerHandler.GetConcurrency() {
|
if 0 < s.ServerHandler.GetConcurrency() {
|
||||||
sz := s.srw.ConnectionSize()
|
sz := s.srw.ConnectionSize()
|
||||||
if sz >= s.ServerHandler.GetConcurrency() {
|
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)
|
s.onError(ctx, fasthttp.StatusServiceUnavailable, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,17 +29,17 @@ func (s *Server) ListenAndServe() error {
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
)
|
)
|
||||||
if nil == s.ServerHandler {
|
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()
|
s.ServerHandler.Validate()
|
||||||
|
|
||||||
if s.stopChan != nil {
|
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()
|
s.ctx = s.ServerHandler.ServerCtx()
|
||||||
if nil == s.ctx {
|
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{
|
s.hs = &fasthttp.Server{
|
||||||
|
@ -68,7 +68,7 @@ func (s *Server) ListenAndServe() error {
|
||||||
|
|
||||||
func (s *Server) Shutdown(ctx context.Context) error {
|
func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
if s.stopChan == nil {
|
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)
|
close(s.stopChan)
|
||||||
s.stopWg.Wait()
|
s.stopWg.Wait()
|
||||||
|
@ -80,8 +80,8 @@ func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) serverMessage(msg string) string {
|
func (s *Server) logHeader() string {
|
||||||
return fmt.Sprintf("Server[%s]: %s", s.ServerHandler.GetName(), msg)
|
return fmt.Sprintf("Server[%s]:", s.ServerHandler.GetName())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleServer(listener net.Listener) error {
|
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)
|
s.ServerHandler.OnStop(s.ctx)
|
||||||
|
|
||||||
logging.Logger().Infof(s.serverMessage("Stopped"))
|
logging.Logger().Infof("%s Stopped", s.logHeader())
|
||||||
|
|
||||||
s.stopWg.Done()
|
s.stopWg.Done()
|
||||||
}()
|
}()
|
||||||
|
@ -116,7 +116,7 @@ func (s *Server) handleServer(listener net.Listener) error {
|
||||||
hsCloseChan <- nil
|
hsCloseChan <- nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
logging.Logger().Infof(s.serverMessage("Started"))
|
logging.Logger().Infof("%s Started", s.logHeader())
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case err, _ := <-hsCloseChan:
|
case err, _ := <-hsCloseChan:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user