Переглянути джерело

clientv3: modify lock type.

johncming 6 роки тому
батько
коміт
9b5c468dc6
1 змінених файлів з 7 додано та 5 видалено
  1. 7 5
      clientv3/client.go

+ 7 - 5
clientv3/client.go

@@ -79,7 +79,7 @@ type Client struct {
 	creds         *credentials.TransportCredentials
 	balancer      balancer.Balancer
 	resolverGroup *endpoint.ResolverGroup
-	mu            *sync.Mutex
+	mu            *sync.RWMutex
 
 	ctx    context.Context
 	cancel context.CancelFunc
@@ -143,11 +143,13 @@ func (c *Client) Close() error {
 func (c *Client) Ctx() context.Context { return c.ctx }
 
 // Endpoints lists the registered endpoints for the client.
-func (c *Client) Endpoints() (eps []string) {
+func (c *Client) Endpoints() []string {
 	// copy the slice; protect original endpoints from being changed
-	eps = make([]string, len(c.cfg.Endpoints))
+	c.mu.RLock()
+	defer c.mu.RUnlock()
+	eps := make([]string, len(c.cfg.Endpoints))
 	copy(eps, c.cfg.Endpoints)
-	return
+	return eps
 }
 
 // SetEndpoints updates client's endpoints.
@@ -442,7 +444,7 @@ func newClient(cfg *Config) (*Client, error) {
 		creds:    creds,
 		ctx:      ctx,
 		cancel:   cancel,
-		mu:       new(sync.Mutex),
+		mu:       new(sync.RWMutex),
 		callOpts: defaultCallOpts,
 	}