server-go/server-handler.go
crusader 8a218a0806 ing
2018-04-04 16:19:43 +09:00

191 lines
4.2 KiB
Go

package server
import (
"fmt"
"net"
"time"
)
type ServerHandler interface {
GetName() string
GetConcurrency() int
GetHandshakeTimeout() time.Duration
GetMaxMessageSize() int64
GetReadBufferSize() int
GetWriteBufferSize() int
GetReadTimeout() time.Duration
GetWriteTimeout() time.Duration
GetPongTimeout() time.Duration
GetPingTimeout() time.Duration
GetPingPeriod() time.Duration
IsEnableCompression() bool
ServerCtx() ServerCtx
Init(serverCtx ServerCtx) error
OnStart(serverCtx ServerCtx) error
OnStop(serverCtx ServerCtx)
Destroy(serverCtx ServerCtx)
Listener(serverCtx ServerCtx) (net.Listener, error)
Validate() error
}
type ServerHandlers struct {
ServerHandler
// 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
HandshakeTimeout time.Duration
MaxMessageSize int64
// 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
PongTimeout time.Duration
PingTimeout time.Duration
PingPeriod time.Duration
EnableCompression bool
}
func (sh *ServerHandlers) ServerCtx() ServerCtx {
return NewServerCtx(nil)
}
func (sh *ServerHandlers) Init(serverCtx ServerCtx) error {
return nil
}
func (sh *ServerHandlers) OnStart(serverCtx ServerCtx) error {
return nil
}
func (sh *ServerHandlers) OnStop(serverCtx ServerCtx) {
}
func (sh *ServerHandlers) Destroy(serverCtx ServerCtx) {
}
func (sh *ServerHandlers) Listener(serverCtx ServerCtx) (net.Listener, error) {
return nil, fmt.Errorf("Server: Method[ServerHandler.Listener] is not implemented")
}
func (sh *ServerHandlers) GetName() string {
return sh.Name
}
func (sh *ServerHandlers) GetConcurrency() int {
return sh.Concurrency
}
func (sh *ServerHandlers) GetHandshakeTimeout() time.Duration {
return sh.HandshakeTimeout
}
func (sh *ServerHandlers) GetMaxMessageSize() int64 {
return sh.MaxMessageSize
}
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) GetPongTimeout() time.Duration {
return sh.PongTimeout
}
func (sh *ServerHandlers) GetPingTimeout() time.Duration {
return sh.PingTimeout
}
func (sh *ServerHandlers) GetPingPeriod() time.Duration {
return sh.PingPeriod
}
func (sh *ServerHandlers) IsEnableCompression() bool {
return sh.EnableCompression
}
func (sh *ServerHandlers) Validate() error {
if "" == sh.Name {
sh.Name = "Server"
}
if sh.Concurrency <= 0 {
sh.Concurrency = DefaultConcurrency
}
if sh.HandshakeTimeout <= 0 {
sh.HandshakeTimeout = DefaultHandshakeTimeout
}
if sh.MaxMessageSize <= 0 {
sh.MaxMessageSize = DefaultMaxMessageSize
}
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
}
if sh.PongTimeout <= 0 {
sh.PongTimeout = DefaultPongTimeout
}
if sh.PingTimeout <= 0 {
sh.PingTimeout = DefaultPingTimeout
}
if sh.PingPeriod <= 0 {
sh.PingPeriod = (sh.PingTimeout * 9) / 10
}
return nil
}