modify option setting of server by functional options

This commit is contained in:
smallnest
2017-10-30 19:31:09 +08:00
parent b70fd2d243
commit 24c716b4bd
10 changed files with 77 additions and 24 deletions

View File

@@ -34,7 +34,7 @@ func (t *PBArith) Mul(ctx context.Context, args *testutils.ProtoArgs, reply *tes
} }
func TestClient_IT(t *testing.T) { func TestClient_IT(t *testing.T) {
s := server.Server{} s := server.NewServer()
s.RegisterName("Arith", new(Arith), "") s.RegisterName("Arith", new(Arith), "")
s.RegisterName("PBArith", new(PBArith), "") s.RegisterName("PBArith", new(PBArith), "")
go s.Serve("tcp", "127.0.0.1:0") go s.Serve("tcp", "127.0.0.1:0")

View File

@@ -9,7 +9,7 @@ import (
) )
func TestXClient_IT(t *testing.T) { func TestXClient_IT(t *testing.T) {
s := server.Server{} s := server.NewServer()
s.RegisterName("Arith", new(Arith), "") s.RegisterName("Arith", new(Arith), "")
go s.Serve("tcp", "127.0.0.1:0") go s.Serve("tcp", "127.0.0.1:0")
defer s.Close() defer s.Close()

View File

@@ -14,9 +14,16 @@ func init() {
} }
func kcpMakeListener(s *Server, address string) (ln net.Listener, err error) { func kcpMakeListener(s *Server, address string) (ln net.Listener, err error) {
if s.Options == nil || s.Options["BlockCrypt"] == nil { if s.options == nil || s.options["BlockCrypt"] == nil {
return nil, errors.New("KCP BlockCrypt must be configured in server.Options") return nil, errors.New("KCP BlockCrypt must be configured in server.Options")
} }
return kcp.ListenWithOptions(address, s.Options["BlockCrypt"].(kcp.BlockCrypt), 10, 3) return kcp.ListenWithOptions(address, s.options["BlockCrypt"].(kcp.BlockCrypt), 10, 3)
}
// WithWriteTimeout sets writeTimeout.
func WithBlockCrypt(bc kcp.BlockCrypt) OptionFn {
return func(s *Server) {
s.options["BlockCrypt"] = bc
}
} }

View File

@@ -32,10 +32,10 @@ func (s *Server) makeListener(network, address string) (ln net.Listener, err err
} }
func tcpMakeListener(s *Server, address string) (ln net.Listener, err error) { func tcpMakeListener(s *Server, address string) (ln net.Listener, err error) {
if s.TLSConfig == nil { if s.tlsConfig == nil {
ln, err = net.Listen("tcp", address) ln, err = net.Listen("tcp", address)
} else { } else {
ln, err = tls.Listen("tcp", address, s.TLSConfig) ln, err = tls.Listen("tcp", address, s.tlsConfig)
} }
return ln, err return ln, err

39
server/option.go Normal file
View File

@@ -0,0 +1,39 @@
package server
import (
"crypto/tls"
"time"
)
// OptionFn configures options of server.
type OptionFn func(*Server)
// // WithOptions sets multiple options.
// func WithOptions(ops map[string]interface{}) OptionFn {
// return func(s *Server) {
// for k, v := range ops {
// s.options[k] = v
// }
// }
// }
// WithTLSConfig sets tls.Config.
func WithTLSConfig(cfg *tls.Config) OptionFn {
return func(s *Server) {
s.tlsConfig = cfg
}
}
// WithReadTimeout sets readTimeout.
func WithReadTimeout(readTimeout time.Duration) OptionFn {
return func(s *Server) {
s.readTimeout = readTimeout
}
}
// WithWriteTimeout sets writeTimeout.
func WithWriteTimeout(writeTimeout time.Duration) OptionFn {
return func(s *Server) {
s.writeTimeout = writeTimeout
}
}

View File

@@ -14,8 +14,8 @@ func init() {
} }
func quicMakeListener(s *Server, address string) (ln net.Listener, err error) { func quicMakeListener(s *Server, address string) (ln net.Listener, err error) {
if s.TLSConfig == nil { if s.tlsConfig == nil {
return nil, errors.New("TLSConfig must be configured in server.Options") return nil, errors.New("TLSConfig must be configured in server.Options")
} }
return quicconn.Listen("udp", address, s.TLSConfig) return quicconn.Listen("udp", address, s.tlsConfig)
} }

View File

@@ -49,8 +49,8 @@ var (
// Server is rpcx server that use TCP or UDP. // Server is rpcx server that use TCP or UDP.
type Server struct { type Server struct {
ln net.Listener ln net.Listener
ReadTimeout time.Duration readTimeout time.Duration
WriteTimeout time.Duration writeTimeout time.Duration
serviceMapMu sync.RWMutex serviceMapMu sync.RWMutex
serviceMap map[string]*service serviceMap map[string]*service
@@ -63,9 +63,9 @@ type Server struct {
onShutdown []func() onShutdown []func()
// TLSConfig for creating tls tcp connection. // TLSConfig for creating tls tcp connection.
TLSConfig *tls.Config tlsConfig *tls.Config
// BlockCrypt for kcp.BlockCrypt // BlockCrypt for kcp.BlockCrypt
Options map[string]interface{} options map[string]interface{}
// // use for KCP // // use for KCP
// KCPConfig KCPConfig // KCPConfig KCPConfig
// // for QUIC // // for QUIC
@@ -78,11 +78,18 @@ type Server struct {
} }
// NewServer returns a server. // NewServer returns a server.
func NewServer(options map[string]interface{}) *Server { func NewServer(options ...OptionFn) *Server {
return &Server{ s := &Server{
Plugins: &pluginContainer{}, Plugins: &pluginContainer{},
Options: options, options: make(map[string]interface{}),
} }
for _, op := range options {
fmt.Printf("%T\n", op)
op(s)
}
return s
} }
// Address returns listened address. // Address returns listened address.
@@ -227,10 +234,10 @@ func (s *Server) serveConn(conn net.Conn) {
}() }()
if tlsConn, ok := conn.(*tls.Conn); ok { if tlsConn, ok := conn.(*tls.Conn); ok {
if d := s.ReadTimeout; d != 0 { if d := s.readTimeout; d != 0 {
conn.SetReadDeadline(time.Now().Add(d)) conn.SetReadDeadline(time.Now().Add(d))
} }
if d := s.WriteTimeout; d != 0 { if d := s.writeTimeout; d != 0 {
conn.SetWriteDeadline(time.Now().Add(d)) conn.SetWriteDeadline(time.Now().Add(d))
} }
if err := tlsConn.Handshake(); err != nil { if err := tlsConn.Handshake(); err != nil {
@@ -245,8 +252,8 @@ func (s *Server) serveConn(conn net.Conn) {
for { for {
t0 := time.Now() t0 := time.Now()
if s.ReadTimeout != 0 { if s.readTimeout != 0 {
conn.SetReadDeadline(t0.Add(s.ReadTimeout)) conn.SetReadDeadline(t0.Add(s.readTimeout))
} }
req, err := s.readRequest(ctx, r) req, err := s.readRequest(ctx, r)
@@ -261,8 +268,8 @@ func (s *Server) serveConn(conn net.Conn) {
return return
} }
if s.WriteTimeout != 0 { if s.writeTimeout != 0 {
conn.SetWriteDeadline(t0.Add(s.WriteTimeout)) conn.SetWriteDeadline(t0.Add(s.writeTimeout))
} }
err = s.auth(ctx, req) err = s.auth(ctx, req)

View File

@@ -11,7 +11,7 @@ import (
) )
func TestConsulRegistry(t *testing.T) { func TestConsulRegistry(t *testing.T) {
s := server.NewServer(nil) s := server.NewServer()
r := &ConsulRegisterPlugin{ r := &ConsulRegisterPlugin{
ServiceAddress: "tcp@127.0.0.1:8972", ServiceAddress: "tcp@127.0.0.1:8972",

View File

@@ -11,7 +11,7 @@ import (
) )
func TestEtcdRegistry(t *testing.T) { func TestEtcdRegistry(t *testing.T) {
s := server.NewServer(nil) s := server.NewServer()
r := &EtcdRegisterPlugin{ r := &EtcdRegisterPlugin{
ServiceAddress: "tcp@127.0.0.1:8972", ServiceAddress: "tcp@127.0.0.1:8972",

View File

@@ -11,7 +11,7 @@ import (
) )
func TestZookeeperRegistry(t *testing.T) { func TestZookeeperRegistry(t *testing.T) {
s := server.NewServer(nil) s := server.NewServer()
r := &ZooKeeperRegisterPlugin{ r := &ZooKeeperRegisterPlugin{
ServiceAddress: "tcp@127.0.0.1:8972", ServiceAddress: "tcp@127.0.0.1:8972",