This commit is contained in:
crusader 2017-11-27 19:05:59 +09:00
parent baa53ae03c
commit dff228d0db

View File

@ -7,6 +7,7 @@ import (
"sync"
"git.loafle.net/commons_go/logging"
cuc "git.loafle.net/commons_go/util/context"
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
"github.com/valyala/fasthttp"
@ -15,16 +16,21 @@ import (
type Server interface {
Start() error
Stop()
Attributes() cuc.Attributes
}
func New(sh ServerHandler) Server {
s := &server{
sh: sh,
}
return s
}
type server struct {
attributes cuc.Attributes
sh ServerHandler
httpServer *fasthttp.Server
@ -65,18 +71,19 @@ func (s *server) Start() error {
}
var err error
if err = s.sh.Init(); nil != err {
logging.Logger().Panic(fmt.Sprintf("Server: Initialization of server has been failed %v", err))
}
var listener net.Listener
if listener, err = s.sh.Listen(); nil != err {
return err
}
s.listener = newGracefulListener(listener, s.sh.GetMaxStopWaitTime())
if err = s.sh.Init(); nil != err {
logging.Logger().Panic(fmt.Sprintf("Server: Initialization of server has been failed %v", err))
}
s.stopChan = make(chan struct{})
s.attributes = cuc.NewAttributes(nil)
s.stopWg.Add(1)
go handleServer(s)
@ -92,11 +99,17 @@ func (s *server) Stop() {
s.stopWg.Wait()
s.stopChan = nil
s.attributes.Destroy()
s.sh.OnStop()
logging.Logger().Info(fmt.Sprintf("Server[%s] is stopped", s.sh.GetName()))
}
func (s *server) Attributes() cuc.Attributes {
return s.attributes
}
func handleServer(s *server) {
go func() {