mirror of
https://github.com/smallnest/rpcx.git
synced 2025-10-28 02:11:27 +08:00
modify option setting of server by functional options
This commit is contained in:
@@ -34,7 +34,7 @@ func (t *PBArith) Mul(ctx context.Context, args *testutils.ProtoArgs, reply *tes
|
||||
}
|
||||
|
||||
func TestClient_IT(t *testing.T) {
|
||||
s := server.Server{}
|
||||
s := server.NewServer()
|
||||
s.RegisterName("Arith", new(Arith), "")
|
||||
s.RegisterName("PBArith", new(PBArith), "")
|
||||
go s.Serve("tcp", "127.0.0.1:0")
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func TestXClient_IT(t *testing.T) {
|
||||
s := server.Server{}
|
||||
s := server.NewServer()
|
||||
s.RegisterName("Arith", new(Arith), "")
|
||||
go s.Serve("tcp", "127.0.0.1:0")
|
||||
defer s.Close()
|
||||
|
||||
@@ -14,9 +14,16 @@ func init() {
|
||||
}
|
||||
|
||||
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 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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
if s.TLSConfig == nil {
|
||||
if s.tlsConfig == nil {
|
||||
ln, err = net.Listen("tcp", address)
|
||||
} else {
|
||||
ln, err = tls.Listen("tcp", address, s.TLSConfig)
|
||||
ln, err = tls.Listen("tcp", address, s.tlsConfig)
|
||||
}
|
||||
|
||||
return ln, err
|
||||
|
||||
39
server/option.go
Normal file
39
server/option.go
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,8 @@ func init() {
|
||||
}
|
||||
|
||||
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 quicconn.Listen("udp", address, s.TLSConfig)
|
||||
return quicconn.Listen("udp", address, s.tlsConfig)
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ var (
|
||||
// Server is rpcx server that use TCP or UDP.
|
||||
type Server struct {
|
||||
ln net.Listener
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
readTimeout time.Duration
|
||||
writeTimeout time.Duration
|
||||
|
||||
serviceMapMu sync.RWMutex
|
||||
serviceMap map[string]*service
|
||||
@@ -63,9 +63,9 @@ type Server struct {
|
||||
onShutdown []func()
|
||||
|
||||
// TLSConfig for creating tls tcp connection.
|
||||
TLSConfig *tls.Config
|
||||
tlsConfig *tls.Config
|
||||
// BlockCrypt for kcp.BlockCrypt
|
||||
Options map[string]interface{}
|
||||
options map[string]interface{}
|
||||
// // use for KCP
|
||||
// KCPConfig KCPConfig
|
||||
// // for QUIC
|
||||
@@ -78,11 +78,18 @@ type Server struct {
|
||||
}
|
||||
|
||||
// NewServer returns a server.
|
||||
func NewServer(options map[string]interface{}) *Server {
|
||||
return &Server{
|
||||
func NewServer(options ...OptionFn) *Server {
|
||||
s := &Server{
|
||||
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.
|
||||
@@ -227,10 +234,10 @@ func (s *Server) serveConn(conn net.Conn) {
|
||||
}()
|
||||
|
||||
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))
|
||||
}
|
||||
if d := s.WriteTimeout; d != 0 {
|
||||
if d := s.writeTimeout; d != 0 {
|
||||
conn.SetWriteDeadline(time.Now().Add(d))
|
||||
}
|
||||
if err := tlsConn.Handshake(); err != nil {
|
||||
@@ -245,8 +252,8 @@ func (s *Server) serveConn(conn net.Conn) {
|
||||
|
||||
for {
|
||||
t0 := time.Now()
|
||||
if s.ReadTimeout != 0 {
|
||||
conn.SetReadDeadline(t0.Add(s.ReadTimeout))
|
||||
if s.readTimeout != 0 {
|
||||
conn.SetReadDeadline(t0.Add(s.readTimeout))
|
||||
}
|
||||
|
||||
req, err := s.readRequest(ctx, r)
|
||||
@@ -261,8 +268,8 @@ func (s *Server) serveConn(conn net.Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
if s.WriteTimeout != 0 {
|
||||
conn.SetWriteDeadline(t0.Add(s.WriteTimeout))
|
||||
if s.writeTimeout != 0 {
|
||||
conn.SetWriteDeadline(t0.Add(s.writeTimeout))
|
||||
}
|
||||
|
||||
err = s.auth(ctx, req)
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestConsulRegistry(t *testing.T) {
|
||||
s := server.NewServer(nil)
|
||||
s := server.NewServer()
|
||||
|
||||
r := &ConsulRegisterPlugin{
|
||||
ServiceAddress: "tcp@127.0.0.1:8972",
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestEtcdRegistry(t *testing.T) {
|
||||
s := server.NewServer(nil)
|
||||
s := server.NewServer()
|
||||
|
||||
r := &EtcdRegisterPlugin{
|
||||
ServiceAddress: "tcp@127.0.0.1:8972",
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestZookeeperRegistry(t *testing.T) {
|
||||
s := server.NewServer(nil)
|
||||
s := server.NewServer()
|
||||
|
||||
r := &ZooKeeperRegisterPlugin{
|
||||
ServiceAddress: "tcp@127.0.0.1:8972",
|
||||
|
||||
Reference in New Issue
Block a user