server/server.go

153 lines
2.8 KiB
Go
Raw Normal View History

2017-10-26 11:14:00 +00:00
package server
import (
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"git.loafle.net/commons_go/logging"
)
2017-11-01 06:24:05 +00:00
func New(sh ServerHandler) Server {
2017-10-26 12:13:48 +00:00
s := &server{
2017-10-31 10:46:32 +00:00
sh: sh,
2017-10-26 12:13:48 +00:00
}
return s
}
2017-11-15 04:13:47 +00:00
type Server interface {
Start() error
Stop()
2017-11-29 01:01:48 +00:00
Context() ServerContext
2017-11-15 04:13:47 +00:00
}
2017-10-26 11:14:00 +00:00
type server struct {
2017-11-29 01:01:48 +00:00
ctx ServerContext
2017-10-31 10:46:32 +00:00
sh ServerHandler
2017-10-26 11:14:00 +00:00
listener net.Listener
2017-11-01 05:22:52 +00:00
Stats ServerStats
2017-10-26 11:14:00 +00:00
stopChan chan struct{}
stopWg sync.WaitGroup
}
func (s *server) Start() error {
2017-10-31 10:46:32 +00:00
if nil == s.sh {
2017-10-26 11:14:00 +00:00
panic("Server: server handler must be specified.")
}
2017-10-31 10:46:32 +00:00
s.sh.Validate()
2017-10-26 11:14:00 +00:00
if s.stopChan != nil {
panic("Server: server is already running. Stop it before starting it again")
}
var err error
2017-11-29 01:01:48 +00:00
s.ctx = s.sh.ServerContext()
2017-10-26 11:14:00 +00:00
2017-11-29 01:01:48 +00:00
if err = s.sh.Init(s.ctx); nil != err {
2017-11-15 04:13:47 +00:00
logging.Logger().Panic(fmt.Sprintf("Server: Initialization of server has been failed %v", err))
}
2017-11-29 01:01:48 +00:00
if s.listener, err = s.sh.Listen(s.ctx); nil != err {
return err
}
2017-11-01 05:22:52 +00:00
2017-11-29 01:01:48 +00:00
s.stopChan = make(chan struct{})
2017-10-27 08:53:26 +00:00
2017-10-26 11:14:00 +00:00
s.stopWg.Add(1)
2017-11-01 05:22:52 +00:00
go handleServer(s)
2017-10-27 08:53:26 +00:00
2017-10-26 11:14:00 +00:00
return nil
}
func (s *server) Stop() {
if s.stopChan == nil {
panic("Server: server must be started before stopping it")
}
close(s.stopChan)
s.stopWg.Wait()
s.stopChan = nil
2017-11-15 04:13:47 +00:00
2017-10-31 10:46:32 +00:00
s.sh.OnStop()
2017-11-15 04:13:47 +00:00
logging.Logger().Info(fmt.Sprintf("Server[%s] is stopped", s.sh.GetName()))
2017-10-26 11:14:00 +00:00
}
2017-11-01 05:22:52 +00:00
func handleServer(s *server) {
2017-10-26 11:14:00 +00:00
defer s.stopWg.Done()
2017-11-29 01:01:48 +00:00
logging.Logger().Info(fmt.Sprintf("Server[%s] is started", s.sh.GetName()))
s.sh.OnStart(s.ctx)
2017-11-01 05:22:52 +00:00
var conn net.Conn
2017-10-26 11:14:00 +00:00
var err error
var stopping atomic.Value
for {
acceptChan := make(chan struct{})
go func() {
2017-11-01 05:22:52 +00:00
if conn, err = s.listener.Accept(); err != nil {
2017-10-26 11:14:00 +00:00
if stopping.Load() == nil {
2017-11-09 06:15:00 +00:00
logging.Logger().Error(fmt.Sprintf("Server: Cannot accept new connection: [%s]", err))
2017-10-26 11:14:00 +00:00
}
}
close(acceptChan)
}()
select {
case <-s.stopChan:
stopping.Store(true)
s.listener.Close()
<-acceptChan
return
case <-acceptChan:
s.Stats.incAcceptCalls()
}
if err != nil {
s.Stats.incAcceptErrors()
select {
case <-s.stopChan:
return
case <-time.After(time.Second):
}
continue
}
s.stopWg.Add(1)
2017-11-01 05:22:52 +00:00
go handleConnection(s, conn)
2017-10-26 11:14:00 +00:00
}
}
2017-11-01 05:22:52 +00:00
func handleConnection(s *server, conn net.Conn) {
2017-10-26 11:14:00 +00:00
defer s.stopWg.Done()
2017-10-27 07:14:20 +00:00
2017-11-15 04:13:47 +00:00
var err error
if conn, err = s.sh.OnConnect(conn); nil != err {
logging.Logger().Error(fmt.Sprintf("Server: connecting[%s] failed %v", conn.RemoteAddr(), err))
return
}
2017-11-09 06:15:00 +00:00
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", conn.RemoteAddr()))
2017-11-15 04:13:47 +00:00
2017-10-27 05:33:35 +00:00
clientStopChan := make(chan struct{})
2017-11-07 02:57:26 +00:00
handleDoneChan := make(chan struct{})
2017-11-15 04:13:47 +00:00
2017-11-07 02:57:26 +00:00
go s.sh.Handle(conn, clientStopChan, handleDoneChan)
2017-10-26 11:14:00 +00:00
select {
case <-s.stopChan:
2017-10-27 05:33:35 +00:00
close(clientStopChan)
2017-10-26 11:14:00 +00:00
conn.Close()
2017-11-07 02:57:26 +00:00
<-handleDoneChan
case <-handleDoneChan:
2017-11-01 05:22:52 +00:00
close(clientStopChan)
2017-10-27 03:47:10 +00:00
conn.Close()
2017-11-09 06:15:00 +00:00
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is disconnected.", conn.RemoteAddr()))
2017-10-26 11:14:00 +00:00
}
}