From 69f15dbeff597628b84b7911ab141fbdaa820c04 Mon Sep 17 00:00:00 2001
From: "xg.gao" <xg.gao@tianrang-inc.com>
Date: Thu, 23 Jul 2020 12:07:26 +0800
Subject: [PATCH] code format

---
 remoting/etcdv3/client.go | 128 ++++++++++----------------------------
 1 file changed, 32 insertions(+), 96 deletions(-)

diff --git a/remoting/etcdv3/client.go b/remoting/etcdv3/client.go
index 7632a7cd0..a2a576453 100644
--- a/remoting/etcdv3/client.go
+++ b/remoting/etcdv3/client.go
@@ -35,7 +35,7 @@ import (
 )
 
 const (
-	// ConnDelay connection dalay
+	// ConnDelay connection delay
 	ConnDelay = 3
 	// MaxFailTimes max failure times
 	MaxFailTimes = 15
@@ -93,7 +93,6 @@ func WithHeartbeat(heartbeat int) Option {
 
 // ValidateClient validates client and sets options
 func ValidateClient(container clientFacade, opts ...Option) error {
-
 	options := &Options{
 		heartbeat: 1, // default heartbeat
 	}
@@ -118,7 +117,6 @@ func ValidateClient(container clientFacade, opts ...Option) error {
 
 	// Client lose connection with etcd server
 	if container.Client().rawClient == nil {
-
 		newClient, err := NewClient(options.name, options.endpoints, options.timeout, options.heartbeat)
 		if err != nil {
 			logger.Warnf("new etcd client (name{%s}, etcd addresses{%v}, timeout{%d}) = error{%v}",
@@ -136,7 +134,6 @@ func NewServiceDiscoveryClient(opts ...Option) *Client {
 	options := &Options{
 		heartbeat: 1, // default heartbeat
 	}
-
 	for _, opt := range opts {
 		opt(options)
 	}
@@ -171,7 +168,6 @@ type Client struct {
 
 // nolint
 func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat int) (*Client, error) {
-
 	ctx, cancel := context.WithCancel(context.Background())
 	rawClient, err := clientv3.New(clientv3.Config{
 		Context:     ctx,
@@ -184,7 +180,6 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
 	}
 
 	c := &Client{
-
 		name:      name,
 		timeout:   timeout,
 		endpoints: endpoints,
@@ -205,7 +200,6 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
 
 // NOTICE: need to get the lock before calling this method
 func (c *Client) clean() {
-
 	// close raw client
 	c.rawClient.Close()
 
@@ -217,7 +211,6 @@ func (c *Client) clean() {
 }
 
 func (c *Client) stop() bool {
-
 	select {
 	case <-c.exit:
 		return true
@@ -229,7 +222,6 @@ func (c *Client) stop() bool {
 
 // nolint
 func (c *Client) Close() {
-
 	if c == nil {
 		return
 	}
@@ -241,15 +233,14 @@ func (c *Client) Close() {
 	c.Wait.Wait()
 
 	c.lock.Lock()
+	defer c.lock.Unlock()
 	if c.rawClient != nil {
 		c.clean()
 	}
-	c.lock.Unlock()
 	logger.Warnf("etcd client{name:%s, endpoints:%s} exit now.", c.name, c.endpoints)
 }
 
 func (c *Client) maintenanceStatus() error {
-
 	s, err := concurrency.NewSession(c.rawClient, concurrency.WithTTL(c.heartbeat))
 	if err != nil {
 		return perrors.WithMessage(err, "new session with server")
@@ -262,7 +253,6 @@ func (c *Client) maintenanceStatus() error {
 }
 
 func (c *Client) maintenanceStatusLoop(s *concurrency.Session) {
-
 	defer func() {
 		c.Wait.Done()
 		logger.Infof("etcd client {endpoints:%v, name:%s} maintenance goroutine game over.", c.endpoints, c.name)
@@ -288,7 +278,6 @@ func (c *Client) maintenanceStatusLoop(s *concurrency.Session) {
 
 // if k not exist will put k/v in etcd, otherwise return nil
 func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error {
-
 	c.lock.RLock()
 	defer c.lock.RUnlock()
 
@@ -300,17 +289,12 @@ func (c *Client) put(k string, v string, opts ...clientv3.OpOption) error {
 		If(clientv3.Compare(clientv3.Version(k), "<", 1)).
 		Then(clientv3.OpPut(k, v, opts...)).
 		Commit()
-	if err != nil {
-		return err
-
-	}
-	return nil
+	return err
 }
 
 // if k not exist will put k/v in etcd
 // if k is already exist in etcd, replace it
 func (c *Client) update(k string, v string, opts ...clientv3.OpOption) error {
-
 	c.lock.RLock()
 	defer c.lock.RUnlock()
 
@@ -322,15 +306,10 @@ func (c *Client) update(k string, v string, opts ...clientv3.OpOption) error {
 		If(clientv3.Compare(clientv3.Version(k), "!=", -1)).
 		Then(clientv3.OpPut(k, v, opts...)).
 		Commit()
-	if err != nil {
-		return err
-
-	}
-	return nil
+	return err
 }
 
 func (c *Client) delete(k string) error {
-
 	c.lock.RLock()
 	defer c.lock.RUnlock()
 
@@ -339,15 +318,10 @@ func (c *Client) delete(k string) error {
 	}
 
 	_, err := c.rawClient.Delete(c.ctx, k)
-	if err != nil {
-		return err
-
-	}
-	return nil
+	return err
 }
 
 func (c *Client) get(k string) (string, error) {
-
 	c.lock.RLock()
 	defer c.lock.RUnlock()
 
@@ -369,7 +343,6 @@ func (c *Client) get(k string) (string, error) {
 
 // nolint
 func (c *Client) CleanKV() error {
-
 	c.lock.RLock()
 	defer c.lock.RUnlock()
 
@@ -378,15 +351,10 @@ func (c *Client) CleanKV() error {
 	}
 
 	_, err := c.rawClient.Delete(c.ctx, "", clientv3.WithPrefix())
-	if err != nil {
-		return err
-	}
-
-	return nil
+	return err
 }
 
 func (c *Client) getChildren(k string) ([]string, []string, error) {
-
 	c.lock.RLock()
 	defer c.lock.RUnlock()
 
@@ -403,21 +371,16 @@ func (c *Client) getChildren(k string) ([]string, []string, error) {
 		return nil, nil, ErrKVPairNotFound
 	}
 
-	var (
-		kList []string
-		vList []string
-	)
-
+	kList := make([]string, 0, len(resp.Kvs))
+	vList := make([]string, 0, len(resp.Kvs))
 	for _, kv := range resp.Kvs {
 		kList = append(kList, string(kv.Key))
 		vList = append(vList, string(kv.Value))
 	}
-
 	return kList, vList, nil
 }
 
 func (c *Client) watchWithPrefix(prefix string) (clientv3.WatchChan, error) {
-
 	c.lock.RLock()
 	defer c.lock.RUnlock()
 
@@ -429,7 +392,6 @@ func (c *Client) watchWithPrefix(prefix string) (clientv3.WatchChan, error) {
 }
 
 func (c *Client) watch(k string) (clientv3.WatchChan, error) {
-
 	c.lock.RLock()
 	defer c.lock.RUnlock()
 
@@ -441,7 +403,6 @@ func (c *Client) watch(k string) (clientv3.WatchChan, error) {
 }
 
 func (c *Client) keepAliveKV(k string, v string) error {
-
 	c.lock.RLock()
 	defer c.lock.RUnlock()
 
@@ -457,14 +418,16 @@ func (c *Client) keepAliveKV(k string, v string) error {
 	keepAlive, err := c.rawClient.KeepAlive(c.ctx, lease.ID)
 	if err != nil || keepAlive == nil {
 		c.rawClient.Revoke(c.ctx, lease.ID)
-		return perrors.WithMessage(err, "keep alive lease")
+		if err != nil {
+			return perrors.WithMessage(err, "keep alive lease")
+		} else {
+			return perrors.New("keep alive lease")
+		}
 	}
 
 	_, err = c.rawClient.Put(c.ctx, k, v, clientv3.WithLease(lease.ID))
-	if err != nil {
-		return perrors.WithMessage(err, "put k/v with lease")
-	}
-	return nil
+	err = perrors.WithMessage(err, "put k/v with lease")
+	return err
 }
 
 // nolint
@@ -481,92 +444,65 @@ func (c *Client) Valid() bool {
 	}
 
 	c.lock.RLock()
+	defer c.lock.RUnlock()
 	if c.rawClient == nil {
-		c.lock.RUnlock()
 		return false
 	}
-	c.lock.RUnlock()
 	return true
 }
 
 // nolint
 func (c *Client) Create(k string, v string) error {
-
 	err := c.put(k, v)
-	if err != nil {
-		return perrors.WithMessagef(err, "put k/v (key: %s value %s)", k, v)
-	}
-	return nil
+	err = perrors.WithMessagef(err, "put k/v (key: %s value %s)", k, v)
+	return err
 }
 
 // Update key value ...
 func (c *Client) Update(k, v string) error {
 	err := c.update(k, v)
-	if err != nil {
-		return perrors.WithMessagef(err, "Update k/v (key: %s value %s)", k, v)
-	}
-	return nil
+	err = perrors.WithMessagef(err, "Update k/v (key: %s value %s)", k, v)
+	return err
 }
 
 // nolint
 func (c *Client) Delete(k string) error {
-
 	err := c.delete(k)
-	if err != nil {
-		return perrors.WithMessagef(err, "delete k/v (key %s)", k)
-	}
-
-	return nil
+	err = perrors.WithMessagef(err, "delete k/v (key %s)", k)
+	return err
 }
 
 // RegisterTemp registers a temporary node
 func (c *Client) RegisterTemp(k, v string) error {
-
 	err := c.keepAliveKV(k, v)
-	if err != nil {
-		return perrors.WithMessagef(err, "keepalive kv (key %s)", k)
-	}
-
-	return nil
+	err = perrors.WithMessagef(err, "keepalive kv (key %s)", k)
+	return err
 }
 
 // GetChildrenKVList gets children kv list by @k
 func (c *Client) GetChildrenKVList(k string) ([]string, []string, error) {
-
 	kList, vList, err := c.getChildren(k)
-	if err != nil {
-		return nil, nil, perrors.WithMessagef(err, "get key children (key %s)", k)
-	}
-	return kList, vList, nil
+	err = perrors.WithMessagef(err, "get key children (key %s)", k)
+	return kList, vList, err
 }
 
 // Get gets value by @k
 func (c *Client) Get(k string) (string, error) {
-
 	v, err := c.get(k)
-	if err != nil {
-		return "", perrors.WithMessagef(err, "get key value (key %s)", k)
-	}
-
-	return v, nil
+	err = perrors.WithMessagef(err, "get key value (key %s)", k)
+	return v, err
 }
 
 // Watch watches on spec key
 func (c *Client) Watch(k string) (clientv3.WatchChan, error) {
-
 	wc, err := c.watch(k)
-	if err != nil {
-		return nil, perrors.WithMessagef(err, "watch prefix (key %s)", k)
-	}
-	return wc, nil
+	err = perrors.WithMessagef(err, "watch prefix (key %s)", k)
+	return wc, err
 }
 
 // WatchWithPrefix watches on spec prefix
 func (c *Client) WatchWithPrefix(prefix string) (clientv3.WatchChan, error) {
-
 	wc, err := c.watchWithPrefix(prefix)
-	if err != nil {
-		return nil, perrors.WithMessagef(err, "watch prefix (key %s)", prefix)
-	}
-	return wc, nil
+	err = perrors.WithMessagef(err, "watch prefix (key %s)", prefix)
+	return wc, err
 }
-- 
GitLab