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