2018-04-05 15:15:29 +00:00
|
|
|
package web
|
2018-04-04 04:01:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
2018-04-06 01:18:19 +00:00
|
|
|
"sync/atomic"
|
2018-04-04 04:01:26 +00:00
|
|
|
|
|
|
|
"git.loafle.net/commons/logging-go"
|
|
|
|
"git.loafle.net/commons/server-go"
|
2018-04-05 15:15:29 +00:00
|
|
|
"git.loafle.net/commons/server-go/socket"
|
2018-04-04 04:01:26 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
ServerHandler ServerHandler
|
|
|
|
|
2018-04-04 13:28:35 +00:00
|
|
|
ctx server.ServerCtx
|
|
|
|
stopChan chan struct{}
|
|
|
|
stopWg sync.WaitGroup
|
|
|
|
|
2018-04-05 15:15:29 +00:00
|
|
|
srw socket.ServerReadWriter
|
2018-04-04 04:01:26 +00:00
|
|
|
|
|
|
|
hs *fasthttp.Server
|
|
|
|
upgrader *Upgrader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ListenAndServe() error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
listener net.Listener
|
|
|
|
)
|
|
|
|
if nil == s.ServerHandler {
|
2018-04-12 01:43:43 +00:00
|
|
|
return fmt.Errorf("%s server handler must be specified", s.logHeader())
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|
|
|
|
s.ServerHandler.Validate()
|
|
|
|
|
|
|
|
if s.stopChan != nil {
|
2018-04-12 01:43:43 +00:00
|
|
|
return fmt.Errorf("%s already running. Stop it before starting it again", s.logHeader())
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.ctx = s.ServerHandler.ServerCtx()
|
|
|
|
if nil == s.ctx {
|
2018-04-12 01:43:43 +00:00
|
|
|
return fmt.Errorf("%s ServerCtx is nil", s.logHeader())
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.hs = &fasthttp.Server{
|
|
|
|
Handler: s.httpHandler,
|
|
|
|
Name: s.ServerHandler.GetName(),
|
|
|
|
Concurrency: s.ServerHandler.GetConcurrency(),
|
|
|
|
ReadBufferSize: s.ServerHandler.GetReadBufferSize(),
|
|
|
|
WriteBufferSize: s.ServerHandler.GetWriteBufferSize(),
|
|
|
|
ReadTimeout: s.ServerHandler.GetReadTimeout(),
|
|
|
|
WriteTimeout: s.ServerHandler.GetWriteTimeout(),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.upgrader = &Upgrader{
|
|
|
|
HandshakeTimeout: s.ServerHandler.GetHandshakeTimeout(),
|
|
|
|
ReadBufferSize: s.ServerHandler.GetReadBufferSize(),
|
|
|
|
WriteBufferSize: s.ServerHandler.GetWriteBufferSize(),
|
2018-04-04 13:28:35 +00:00
|
|
|
CheckOrigin: s.ServerHandler.(ServerHandler).CheckOrigin,
|
2018-04-04 04:01:26 +00:00
|
|
|
Error: s.onError,
|
|
|
|
EnableCompression: s.ServerHandler.IsEnableCompression(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.ServerHandler.Init(s.ctx); nil != err {
|
2018-05-03 10:17:44 +00:00
|
|
|
logging.Logger().Errorf("%s Init has been failed %v", s.logHeader(), err)
|
2018-04-04 04:01:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if listener, err = s.ServerHandler.Listener(s.ctx); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.stopChan = make(chan struct{})
|
2018-04-04 13:28:35 +00:00
|
|
|
|
2018-04-04 13:56:14 +00:00
|
|
|
s.srw.ReadwriteHandler = s.ServerHandler
|
|
|
|
s.srw.ServerStopChan = s.stopChan
|
|
|
|
s.srw.ServerStopWg = &s.stopWg
|
2018-04-04 13:28:35 +00:00
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
s.stopWg.Add(1)
|
|
|
|
return s.handleServer(listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Shutdown(ctx context.Context) error {
|
|
|
|
if s.stopChan == nil {
|
2018-04-12 01:43:43 +00:00
|
|
|
return fmt.Errorf("%s must be started before stopping it", s.logHeader())
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|
|
|
|
close(s.stopChan)
|
|
|
|
s.stopWg.Wait()
|
|
|
|
|
|
|
|
s.ServerHandler.Destroy(s.ctx)
|
|
|
|
|
|
|
|
s.stopChan = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-12 01:43:43 +00:00
|
|
|
func (s *Server) logHeader() string {
|
|
|
|
return fmt.Sprintf("Server[%s]:", s.ServerHandler.GetName())
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleServer(listener net.Listener) error {
|
|
|
|
var (
|
2018-04-06 01:18:19 +00:00
|
|
|
err error
|
|
|
|
stopping atomic.Value
|
2018-04-04 04:01:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if nil != listener {
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
s.ServerHandler.OnStop(s.ctx)
|
|
|
|
|
2018-04-12 01:43:43 +00:00
|
|
|
logging.Logger().Infof("%s Stopped", s.logHeader())
|
2018-04-06 01:18:19 +00:00
|
|
|
|
|
|
|
s.stopWg.Done()
|
2018-04-04 04:01:26 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
if err = s.ServerHandler.OnStart(s.ctx); nil != err {
|
2018-05-03 10:17:44 +00:00
|
|
|
logging.Logger().Errorf("%s OnStart has been failed %v", s.logHeader(), err)
|
2018-04-04 04:01:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-06 01:18:19 +00:00
|
|
|
hsCloseChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
if err := s.hs.Serve(listener); nil != err {
|
|
|
|
if nil == stopping.Load() {
|
|
|
|
hsCloseChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hsCloseChan <- nil
|
|
|
|
}()
|
|
|
|
|
2018-04-12 01:43:43 +00:00
|
|
|
logging.Logger().Infof("%s Started", s.logHeader())
|
2018-04-04 04:01:26 +00:00
|
|
|
|
|
|
|
select {
|
2018-04-06 01:18:19 +00:00
|
|
|
case err, _ := <-hsCloseChan:
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-04 04:01:26 +00:00
|
|
|
case <-s.stopChan:
|
2018-04-06 01:18:19 +00:00
|
|
|
stopping.Store(true)
|
2018-04-04 04:01:26 +00:00
|
|
|
listener.Close()
|
2018-04-06 01:18:19 +00:00
|
|
|
<-hsCloseChan
|
2018-04-04 04:01:26 +00:00
|
|
|
listener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) httpHandler(ctx *fasthttp.RequestCtx) {
|
|
|
|
var (
|
|
|
|
servlet Servlet
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if 0 < s.ServerHandler.GetConcurrency() {
|
2018-04-04 13:56:14 +00:00
|
|
|
sz := s.srw.ConnectionSize()
|
2018-04-04 04:01:26 +00:00
|
|
|
if sz >= s.ServerHandler.GetConcurrency() {
|
2018-04-12 01:43:43 +00:00
|
|
|
logging.Logger().Warnf("%s max connections size %d, refuse", s.logHeader(), sz)
|
2018-04-04 04:01:26 +00:00
|
|
|
s.onError(ctx, fasthttp.StatusServiceUnavailable, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 16:21:13 +00:00
|
|
|
if servlet = s.ServerHandler.Servlet(s.ctx, ctx); nil == servlet {
|
2018-04-04 04:01:26 +00:00
|
|
|
s.onError(ctx, fasthttp.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseHeader *fasthttp.ResponseHeader
|
|
|
|
servletCtx := servlet.ServletCtx(s.ctx)
|
|
|
|
|
|
|
|
if responseHeader, err = servlet.Handshake(servletCtx, ctx); nil != err {
|
|
|
|
s.onError(ctx, http.StatusNotAcceptable, fmt.Errorf("Handshake err: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-05 15:15:29 +00:00
|
|
|
s.upgrader.Upgrade(ctx, responseHeader, func(conn *socket.SocketConn, err error) {
|
2018-04-04 04:01:26 +00:00
|
|
|
if err != nil {
|
|
|
|
s.onError(ctx, fasthttp.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.stopWg.Add(1)
|
2018-04-06 07:20:19 +00:00
|
|
|
s.srw.HandleConnection(servlet, servletCtx, conn)
|
2018-04-04 08:36:23 +00:00
|
|
|
})
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) onError(ctx *fasthttp.RequestCtx, status int, reason error) {
|
2018-04-04 13:28:35 +00:00
|
|
|
s.ServerHandler.(ServerHandler).OnError(s.ctx, ctx, status, reason)
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|