server-go/socket/readwrite-handler.go

84 lines
1.9 KiB
Go
Raw Normal View History

2018-04-05 15:15:29 +00:00
package socket
import (
2018-04-13 07:11:43 +00:00
"sync/atomic"
2018-04-05 15:15:29 +00:00
"time"
"git.loafle.net/commons/server-go"
)
type ReadWriteHandler interface {
server.ReadWriteHandler
GetPongTimeout() time.Duration
GetPingTimeout() time.Duration
GetPingPeriod() time.Duration
IsEnableCompression() bool
}
type ReadWriteHandlers struct {
server.ReadWriteHandlers
2018-04-14 08:49:33 +00:00
PongTimeout time.Duration `json:"pongTimeout,omitempty"`
PingTimeout time.Duration `json:"pingTimeout,omitempty"`
PingPeriod time.Duration `json:"pingPeriod,omitempty"`
2018-04-05 15:15:29 +00:00
2018-04-14 08:49:33 +00:00
EnableCompression bool `json:"enableCompression,omitempty"`
2018-04-13 07:11:43 +00:00
validated atomic.Value
2018-04-05 15:15:29 +00:00
}
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
}
2018-04-12 05:55:01 +00:00
func (rwh *ReadWriteHandlers) Clone() *ReadWriteHandlers {
return &ReadWriteHandlers{
ReadWriteHandlers: *rwh.ReadWriteHandlers.Clone(),
PongTimeout: rwh.PongTimeout,
PingTimeout: rwh.PingTimeout,
PingPeriod: rwh.PingPeriod,
EnableCompression: rwh.EnableCompression,
2018-04-13 07:11:43 +00:00
validated: rwh.validated,
2018-04-12 05:55:01 +00:00
}
}
2018-04-05 15:15:29 +00:00
func (rwh *ReadWriteHandlers) Validate() error {
2018-04-13 07:11:43 +00:00
if nil != rwh.validated.Load() {
return nil
}
rwh.validated.Store(true)
2018-04-05 15:15:29 +00:00
if err := rwh.ReadWriteHandlers.Validate(); nil != err {
return err
}
if rwh.PongTimeout <= 0 {
rwh.PongTimeout = server.DefaultPongTimeout
2018-04-13 06:37:27 +00:00
} else {
rwh.PongTimeout = rwh.PongTimeout * time.Second
2018-04-05 15:15:29 +00:00
}
2018-04-12 13:14:11 +00:00
2018-04-05 15:15:29 +00:00
if rwh.PingTimeout <= 0 {
rwh.PingTimeout = server.DefaultPingTimeout
2018-04-13 06:37:27 +00:00
} else {
rwh.PingTimeout = rwh.PingTimeout * time.Second
2018-04-05 15:15:29 +00:00
}
if rwh.PingPeriod <= 0 {
rwh.PingPeriod = (rwh.PingTimeout * 9) / 10
2018-04-13 06:37:27 +00:00
} else {
rwh.PingPeriod = rwh.PingPeriod * time.Second
2018-04-05 15:15:29 +00:00
}
return nil
}