From 3a437b10a46a2e81522b0668e5d925f46b24bb9b Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 7 Nov 2017 11:57:26 +0900 Subject: [PATCH 1/6] ing --- server.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server.go b/server.go index e2c4800..0f25708 100644 --- a/server.go +++ b/server.go @@ -126,15 +126,15 @@ func handleConnection(s *server, conn net.Conn) { logging.Logger.Debug(fmt.Sprintf("Server: Client[%s] is connected.", conn.RemoteAddr())) clientStopChan := make(chan struct{}) - handleDoneCnah := make(chan struct{}) - go s.sh.Handle(conn, clientStopChan, handleDoneCnah) + handleDoneChan := make(chan struct{}) + go s.sh.Handle(conn, clientStopChan, handleDoneChan) select { case <-s.stopChan: close(clientStopChan) conn.Close() - <-handleDoneCnah - case <-handleDoneCnah: + <-handleDoneChan + case <-handleDoneChan: close(clientStopChan) conn.Close() logging.Logger.Debug(fmt.Sprintf("Server: Client[%s] is disconnected.", conn.RemoteAddr())) From 5edc46cefea6647c348b4463801408c1c20f3291 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 7 Nov 2017 17:38:50 +0900 Subject: [PATCH 2/6] ing --- server_handler.go | 2 -- server_handlers.go | 5 ----- util.go | 7 +++++++ 3 files changed, 7 insertions(+), 7 deletions(-) create mode 100644 util.go diff --git a/server_handler.go b/server_handler.go index 3ed7127..cd9f06a 100644 --- a/server_handler.go +++ b/server_handler.go @@ -13,7 +13,5 @@ type ServerHandler interface { Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) - IsClientDisconnect(err error) bool - Validate() } diff --git a/server_handlers.go b/server_handlers.go index fe7e2b7..6a2e46c 100644 --- a/server_handlers.go +++ b/server_handlers.go @@ -2,7 +2,6 @@ package server import ( "errors" - "io" "log" "net" ) @@ -30,10 +29,6 @@ func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneCh log.Printf("Server.Handle") } -func (sh *ServerHandlers) IsClientDisconnect(err error) bool { - return err == io.ErrUnexpectedEOF || err == io.EOF -} - func (sh *ServerHandlers) Validate() { } diff --git a/util.go b/util.go new file mode 100644 index 0000000..ecb2790 --- /dev/null +++ b/util.go @@ -0,0 +1,7 @@ +package server + +import "io" + +func IsClientDisconnect(err error) bool { + return err == io.ErrUnexpectedEOF || err == io.EOF +} From 07300537c23245ffe16d5791c15cb55864289637 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 9 Nov 2017 15:15:00 +0900 Subject: [PATCH 3/6] ing --- server.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server.go b/server.go index 0f25708..a229bfe 100644 --- a/server.go +++ b/server.go @@ -88,7 +88,7 @@ func handleServer(s *server) { go func() { if conn, err = s.listener.Accept(); err != 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) @@ -124,7 +124,7 @@ func handleServer(s *server) { func handleConnection(s *server, conn net.Conn) { defer s.stopWg.Done() - 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{}) handleDoneChan := make(chan struct{}) go s.sh.Handle(conn, clientStopChan, handleDoneChan) @@ -137,6 +137,6 @@ func handleConnection(s *server, conn net.Conn) { case <-handleDoneChan: close(clientStopChan) conn.Close() - logging.Logger.Debug(fmt.Sprintf("Server: Client[%s] is disconnected.", conn.RemoteAddr())) + logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is disconnected.", conn.RemoteAddr())) } } From faa9bd4567062953f0fe6d462a05aec23886ec50 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 15 Nov 2017 13:13:47 +0900 Subject: [PATCH 4/6] ing --- server.go | 29 +++++++++++++++++++++-------- server_handler.go | 37 ++++++++++++++++++++++++++++++++++--- server_handlers.go | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/server.go b/server.go index a229bfe..122dd53 100644 --- a/server.go +++ b/server.go @@ -10,12 +10,6 @@ import ( "git.loafle.net/commons_go/logging" ) -type Server interface { - Start() error - Stop() - Serve() error -} - func New(sh ServerHandler) Server { s := &server{ sh: sh, @@ -23,6 +17,12 @@ func New(sh ServerHandler) Server { return s } +type Server interface { + Start() error + Stop() + Serve() error +} + type server struct { sh ServerHandler @@ -48,6 +48,10 @@ func (s *server) Start() error { 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.sh.OnStart() @@ -65,7 +69,10 @@ func (s *server) Stop() { close(s.stopChan) s.stopWg.Wait() s.stopChan = nil + s.sh.OnStop() + + logging.Logger().Info(fmt.Sprintf("Server[%s] is stopped", s.sh.GetName())) } func (s *server) Serve() error { @@ -90,8 +97,6 @@ func handleServer(s *server) { if stopping.Load() == nil { logging.Logger().Error(fmt.Sprintf("Server: Cannot accept new connection: [%s]", err)) } - } else { - conn, err = s.sh.OnAccept(conn) } close(acceptChan) }() @@ -124,9 +129,17 @@ func handleServer(s *server) { func handleConnection(s *server, conn net.Conn) { 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())) + clientStopChan := make(chan struct{}) handleDoneChan := make(chan struct{}) + go s.sh.Handle(conn, clientStopChan, handleDoneChan) select { diff --git a/server_handler.go b/server_handler.go index cd9f06a..de433c8 100644 --- a/server_handler.go +++ b/server_handler.go @@ -5,13 +5,44 @@ import ( ) type ServerHandler interface { - Listen() (net.Listener, error) - OnAccept(conn net.Conn) (net.Conn, error) + // Init invoked before the server is started + // 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() - OnStop() + + OnConnect(conn net.Conn) (net.Conn, error) 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() } diff --git a/server_handlers.go b/server_handlers.go index 6a2e46c..2d3788c 100644 --- a/server_handlers.go +++ b/server_handlers.go @@ -2,26 +2,36 @@ package server import ( "errors" + "fmt" "log" "net" + + "git.loafle.net/commons_go/logging" ) type ServerHandlers struct { + // Server name for sending in response headers. + // + // Default server name is used if left blank. + Name string } -func (sh *ServerHandlers) OnStart() { - // no op -} +func (sh *ServerHandlers) Init() error { -func (sh *ServerHandlers) OnStop() { - // no op + return nil } func (sh *ServerHandlers) Listen() (net.Listener, error) { 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 } @@ -29,6 +39,20 @@ func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneCh 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() { } From 147477117cdda101d132d75588aeae5efee5ff3e Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 15 Nov 2017 13:16:31 +0900 Subject: [PATCH 5/6] ing --- server_handlers.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server_handlers.go b/server_handlers.go index 2d3788c..66d5e9d 100644 --- a/server_handlers.go +++ b/server_handlers.go @@ -9,6 +9,11 @@ import ( "git.loafle.net/commons_go/logging" ) +func NewServerHandler() ServerHandler { + sh := &ServerHandlers{} + return sh +} + type ServerHandlers struct { // Server name for sending in response headers. // From 36cab4575aa8e514ff98792acf418cf63a26085c Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 15 Nov 2017 13:20:59 +0900 Subject: [PATCH 6/6] ing --- server_handlers.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/server_handlers.go b/server_handlers.go index 66d5e9d..c07ba63 100644 --- a/server_handlers.go +++ b/server_handlers.go @@ -3,7 +3,6 @@ package server import ( "errors" "fmt" - "log" "net" "git.loafle.net/commons_go/logging" @@ -41,7 +40,6 @@ func (sh *ServerHandlers) OnConnect(conn net.Conn) (net.Conn, error) { } func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) { - log.Printf("Server.Handle") } func (sh *ServerHandlers) OnError(status int, reason error) {