golint core/discov (#525)

master
Kevin Wan 4 years ago committed by GitHub
parent ad32f9de23
commit f02711a9cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
) )
// EtcdClient interface represents an etcd client.
type EtcdClient interface { type EtcdClient interface {
ActiveConnection() *grpc.ClientConn ActiveConnection() *grpc.ClientConn
Close() error Close() error

@ -1,5 +1,6 @@
package internal package internal
// Listener interface wraps the OnUpdate method.
type Listener interface { type Listener interface {
OnUpdate(keys []string, values []string, newKey string) OnUpdate(keys []string, values []string, newKey string)
} }

@ -18,19 +18,31 @@ import (
) )
var ( var (
registryInstance = Registry{ registry = Registry{
clusters: make(map[string]*cluster), clusters: make(map[string]*cluster),
} }
connManager = syncx.NewResourceManager() connManager = syncx.NewResourceManager()
) )
// A Registry is a registry that manages the etcd client connections.
type Registry struct { type Registry struct {
clusters map[string]*cluster clusters map[string]*cluster
lock sync.Mutex lock sync.Mutex
} }
// GetRegistry returns a global Registry.
func GetRegistry() *Registry { func GetRegistry() *Registry {
return &registryInstance return &registry
}
// 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 { func (r *Registry) getCluster(endpoints []string) *cluster {
@ -46,14 +58,6 @@ func (r *Registry) getCluster(endpoints []string) *cluster {
return c 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 { type cluster struct {
endpoints []string endpoints []string
key string key string
@ -288,6 +292,7 @@ func (c *cluster) watchConnState(cli EtcdClient) {
watcher.watch(cli.ActiveConnection()) watcher.watch(cli.ActiveConnection())
} }
// DialClient dials an etcd cluster with given endpoints.
func DialClient(endpoints []string) (EtcdClient, error) { func DialClient(endpoints []string) (EtcdClient, error) {
return clientv3.New(clientv3.Config{ return clientv3.New(clientv3.Config{
Endpoints: endpoints, Endpoints: endpoints,

@ -3,11 +3,13 @@
package internal package internal
type ( type (
// A KV is used to store an etcd entry with key and value.
KV struct { KV struct {
Key string Key string
Val string Val string
} }
// UpdateListener wraps the OnAdd and OnDelete methods.
UpdateListener interface { UpdateListener interface {
OnAdd(kv KV) OnAdd(kv KV)
OnDelete(kv KV) OnDelete(kv KV)

@ -3,17 +3,22 @@ package internal
import "time" import "time"
const ( const (
// Delimiter is a separator that separates the etcd path.
Delimiter = '/'
autoSyncInterval = time.Minute autoSyncInterval = time.Minute
coolDownInterval = time.Second coolDownInterval = time.Second
dialTimeout = 5 * time.Second dialTimeout = 5 * time.Second
dialKeepAliveTime = 5 * time.Second dialKeepAliveTime = 5 * time.Second
requestTimeout = 3 * time.Second requestTimeout = 3 * time.Second
Delimiter = '/'
endpointsSeparator = "," endpointsSeparator = ","
) )
var ( var (
DialTimeout = dialTimeout // DialTimeout is the dial timeout.
DialTimeout = dialTimeout
// RequestTimeout is the request timeout.
RequestTimeout = requestTimeout RequestTimeout = requestTimeout
NewClient = DialClient // NewClient is used to create etcd clients.
NewClient = DialClient
) )

Loading…
Cancel
Save