From 36bb30e67ade7a72f063a9afbfffa7cf79120a52 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Tue, 28 Nov 2017 16:30:07 -0800 Subject: [PATCH] Creates a registration mechanism for RPC endpoints. --- agent/consul/server.go | 58 ++++++++++---------------------------- agent/consul/server_oss.go | 15 ++++++++++ 2 files changed, 30 insertions(+), 43 deletions(-) create mode 100644 agent/consul/server_oss.go diff --git a/agent/consul/server.go b/agent/consul/server.go index 09c13a8e7..5edc49294 100644 --- a/agent/consul/server.go +++ b/agent/consul/server.go @@ -112,9 +112,6 @@ type Server struct { // Connection pool to other consul servers connPool *pool.ConnPool - // Endpoints holds our RPC endpoints - endpoints endpoints - // eventChLAN is used to receive events from the // serf cluster in the datacenter eventChLAN chan serf.Event @@ -218,21 +215,6 @@ type Server struct { shutdownLock sync.Mutex } -// Holds the RPC endpoints -type endpoints struct { - ACL *ACL - Catalog *Catalog - Coordinate *Coordinate - Health *Health - Internal *Internal - KVS *KVS - Operator *Operator - PreparedQuery *PreparedQuery - Session *Session - Status *Status - Txn *Txn -} - func NewServer(config *Config) (*Server, error) { return NewServerLogger(config, nil, new(token.Store)) } @@ -624,33 +606,23 @@ func (s *Server) setupRaft() error { return nil } +// endpointFactory is a function that returns an RPC endpoint bound to the given +// server. +type factory func(s *Server) interface{} + +// endpoints is a list of registered RPC endpoint factories. +var endpoints []factory + +// registerEndpoint registers a new RPC endpoint factory. +func registerEndpoint(fn factory) { + endpoints = append(endpoints, fn) +} + // setupRPC is used to setup the RPC listener func (s *Server) setupRPC(tlsWrap tlsutil.DCWrapper) error { - // Create endpoints - s.endpoints.ACL = &ACL{s} - s.endpoints.Catalog = &Catalog{s} - s.endpoints.Coordinate = NewCoordinate(s) - s.endpoints.Health = &Health{s} - s.endpoints.Internal = &Internal{s} - s.endpoints.KVS = &KVS{s} - s.endpoints.Operator = &Operator{s} - s.endpoints.PreparedQuery = &PreparedQuery{s} - s.endpoints.Session = &Session{s} - s.endpoints.Status = &Status{s} - s.endpoints.Txn = &Txn{s} - - // Register the handlers - s.rpcServer.Register(s.endpoints.ACL) - s.rpcServer.Register(s.endpoints.Catalog) - s.rpcServer.Register(s.endpoints.Coordinate) - s.rpcServer.Register(s.endpoints.Health) - s.rpcServer.Register(s.endpoints.Internal) - s.rpcServer.Register(s.endpoints.KVS) - s.rpcServer.Register(s.endpoints.Operator) - s.rpcServer.Register(s.endpoints.PreparedQuery) - s.rpcServer.Register(s.endpoints.Session) - s.rpcServer.Register(s.endpoints.Status) - s.rpcServer.Register(s.endpoints.Txn) + for _, fn := range endpoints { + s.rpcServer.Register(fn(s)) + } ln, err := net.ListenTCP("tcp", s.config.RPCAddr) if err != nil { diff --git a/agent/consul/server_oss.go b/agent/consul/server_oss.go new file mode 100644 index 000000000..05c02e46c --- /dev/null +++ b/agent/consul/server_oss.go @@ -0,0 +1,15 @@ +package consul + +func init() { + registerEndpoint(func(s *Server) interface{} { return &ACL{s} }) + registerEndpoint(func(s *Server) interface{} { return &Catalog{s} }) + registerEndpoint(func(s *Server) interface{} { return NewCoordinate(s) }) + registerEndpoint(func(s *Server) interface{} { return &Health{s} }) + registerEndpoint(func(s *Server) interface{} { return &Internal{s} }) + registerEndpoint(func(s *Server) interface{} { return &KVS{s} }) + registerEndpoint(func(s *Server) interface{} { return &Operator{s} }) + registerEndpoint(func(s *Server) interface{} { return &PreparedQuery{s} }) + registerEndpoint(func(s *Server) interface{} { return &Session{s} }) + registerEndpoint(func(s *Server) interface{} { return &Status{s} }) + registerEndpoint(func(s *Server) interface{} { return &Txn{s} }) +}