This commit is contained in:
crusader 2018-04-13 16:11:43 +09:00
parent 490aaae260
commit 7f7773d01b
13 changed files with 115 additions and 1 deletions

View File

@ -1,5 +1,7 @@
package client
import "sync/atomic"
type Connector interface {
Connect() (readChan <-chan []byte, writeChan chan<- []byte, err error)
Disconnect() error
@ -12,6 +14,8 @@ type Connector interface {
type Connectors struct {
Name string `json:"name"`
validated atomic.Value
}
func (c *Connectors) GetName() string {
@ -20,11 +24,17 @@ func (c *Connectors) GetName() string {
func (c *Connectors) Clone() *Connectors {
return &Connectors{
Name: c.Name,
Name: c.Name,
validated: c.validated,
}
}
func (c *Connectors) Validate() error {
if nil != c.validated.Load() {
return nil
}
c.validated.Store(true)
if "" == c.Name {
c.Name = "Connector"
}

View File

@ -3,6 +3,7 @@ package server
import (
"crypto/tls"
"net"
"sync/atomic"
"time"
)
@ -25,6 +26,7 @@ type ConnectionHandlers struct {
KeepAlive time.Duration `json:"keepAlive"`
HandshakeTimeout time.Duration `json:"handshakeTimeout"`
TLSConfig *tls.Config
validated atomic.Value
}
func (ch *ConnectionHandlers) Listener(serverCtx ServerCtx) (net.Listener, error) {
@ -60,10 +62,16 @@ func (ch *ConnectionHandlers) Clone() *ConnectionHandlers {
KeepAlive: ch.KeepAlive,
HandshakeTimeout: ch.HandshakeTimeout,
TLSConfig: ch.TLSConfig,
validated: ch.validated,
}
}
func (ch *ConnectionHandlers) Validate() error {
if nil != ch.validated.Load() {
return nil
}
ch.validated.Store(true)
if ch.Concurrency <= 0 {
ch.Concurrency = DefaultConcurrency
}

View File

@ -1,6 +1,7 @@
package server
import (
"sync/atomic"
"time"
)
@ -38,6 +39,8 @@ type ReadWriteHandlers struct {
//
// By default response write timeout is unlimited.
WriteTimeout time.Duration `json:"writeTimeout"`
validated atomic.Value
}
func (rwh *ReadWriteHandlers) GetMaxMessageSize() int64 {
@ -63,10 +66,16 @@ func (rwh *ReadWriteHandlers) Clone() *ReadWriteHandlers {
WriteBufferSize: rwh.WriteBufferSize,
ReadTimeout: rwh.ReadTimeout,
WriteTimeout: rwh.WriteTimeout,
validated: rwh.validated,
}
}
func (rwh *ReadWriteHandlers) Validate() error {
if nil != rwh.validated.Load() {
return nil
}
rwh.validated.Store(true)
if rwh.MaxMessageSize <= 0 {
rwh.MaxMessageSize = DefaultMaxMessageSize
}

View File

@ -1,5 +1,7 @@
package server
import "sync/atomic"
type ServerHandler interface {
ConnectionHandler
@ -21,6 +23,8 @@ type ServerHandlers struct {
//
// Default server name is used if left blank.
Name string `json:"name"`
validated atomic.Value
}
func (sh *ServerHandlers) ServerCtx() ServerCtx {
@ -48,6 +52,11 @@ func (sh *ServerHandlers) GetName() string {
}
func (sh *ServerHandlers) Validate() error {
if nil != sh.validated.Load() {
return nil
}
sh.validated.Store(true)
if err := sh.ConnectionHandlers.Validate(); nil != err {
return err
}

View File

@ -1,6 +1,7 @@
package socket
import (
"sync/atomic"
"time"
"git.loafle.net/commons/server-go"
@ -18,6 +19,8 @@ type ClientConnHandlers struct {
ReconnectInterval time.Duration `json:"reconnectInterval"`
ReconnectTryTime int `json:"reconnectTryTime"`
validated atomic.Value
}
func (cch *ClientConnHandlers) GetReconnectInterval() time.Duration {
@ -33,10 +36,16 @@ func (cch *ClientConnHandlers) Clone() *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
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"git.loafle.net/commons/logging-go"
@ -31,6 +32,8 @@ type Connectors struct {
reconnectedChan chan socket.Conn
crw socket.ClientReadWriter
validated atomic.Value
}
func (c *Connectors) Connect() (readChan <-chan []byte, writeChan chan<- []byte, err error) {
@ -176,10 +179,16 @@ func (c *Connectors) Clone() client.Connector {
Network: c.Network,
Address: c.Address,
LocalAddress: c.LocalAddress,
validated: c.validated,
}
}
func (c *Connectors) Validate() error {
if nil != c.validated.Load() {
return nil
}
c.validated.Store(true)
if err := c.Connectors.Validate(); nil != err {
return err
}

View File

@ -2,6 +2,7 @@ package net
import (
"net"
"sync/atomic"
"git.loafle.net/commons/server-go"
"git.loafle.net/commons/server-go/socket"
@ -20,6 +21,8 @@ type ServerHandlers struct {
socket.ServerHandlers
servlet Servlet
validated atomic.Value
}
func (sh *ServerHandlers) Init(serverCtx server.ServerCtx) error {
@ -78,6 +81,11 @@ func (sh *ServerHandlers) Servlet(serverCtx server.ServerCtx, conn net.Conn) Ser
}
func (sh *ServerHandlers) Validate() error {
if nil != sh.validated.Load() {
return nil
}
sh.validated.Store(true)
if err := sh.ServerHandlers.Validate(); nil != err {
return err
}

View File

@ -1,6 +1,7 @@
package socket
import (
"sync/atomic"
"time"
"git.loafle.net/commons/server-go"
@ -23,6 +24,8 @@ type ReadWriteHandlers struct {
PingPeriod time.Duration `json:"pingPeriod"`
EnableCompression bool `json:"enableCompression"`
validated atomic.Value
}
func (rwh *ReadWriteHandlers) GetPongTimeout() time.Duration {
@ -45,10 +48,16 @@ func (rwh *ReadWriteHandlers) Clone() *ReadWriteHandlers {
PingTimeout: rwh.PingTimeout,
PingPeriod: rwh.PingPeriod,
EnableCompression: rwh.EnableCompression,
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
}

View File

@ -1,6 +1,8 @@
package socket
import (
"sync/atomic"
"git.loafle.net/commons/server-go"
)
@ -12,9 +14,16 @@ type ServerHandler interface {
type ServerHandlers struct {
server.ServerHandlers
ReadWriteHandlers
validated atomic.Value
}
func (sh *ServerHandlers) Validate() error {
if nil != sh.validated.Load() {
return nil
}
sh.validated.Store(true)
if err := sh.ServerHandlers.Validate(); nil != err {
return err
}

View File

@ -14,6 +14,7 @@ import (
"net/url"
"strings"
"sync"
"sync/atomic"
"time"
"git.loafle.net/commons/logging-go"
@ -62,6 +63,8 @@ type Connectors struct {
reconnectedChan chan socket.Conn
crw socket.ClientReadWriter
validated atomic.Value
}
func (c *Connectors) Connect() (readChan <-chan []byte, writeChan chan<- []byte, err error) {
@ -470,10 +473,16 @@ func (c *Connectors) Clone() client.Connector {
NetDial: c.NetDial,
Proxy: c.Proxy,
serverURL: c.serverURL,
validated: c.validated,
}
}
func (c *Connectors) Validate() error {
if nil != c.validated.Load() {
return nil
}
c.validated.Store(true)
if err := c.Connectors.Validate(); nil != err {
return err
}

View File

@ -2,6 +2,7 @@ package web
import (
"net/http"
"sync/atomic"
"git.loafle.net/commons/server-go"
"git.loafle.net/commons/server-go/socket"
@ -24,6 +25,8 @@ type ServerHandlers struct {
socket.ServerHandlers
servlets map[string]Servlet
validated atomic.Value
}
func (sh *ServerHandlers) Init(serverCtx server.ServerCtx) error {
@ -110,6 +113,11 @@ func (sh *ServerHandlers) CheckOrigin(ctx *fasthttp.RequestCtx) bool {
}
func (sh *ServerHandlers) Validate() error {
if nil != sh.validated.Load() {
return nil
}
sh.validated.Store(true)
if err := sh.ServerHandlers.Validate(); nil != err {
return err
}

View File

@ -3,6 +3,7 @@ package fasthttp
import (
"fmt"
"strings"
"sync/atomic"
logging "git.loafle.net/commons/logging-go"
"git.loafle.net/commons/server-go"
@ -30,6 +31,8 @@ type ServerHandlers struct {
// path = context only.
// ex) /auth => /auth, /auth/member => /auth
servlets map[string]Servlet
validated atomic.Value
}
func (sh *ServerHandlers) Init(serverCtx server.ServerCtx) error {
@ -125,6 +128,11 @@ func (sh *ServerHandlers) CheckOrigin(ctx *fasthttp.RequestCtx) bool {
}
func (sh *ServerHandlers) Validate() error {
if nil != sh.validated.Load() {
return nil
}
sh.validated.Store(true)
if err := sh.ServerHandlers.Validate(); nil != err {
return err
}

View File

@ -1,6 +1,8 @@
package web
import (
"sync/atomic"
"git.loafle.net/commons/server-go"
)
@ -12,9 +14,16 @@ type ServerHandler interface {
type ServerHandlers struct {
server.ServerHandlers
server.ReadWriteHandlers
validated atomic.Value
}
func (sh *ServerHandlers) Validate() error {
if nil != sh.validated.Load() {
return nil
}
sh.validated.Store(true)
if err := sh.ServerHandlers.Validate(); nil != err {
return err
}