server-go/connection-handler.go

66 lines
1.4 KiB
Go
Raw Normal View History

2018-04-04 13:28:35 +00:00
package server
import (
"crypto/tls"
"fmt"
"net"
"time"
)
type ConnectionHandler interface {
GetConcurrency() int
GetKeepAlive() time.Duration
GetHandshakeTimeout() time.Duration
GetTLSConfig() *tls.Config
Listener(serverCtx ServerCtx) (net.Listener, error)
}
type ConnectionHandlers struct {
// The maximum number of concurrent connections the server may serve.
//
// DefaultConcurrency is used if not set.
2018-04-09 12:11:52 +00:00
Network string `json:"network"`
Address string `json:"address"`
Concurrency int `json:"concurrency"`
KeepAlive time.Duration `json:"keepAlive"`
HandshakeTimeout time.Duration `json:"handshakeTimeout"`
2018-04-04 13:28:35 +00:00
TLSConfig *tls.Config
}
func (ch *ConnectionHandlers) Listener(serverCtx ServerCtx) (net.Listener, error) {
return nil, fmt.Errorf("Method[ConnectionHandler.Listener] is not implemented")
}
func (ch *ConnectionHandlers) GetConcurrency() int {
return ch.Concurrency
}
func (ch *ConnectionHandlers) GetKeepAlive() time.Duration {
return ch.KeepAlive
}
func (ch *ConnectionHandlers) GetHandshakeTimeout() time.Duration {
return ch.HandshakeTimeout
}
func (ch *ConnectionHandlers) GetTLSConfig() *tls.Config {
return ch.TLSConfig
}
func (ch *ConnectionHandlers) Validate() error {
if ch.Concurrency <= 0 {
ch.Concurrency = DefaultConcurrency
}
if ch.KeepAlive <= 0 {
ch.KeepAlive = DefaultKeepAlive
}
if ch.HandshakeTimeout <= 0 {
ch.HandshakeTimeout = DefaultHandshakeTimeout
}
return nil
}