From f02711a9cb39e5b7cc6e94e2b728b216c0c82182 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 27 Feb 2021 23:56:18 +0800 Subject: [PATCH] golint core/discov (#525) --- core/discov/internal/etcdclient.go | 1 + core/discov/internal/listener.go | 1 + core/discov/internal/registry.go | 25 +++++++++++++++---------- core/discov/internal/updatelistener.go | 2 ++ core/discov/internal/vars.go | 11 ++++++++--- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/core/discov/internal/etcdclient.go b/core/discov/internal/etcdclient.go index b3ade160..4ce82cf2 100644 --- a/core/discov/internal/etcdclient.go +++ b/core/discov/internal/etcdclient.go @@ -9,6 +9,7 @@ import ( "google.golang.org/grpc" ) +// EtcdClient interface represents an etcd client. type EtcdClient interface { ActiveConnection() *grpc.ClientConn Close() error diff --git a/core/discov/internal/listener.go b/core/discov/internal/listener.go index 6340b1cb..3087410f 100644 --- a/core/discov/internal/listener.go +++ b/core/discov/internal/listener.go @@ -1,5 +1,6 @@ package internal +// Listener interface wraps the OnUpdate method. type Listener interface { OnUpdate(keys []string, values []string, newKey string) } diff --git a/core/discov/internal/registry.go b/core/discov/internal/registry.go index 679f998d..38e5765e 100644 --- a/core/discov/internal/registry.go +++ b/core/discov/internal/registry.go @@ -18,19 +18,31 @@ import ( ) var ( - registryInstance = Registry{ + registry = Registry{ clusters: make(map[string]*cluster), } connManager = syncx.NewResourceManager() ) +// A Registry is a registry that manages the etcd client connections. type Registry struct { clusters map[string]*cluster lock sync.Mutex } +// GetRegistry returns a global Registry. func GetRegistry() *Registry { - return ®istryInstance + return ®istry +} + +// GetConn returns an etcd client connection associated with given endpoints. +func (r *Registry) GetConn(endpoints []string) (EtcdClient, error) { + return r.getCluster(endpoints).getClient() +} + +// Monitor monitors the key on given etcd endpoints, notify with the given UpdateListener. +func (r *Registry) Monitor(endpoints []string, key string, l UpdateListener) error { + return r.getCluster(endpoints).monitor(key, l) } func (r *Registry) getCluster(endpoints []string) *cluster { @@ -46,14 +58,6 @@ func (r *Registry) getCluster(endpoints []string) *cluster { return c } -func (r *Registry) GetConn(endpoints []string) (EtcdClient, error) { - return r.getCluster(endpoints).getClient() -} - -func (r *Registry) Monitor(endpoints []string, key string, l UpdateListener) error { - return r.getCluster(endpoints).monitor(key, l) -} - type cluster struct { endpoints []string key string @@ -288,6 +292,7 @@ func (c *cluster) watchConnState(cli EtcdClient) { watcher.watch(cli.ActiveConnection()) } +// DialClient dials an etcd cluster with given endpoints. func DialClient(endpoints []string) (EtcdClient, error) { return clientv3.New(clientv3.Config{ Endpoints: endpoints, diff --git a/core/discov/internal/updatelistener.go b/core/discov/internal/updatelistener.go index 645469f3..535ceda1 100644 --- a/core/discov/internal/updatelistener.go +++ b/core/discov/internal/updatelistener.go @@ -3,11 +3,13 @@ package internal type ( + // A KV is used to store an etcd entry with key and value. KV struct { Key string Val string } + // UpdateListener wraps the OnAdd and OnDelete methods. UpdateListener interface { OnAdd(kv KV) OnDelete(kv KV) diff --git a/core/discov/internal/vars.go b/core/discov/internal/vars.go index 719612e6..77360b49 100644 --- a/core/discov/internal/vars.go +++ b/core/discov/internal/vars.go @@ -3,17 +3,22 @@ package internal import "time" const ( + // Delimiter is a separator that separates the etcd path. + Delimiter = '/' + autoSyncInterval = time.Minute coolDownInterval = time.Second dialTimeout = 5 * time.Second dialKeepAliveTime = 5 * time.Second requestTimeout = 3 * time.Second - Delimiter = '/' endpointsSeparator = "," ) var ( - DialTimeout = dialTimeout + // DialTimeout is the dial timeout. + DialTimeout = dialTimeout + // RequestTimeout is the request timeout. RequestTimeout = requestTimeout - NewClient = DialClient + // NewClient is used to create etcd clients. + NewClient = DialClient )