websocket_fasthttp/server_handlers.go

177 lines
4.2 KiB
Go
Raw Normal View History

2017-11-07 10:03:40 +00:00
package websocket_fasthttp
import (
"errors"
"fmt"
"net"
"time"
2017-11-10 08:33:40 +00:00
"git.loafle.net/commons_go/logging"
2017-11-07 10:03:40 +00:00
"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
}
2017-11-10 09:49:19 +00:00
func (sh *ServerHandlers) Init() error {
if nil != sh.socketHandlers {
for _, socketHandler := range sh.socketHandlers {
if err := socketHandler.Init(); nil != err {
return err
}
}
}
return nil
}
2017-11-07 10:03:40 +00:00
func (sh *ServerHandlers) Listen() (net.Listener, error) {
return nil, errors.New("Server: Handler method[Listen] of Server is not implement")
}
2017-11-10 09:49:19 +00:00
// OnStart invoked when server is stated
// If you override ths method, must call
func (sh *ServerHandlers) OnStart() {
// no op
}
2017-11-07 10:03:40 +00:00
func (sh *ServerHandlers) CheckOrigin(ctx *fasthttp.RequestCtx) bool {
return true
}
func (sh *ServerHandlers) OnError(ctx *fasthttp.RequestCtx, status int, reason error) {
2017-11-10 08:33:40 +00:00
logging.Logger().Error(fmt.Sprintf("Server: error status: %d, reason: %v, [%v]", status, reason, ctx))
2017-11-07 10:03:40 +00:00
}
2017-11-10 08:33:40 +00:00
// OnStop invoked when server is stopped
// If you override ths method, must call
2017-11-07 10:03:40 +00:00
func (sh *ServerHandlers) OnStop() {
2017-11-10 08:33:40 +00:00
if nil != sh.socketHandlers {
for _, socketHandler := range sh.socketHandlers {
socketHandler.Destroy()
}
}
2017-11-07 10:03:40 +00:00
}
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
}
2017-11-10 08:33:40 +00:00
2017-11-07 10:03:40 +00:00
func (sh *ServerHandlers) GetMaxStopWaitTime() time.Duration {
return sh.MaxStopWaitTime
}
2017-11-10 08:33:40 +00:00
2017-11-07 10:03:40 +00:00
func (sh *ServerHandlers) GetHandshakeTimeout() time.Duration {
return sh.HandshakeTimeout
}
2017-11-10 08:33:40 +00:00
2017-11-07 10:03:40 +00:00
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
}
}