108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package socket
|
|
|
|
import (
|
|
"errors"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"git.loafle.net/overflow/server-go"
|
|
)
|
|
|
|
type ReadWriteHandler interface {
|
|
server.ReadWriteHandler
|
|
GetPongTimeout() time.Duration
|
|
GetPingTimeout() time.Duration
|
|
GetPingPeriod() time.Duration
|
|
|
|
IsEnableCompression() bool
|
|
GetCompressionLevel() int
|
|
GetCompressionThreshold() int
|
|
}
|
|
|
|
type ReadWriteHandlers struct {
|
|
server.ReadWriteHandlers
|
|
|
|
PongTimeout time.Duration `json:"pongTimeout,omitempty"`
|
|
PingTimeout time.Duration `json:"pingTimeout,omitempty"`
|
|
PingPeriod time.Duration `json:"pingPeriod,omitempty"`
|
|
|
|
EnableCompression bool `json:"enableCompression,omitempty"`
|
|
CompressionLevel int `json:"compressionLevel,omitempty"`
|
|
CompressionThreshold int `json:"compressionThreshold,omitempty"`
|
|
|
|
validated atomic.Value
|
|
}
|
|
|
|
func (rwh *ReadWriteHandlers) GetPongTimeout() time.Duration {
|
|
return rwh.PongTimeout
|
|
}
|
|
func (rwh *ReadWriteHandlers) GetPingTimeout() time.Duration {
|
|
return rwh.PingTimeout
|
|
}
|
|
func (rwh *ReadWriteHandlers) GetPingPeriod() time.Duration {
|
|
return rwh.PingPeriod
|
|
}
|
|
|
|
func (rwh *ReadWriteHandlers) IsEnableCompression() bool {
|
|
return rwh.EnableCompression
|
|
}
|
|
func (rwh *ReadWriteHandlers) GetCompressionLevel() int {
|
|
return rwh.CompressionLevel
|
|
}
|
|
func (rwh *ReadWriteHandlers) GetCompressionThreshold() int {
|
|
return rwh.CompressionThreshold
|
|
}
|
|
|
|
func (rwh *ReadWriteHandlers) Clone() *ReadWriteHandlers {
|
|
return &ReadWriteHandlers{
|
|
ReadWriteHandlers: *rwh.ReadWriteHandlers.Clone(),
|
|
PongTimeout: rwh.PongTimeout,
|
|
PingTimeout: rwh.PingTimeout,
|
|
PingPeriod: rwh.PingPeriod,
|
|
EnableCompression: rwh.EnableCompression,
|
|
CompressionLevel: rwh.CompressionLevel,
|
|
CompressionThreshold: rwh.CompressionThreshold,
|
|
validated: rwh.validated,
|
|
}
|
|
}
|
|
|
|
func (rwh *ReadWriteHandlers) Validate() error {
|
|
if nil != rwh.validated.Load() {
|
|
return nil
|
|
}
|
|
rwh.validated.Store(true)
|
|
|
|
if err := rwh.ReadWriteHandlers.Validate(); nil != err {
|
|
return err
|
|
}
|
|
|
|
if rwh.PongTimeout <= 0 {
|
|
rwh.PongTimeout = server.DefaultPongTimeout
|
|
} else {
|
|
rwh.PongTimeout = rwh.PongTimeout * time.Second
|
|
}
|
|
|
|
if rwh.PingTimeout <= 0 {
|
|
rwh.PingTimeout = server.DefaultPingTimeout
|
|
} else {
|
|
rwh.PingTimeout = rwh.PingTimeout * time.Second
|
|
}
|
|
if rwh.PingPeriod <= 0 {
|
|
rwh.PingPeriod = (rwh.PingTimeout * 9) / 10
|
|
} else {
|
|
rwh.PingPeriod = rwh.PingPeriod * time.Second
|
|
}
|
|
|
|
if rwh.EnableCompression {
|
|
if !IsValidCompressionLevel(rwh.CompressionLevel) {
|
|
return errors.New("Socket: invalid compression level")
|
|
}
|
|
}
|
|
|
|
if 0 > rwh.CompressionThreshold {
|
|
rwh.CompressionThreshold = server.DefaultCompressionThreshold
|
|
}
|
|
|
|
return nil
|
|
}
|