websocket_fasthttp/server_handlers.go
crusader 4522a80579 ing
2017-11-27 23:30:18 +09:00

181 lines
4.4 KiB
Go

package websocket_fasthttp
import (
"errors"
"fmt"
"net"
"time"
"git.loafle.net/commons_go/logging"
"github.com/valyala/fasthttp"
)
type ServerHandlers struct {
// Server name for sending in response headers.
//
// Default server name is used if left blank.
Name string
// The maximum number of concurrent connections the server may serve.
//
// DefaultConcurrency is used if not set.
Concurrency int
MaxStopWaitTime time.Duration
HandshakeTimeout time.Duration
// Per-connection buffer size for requests' reading.
// This also limits the maximum header size.
//
// Increase this buffer if your clients send multi-KB RequestURIs
// and/or multi-KB headers (for example, BIG cookies).
//
// Default buffer size is used if not set.
ReadBufferSize int
// Per-connection buffer size for responses' writing.
//
// Default buffer size is used if not set.
WriteBufferSize int
// Maximum duration for reading the full request (including body).
//
// This also limits the maximum duration for idle keep-alive
// connections.
//
// By default request read timeout is unlimited.
ReadTimeout time.Duration
// Maximum duration for writing the full response (including body).
//
// By default response write timeout is unlimited.
WriteTimeout time.Duration
EnableCompression bool
socketHandlers map[string]SocketHandler
}
func (sh *ServerHandlers) ServerContext() ServerContext {
return newServerContext()
}
func (sh *ServerHandlers) Init(serverCTX ServerContext) error {
if nil != sh.socketHandlers {
for _, socketHandler := range sh.socketHandlers {
if err := socketHandler.Init(serverCTX); nil != err {
return err
}
}
}
return nil
}
func (sh *ServerHandlers) Listen(serverCTX ServerContext) (net.Listener, error) {
return nil, errors.New("Server: Handler method[Listen] of Server is not implement")
}
// OnStart invoked when server is stated
// If you override ths method, must call
func (sh *ServerHandlers) OnStart(serverCTX ServerContext) {
// no op
}
func (sh *ServerHandlers) CheckOrigin(ctx *fasthttp.RequestCtx) bool {
return true
}
func (sh *ServerHandlers) OnError(serverCTX ServerContext, ctx *fasthttp.RequestCtx, status int, reason error) {
logging.Logger().Error(fmt.Sprintf("Server: error status: %d, reason: %v, [%v]", status, reason, ctx))
}
// OnStop invoked when server is stopped
// If you override ths method, must call
func (sh *ServerHandlers) OnStop(serverCTX ServerContext) {
if nil != sh.socketHandlers {
for _, socketHandler := range sh.socketHandlers {
socketHandler.Destroy()
}
}
}
func (sh *ServerHandlers) RegisterSocketHandler(path string, handler SocketHandler) {
if nil == sh.socketHandlers {
sh.socketHandlers = make(map[string]SocketHandler)
}
sh.socketHandlers[path] = handler
}
func (sh *ServerHandlers) GetSocketHandler(path string) (SocketHandler, error) {
var handler SocketHandler
if path == "" && len(sh.socketHandlers) == 1 {
for _, h := range sh.socketHandlers {
handler = h
}
} else if handler = sh.socketHandlers[path]; nil == handler {
return nil, fmt.Errorf("Unrecognized path: %s", path)
}
return handler, nil
}
func (sh *ServerHandlers) GetName() string {
return sh.Name
}
func (sh *ServerHandlers) GetConcurrency() int {
return sh.Concurrency
}
func (sh *ServerHandlers) GetMaxStopWaitTime() time.Duration {
return sh.MaxStopWaitTime
}
func (sh *ServerHandlers) GetHandshakeTimeout() time.Duration {
return sh.HandshakeTimeout
}
func (sh *ServerHandlers) GetReadBufferSize() int {
return sh.ReadBufferSize
}
func (sh *ServerHandlers) GetWriteBufferSize() int {
return sh.WriteBufferSize
}
func (sh *ServerHandlers) GetReadTimeout() time.Duration {
return sh.ReadTimeout
}
func (sh *ServerHandlers) GetWriteTimeout() time.Duration {
return sh.WriteTimeout
}
func (sh *ServerHandlers) IsEnableCompression() bool {
return sh.EnableCompression
}
func (sh *ServerHandlers) Validate() {
if sh.Concurrency <= 0 {
sh.Concurrency = DefaultConcurrency
}
if sh.MaxStopWaitTime <= 0 {
sh.MaxStopWaitTime = DefaultMaxStopWaitTime
}
if sh.HandshakeTimeout <= 0 {
sh.HandshakeTimeout = DefaultHandshakeTimeout
}
if sh.ReadBufferSize <= 0 {
sh.ReadBufferSize = DefaultReadBufferSize
}
if sh.WriteBufferSize <= 0 {
sh.WriteBufferSize = DefaultWriteBufferSize
}
if sh.ReadTimeout <= 0 {
sh.ReadTimeout = DefaultReadTimeout
}
if sh.WriteTimeout <= 0 {
sh.WriteTimeout = DefaultWriteTimeout
}
}