server/server.go

143 lines
2.4 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"
)
type Server interface {
Start() error
Stop()
Serve() error
}
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-10-26 11:14:00 +00:00
type server struct {
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-10-31 10:46:32 +00:00
if s.listener, err = s.sh.Listen(); nil != err {
2017-10-26 11:14:00 +00:00
return err
}
2017-11-01 05:22:52 +00:00
s.stopChan = make(chan struct{})
2017-10-31 10:46:32 +00:00
s.sh.OnStart()
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-10-31 10:46:32 +00:00
s.sh.OnStop()
2017-10-26 11:14:00 +00:00
}
func (s *server) Serve() error {
if err := s.Start(); err != nil {
return err
}
s.stopWg.Wait()
return nil
}
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-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
}
2017-11-01 05:22:52 +00:00
} else {
conn, err = s.sh.OnAccept(conn)
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-09 06:15:00 +00:00
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", conn.RemoteAddr()))
2017-10-27 05:33:35 +00:00
clientStopChan := make(chan struct{})
2017-11-07 02:57:26 +00:00
handleDoneChan := make(chan struct{})
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
}
}