This commit is contained in:
crusader 2017-11-15 13:13:47 +09:00
parent 07300537c2
commit faa9bd4567
3 changed files with 85 additions and 17 deletions

View File

@ -10,12 +10,6 @@ import (
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
) )
type Server interface {
Start() error
Stop()
Serve() error
}
func New(sh ServerHandler) Server { func New(sh ServerHandler) Server {
s := &server{ s := &server{
sh: sh, sh: sh,
@ -23,6 +17,12 @@ func New(sh ServerHandler) Server {
return s return s
} }
type Server interface {
Start() error
Stop()
Serve() error
}
type server struct { type server struct {
sh ServerHandler sh ServerHandler
@ -48,6 +48,10 @@ func (s *server) Start() error {
return err return err
} }
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.sh.OnStart() s.sh.OnStart()
@ -65,7 +69,10 @@ func (s *server) Stop() {
close(s.stopChan) close(s.stopChan)
s.stopWg.Wait() s.stopWg.Wait()
s.stopChan = nil s.stopChan = nil
s.sh.OnStop() s.sh.OnStop()
logging.Logger().Info(fmt.Sprintf("Server[%s] is stopped", s.sh.GetName()))
} }
func (s *server) Serve() error { func (s *server) Serve() error {
@ -90,8 +97,6 @@ func handleServer(s *server) {
if stopping.Load() == nil { if stopping.Load() == nil {
logging.Logger().Error(fmt.Sprintf("Server: Cannot accept new connection: [%s]", err)) logging.Logger().Error(fmt.Sprintf("Server: Cannot accept new connection: [%s]", err))
} }
} else {
conn, err = s.sh.OnAccept(conn)
} }
close(acceptChan) close(acceptChan)
}() }()
@ -124,9 +129,17 @@ func handleServer(s *server) {
func handleConnection(s *server, conn net.Conn) { func handleConnection(s *server, conn net.Conn) {
defer s.stopWg.Done() defer s.stopWg.Done()
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
}
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", conn.RemoteAddr())) logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", conn.RemoteAddr()))
clientStopChan := make(chan struct{}) clientStopChan := make(chan struct{})
handleDoneChan := make(chan struct{}) handleDoneChan := make(chan struct{})
go s.sh.Handle(conn, clientStopChan, handleDoneChan) go s.sh.Handle(conn, clientStopChan, handleDoneChan)
select { select {

View File

@ -5,13 +5,44 @@ import (
) )
type ServerHandler interface { type ServerHandler interface {
Listen() (net.Listener, error) // Init invoked before the server is started
OnAccept(conn net.Conn) (net.Conn, error) // If you override ths method, must call
//
// func (sh *ServerHandler) Init() error {
// if err := sh.ServerHandler.Init(); nil != err {
// return err
// }
// ...
// return nil
// }
Init() error
Listen() (net.Listener, error)
// OnStart invoked when server is started
// If you override ths method, must call
//
// func (sh *ServerHandler) OnStart() error {
// sh.ServerHandler.OnStart()
// ...
// return nil
// }
OnStart() OnStart()
OnStop()
OnConnect(conn net.Conn) (net.Conn, error)
Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{})
OnError(status int, reason error)
// OnStop invoked when server is stopped
// If you override ths method, must call
//
// func (sh *ServerHandler) OnStop() {
// ...
// sh.ServerHandler.OnStop()
// }
OnStop()
GetName() string
Validate() Validate()
} }

View File

@ -2,26 +2,36 @@ package server
import ( import (
"errors" "errors"
"fmt"
"log" "log"
"net" "net"
"git.loafle.net/commons_go/logging"
) )
type ServerHandlers struct { type ServerHandlers struct {
// Server name for sending in response headers.
//
// Default server name is used if left blank.
Name string
} }
func (sh *ServerHandlers) OnStart() { func (sh *ServerHandlers) Init() error {
// no op
}
func (sh *ServerHandlers) OnStop() { return nil
// no op
} }
func (sh *ServerHandlers) Listen() (net.Listener, error) { func (sh *ServerHandlers) Listen() (net.Listener, error) {
return nil, errors.New("Server: Handler method[Listen] of Server is not implement") return nil, errors.New("Server: Handler method[Listen] of Server is not implement")
} }
func (sh *ServerHandlers) OnAccept(conn net.Conn) (net.Conn, error) { // OnStart invoked when server is stated
// If you override ths method, must call
func (sh *ServerHandlers) OnStart() {
// no op
}
func (sh *ServerHandlers) OnConnect(conn net.Conn) (net.Conn, error) {
return conn, nil return conn, nil
} }
@ -29,6 +39,20 @@ func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneCh
log.Printf("Server.Handle") log.Printf("Server.Handle")
} }
func (sh *ServerHandlers) OnError(status int, reason error) {
logging.Logger().Error(fmt.Sprintf("Server: error status: %d, reason: %v", status, reason))
}
// OnStop invoked when server is stopped
// If you override ths method, must call
func (sh *ServerHandlers) OnStop() {
// no op
}
func (sh *ServerHandlers) GetName() string {
return sh.Name
}
func (sh *ServerHandlers) Validate() { func (sh *ServerHandlers) Validate() {
} }