server-go/net/server.go
crusader 899fc53ded ing
2018-04-05 01:51:34 +09:00

167 lines
3.4 KiB
Go

package net
import (
"context"
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"git.loafle.net/commons/logging-go"
"git.loafle.net/commons/server-go"
)
type Server struct {
ServerHandler ServerHandler
ctx server.ServerCtx
stopChan chan struct{}
stopWg sync.WaitGroup
srw server.ServerReadWriter
}
func (s *Server) ListenAndServe() error {
if s.stopChan != nil {
return fmt.Errorf(s.serverMessage("already running. Stop it before starting it again"))
}
var (
err error
listener net.Listener
)
if nil == s.ServerHandler {
return fmt.Errorf(s.serverMessage("server handler must be specified"))
}
if err = s.ServerHandler.Validate(); nil != err {
return err
}
s.ctx = s.ServerHandler.ServerCtx()
if nil == s.ctx {
return fmt.Errorf(s.serverMessage("ServerCtx is nil"))
}
if err = s.ServerHandler.Init(s.ctx); nil != err {
return err
}
if listener, err = s.ServerHandler.Listener(s.ctx); nil != err {
return err
}
s.stopChan = make(chan struct{})
s.srw.ReadwriteHandler = s.ServerHandler
s.srw.ServerStopChan = s.stopChan
s.srw.ServerStopWg = &s.stopWg
s.stopWg.Add(1)
return s.handleServer(listener)
}
func (s *Server) Shutdown(ctx context.Context) error {
if s.stopChan == nil {
return fmt.Errorf("server must be started before stopping it")
}
close(s.stopChan)
s.stopWg.Wait()
s.ServerHandler.Destroy(s.ctx)
s.stopChan = nil
return nil
}
func (s *Server) serverMessage(msg string) string {
return fmt.Sprintf("Server[%s]: %s", s.ServerHandler.GetName(), msg)
}
func (s *Server) handleServer(listener net.Listener) error {
var (
stopping atomic.Value
netConn net.Conn
err error
)
defer func() {
if nil != listener {
listener.Close()
}
s.ServerHandler.OnStop(s.ctx)
logging.Logger().Infof(s.serverMessage("Stopped"))
s.stopWg.Done()
}()
if err = s.ServerHandler.OnStart(s.ctx); nil != err {
return err
}
logging.Logger().Infof(s.serverMessage("Started"))
for {
acceptChan := make(chan struct{})
go func() {
if netConn, err = listener.Accept(); err != nil {
if nil == stopping.Load() {
logging.Logger().Errorf(s.serverMessage(fmt.Sprintf("%v", err)))
}
}
close(acceptChan)
}()
select {
case <-s.stopChan:
stopping.Store(true)
listener.Close()
<-acceptChan
listener = nil
return nil
case <-acceptChan:
}
if nil != err {
select {
case <-s.stopChan:
return nil
case <-time.After(time.Second):
}
continue
}
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)))
netConn.Close()
continue
}
}
servlet := s.ServerHandler.(ServerHandler).Servlet(s.ctx, netConn)
if nil == servlet {
logging.Logger().Errorf(s.serverMessage("Servlet is nil"))
continue
}
servletCtx := servlet.ServletCtx(s.ctx)
if nil == servletCtx {
logging.Logger().Errorf(s.serverMessage("ServletCtx is nil"))
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)))
continue
}
conn := server.NewConn(netConn, true, s.ServerHandler.GetReadBufferSize(), s.ServerHandler.GetWriteBufferSize())
s.stopWg.Add(1)
go s.srw.HandleConnection(servlet, servletCtx, conn)
}
}