server-go/net/server.go

170 lines
3.4 KiB
Go
Raw Normal View History

2018-04-04 04:01:26 +00:00
package net
import (
2018-04-04 13:28:35 +00:00
"context"
2018-04-04 04:01:26 +00:00
"fmt"
"net"
2018-04-04 13:28:35 +00:00
"sync"
2018-04-04 04:01:26 +00:00
"sync/atomic"
"time"
"git.loafle.net/commons/logging-go"
"git.loafle.net/commons/server-go"
)
2018-04-04 13:28:35 +00:00
type Server struct {
ServerHandler ServerHandler
2018-04-04 04:01:26 +00:00
2018-04-04 13:28:35 +00:00
ctx server.ServerCtx
stopChan chan struct{}
stopWg sync.WaitGroup
2018-04-04 04:01:26 +00:00
2018-04-04 13:28:35 +00:00
srwch server.ServerRWCHandler
2018-04-04 09:22:11 +00:00
}
2018-04-04 13:28:35 +00:00
func (s *Server) ListenAndServe() error {
if s.stopChan != nil {
return fmt.Errorf(s.serverMessage("already running. Stop it before starting it again"))
2018-04-04 04:01:26 +00:00
}
var (
err error
listener net.Listener
)
if nil == s.ServerHandler {
return fmt.Errorf("Server: server handler must be specified")
}
if err = s.ServerHandler.Validate(); nil != err {
return err
}
2018-04-04 13:28:35 +00:00
s.ctx = s.ServerHandler.ServerCtx()
if nil == s.ctx {
return fmt.Errorf(s.serverMessage("ServerCtx is nil"))
2018-04-04 04:01:26 +00:00
}
2018-04-04 13:28:35 +00:00
if err = s.ServerHandler.Init(s.ctx); nil != err {
2018-04-04 04:01:26 +00:00
return err
}
2018-04-04 13:28:35 +00:00
if listener, err = s.ServerHandler.Listener(s.ctx); nil != err {
2018-04-04 04:01:26 +00:00
return err
}
2018-04-04 13:28:35 +00:00
s.stopChan = make(chan struct{})
s.srwch.ReadwriteHandler = s.ServerHandler
s.srwch.ServerStopChan = s.stopChan
s.srwch.ServerStopWg = &s.stopWg
s.stopWg.Add(1)
2018-04-04 04:01:26 +00:00
return s.handleServer(listener)
}
2018-04-04 13:28:35 +00:00
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 {
2018-04-04 04:01:26 +00:00
var (
stopping atomic.Value
netConn net.Conn
err error
)
defer func() {
if nil != listener {
listener.Close()
}
2018-04-04 13:28:35 +00:00
s.ServerHandler.OnStop(s.ctx)
2018-04-04 04:01:26 +00:00
2018-04-04 13:28:35 +00:00
logging.Logger().Infof(s.serverMessage("Stopped"))
2018-04-04 04:01:26 +00:00
2018-04-04 13:28:35 +00:00
s.stopWg.Done()
2018-04-04 04:01:26 +00:00
}()
2018-04-04 13:28:35 +00:00
if err = s.ServerHandler.OnStart(s.ctx); nil != err {
2018-04-04 04:01:26 +00:00
return err
}
2018-04-04 13:28:35 +00:00
logging.Logger().Infof(s.serverMessage("Started"))
2018-04-04 04:01:26 +00:00
for {
acceptChan := make(chan struct{})
go func() {
if netConn, err = listener.Accept(); err != nil {
if nil == stopping.Load() {
2018-04-04 13:28:35 +00:00
logging.Logger().Errorf(s.serverMessage(fmt.Sprintf("%v", err)))
2018-04-04 04:01:26 +00:00
}
}
close(acceptChan)
}()
select {
2018-04-04 13:28:35 +00:00
case <-s.stopChan:
2018-04-04 04:01:26 +00:00
stopping.Store(true)
listener.Close()
<-acceptChan
listener = nil
return nil
case <-acceptChan:
}
if nil != err {
select {
2018-04-04 13:28:35 +00:00
case <-s.stopChan:
2018-04-04 04:01:26 +00:00
return nil
case <-time.After(time.Second):
}
continue
}
if 0 < s.ServerHandler.GetConcurrency() {
2018-04-04 13:28:35 +00:00
sz := s.srwch.ConnectionSize()
2018-04-04 04:01:26 +00:00
if sz >= s.ServerHandler.GetConcurrency() {
2018-04-04 13:28:35 +00:00
logging.Logger().Warnf(s.serverMessage(fmt.Sprintf("max connections size %d, refuse", sz)))
2018-04-04 04:01:26 +00:00
netConn.Close()
continue
}
}
2018-04-04 09:22:11 +00:00
servlet := s.ServerHandler.(ServerHandler).Servlet()
2018-04-04 04:01:26 +00:00
if nil == servlet {
2018-04-04 13:28:35 +00:00
logging.Logger().Errorf(s.serverMessage("Servlet is nil"))
2018-04-04 04:01:26 +00:00
continue
}
2018-04-04 13:28:35 +00:00
servletCtx := servlet.ServletCtx(s.ctx)
2018-04-04 04:01:26 +00:00
if nil == servletCtx {
2018-04-04 13:28:35 +00:00
logging.Logger().Errorf(s.serverMessage("ServletCtx is nil"))
2018-04-04 04:01:26 +00:00
continue
}
if err := servlet.Handshake(servletCtx, netConn); nil != err {
2018-04-04 13:28:35 +00:00
logging.Logger().Infof(s.serverMessage(fmt.Sprintf("Handshaking of Client[%s] has been failed %v", netConn.RemoteAddr(), err)))
2018-04-04 04:01:26 +00:00
continue
}
2018-04-04 05:47:10 +00:00
conn := server.NewConn(netConn, true, s.ServerHandler.GetReadBufferSize(), s.ServerHandler.GetWriteBufferSize())
2018-04-04 04:01:26 +00:00
2018-04-04 13:28:35 +00:00
s.stopWg.Add(1)
go s.srwch.HandleConnection(servlet, servletCtx, conn)
2018-04-04 04:01:26 +00:00
}
}