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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package socket package socket
import ( import (
"sync/atomic"
"time" "time"
"git.loafle.net/commons/server-go" "git.loafle.net/commons/server-go"
@ -18,6 +19,8 @@ type ClientConnHandlers struct {
ReconnectInterval time.Duration `json:"reconnectInterval"` ReconnectInterval time.Duration `json:"reconnectInterval"`
ReconnectTryTime int `json:"reconnectTryTime"` ReconnectTryTime int `json:"reconnectTryTime"`
validated atomic.Value
} }
func (cch *ClientConnHandlers) GetReconnectInterval() time.Duration { func (cch *ClientConnHandlers) GetReconnectInterval() time.Duration {
@ -33,10 +36,16 @@ func (cch *ClientConnHandlers) Clone() *ClientConnHandlers {
ConnectionHandlers: *cch.ConnectionHandlers.Clone(), ConnectionHandlers: *cch.ConnectionHandlers.Clone(),
ReconnectInterval: cch.ReconnectInterval, ReconnectInterval: cch.ReconnectInterval,
ReconnectTryTime: cch.ReconnectTryTime, ReconnectTryTime: cch.ReconnectTryTime,
validated: cch.validated,
} }
} }
func (cch *ClientConnHandlers) Validate() error { func (cch *ClientConnHandlers) Validate() error {
if nil != cch.validated.Load() {
return nil
}
cch.validated.Store(true)
if err := cch.ConnectionHandlers.Validate(); nil != err { if err := cch.ConnectionHandlers.Validate(); nil != err {
return err return err
} }

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package socket package socket
import ( import (
"sync/atomic"
"time" "time"
"git.loafle.net/commons/server-go" "git.loafle.net/commons/server-go"
@ -23,6 +24,8 @@ type ReadWriteHandlers struct {
PingPeriod time.Duration `json:"pingPeriod"` PingPeriod time.Duration `json:"pingPeriod"`
EnableCompression bool `json:"enableCompression"` EnableCompression bool `json:"enableCompression"`
validated atomic.Value
} }
func (rwh *ReadWriteHandlers) GetPongTimeout() time.Duration { func (rwh *ReadWriteHandlers) GetPongTimeout() time.Duration {
@ -45,10 +48,16 @@ func (rwh *ReadWriteHandlers) Clone() *ReadWriteHandlers {
PingTimeout: rwh.PingTimeout, PingTimeout: rwh.PingTimeout,
PingPeriod: rwh.PingPeriod, PingPeriod: rwh.PingPeriod,
EnableCompression: rwh.EnableCompression, EnableCompression: rwh.EnableCompression,
validated: rwh.validated,
} }
} }
func (rwh *ReadWriteHandlers) Validate() error { func (rwh *ReadWriteHandlers) Validate() error {
if nil != rwh.validated.Load() {
return nil
}
rwh.validated.Store(true)
if err := rwh.ReadWriteHandlers.Validate(); nil != err { if err := rwh.ReadWriteHandlers.Validate(); nil != err {
return err return err
} }

View File

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

View File

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

View File

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

View File

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

View File

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