server-go/socket/client-conn-handler.go
crusader 7f7773d01b ing
2018-04-13 16:11:43 +09:00

65 lines
1.3 KiB
Go

package socket
import (
"sync/atomic"
"time"
"git.loafle.net/commons/server-go"
)
type ClientConnHandler interface {
server.ConnectionHandler
GetReconnectInterval() time.Duration
GetReconnectTryTime() int
}
type ClientConnHandlers struct {
server.ConnectionHandlers
ReconnectInterval time.Duration `json:"reconnectInterval"`
ReconnectTryTime int `json:"reconnectTryTime"`
validated atomic.Value
}
func (cch *ClientConnHandlers) GetReconnectInterval() time.Duration {
return cch.ReconnectInterval
}
func (cch *ClientConnHandlers) GetReconnectTryTime() int {
return cch.ReconnectTryTime
}
func (cch *ClientConnHandlers) Clone() *ClientConnHandlers {
return &ClientConnHandlers{
ConnectionHandlers: *cch.ConnectionHandlers.Clone(),
ReconnectInterval: cch.ReconnectInterval,
ReconnectTryTime: cch.ReconnectTryTime,
validated: cch.validated,
}
}
func (cch *ClientConnHandlers) Validate() error {
if nil != cch.validated.Load() {
return nil
}
cch.validated.Store(true)
if err := cch.ConnectionHandlers.Validate(); nil != err {
return err
}
if cch.ReconnectInterval <= 0 {
cch.ReconnectInterval = server.DefaultReconnectInterval
} else {
cch.ReconnectInterval = cch.ReconnectInterval * time.Second
}
if cch.ReconnectTryTime <= 0 {
cch.ReconnectTryTime = server.DefaultReconnectTryTime
}
return nil
}