@@ -54,7 +54,9 @@ type Client struct {
54
54
cfg Config
55
55
creds grpccredentials.TransportCredentials
56
56
resolver * resolver.EtcdManualResolver
57
- mu * sync.RWMutex
57
+
58
+ epMu * sync.RWMutex
59
+ endpoints []string
58
60
59
61
ctx context.Context
60
62
cancel context.CancelFunc
@@ -160,18 +162,18 @@ func (c *Client) Ctx() context.Context { return c.ctx }
160
162
// Endpoints lists the registered endpoints for the client.
161
163
func (c * Client ) Endpoints () []string {
162
164
// copy the slice; protect original endpoints from being changed
163
- c .mu .RLock ()
164
- defer c .mu .RUnlock ()
165
- eps := make ([]string , len (c .cfg . Endpoints ))
166
- copy (eps , c .cfg . Endpoints )
165
+ c .epMu .RLock ()
166
+ defer c .epMu .RUnlock ()
167
+ eps := make ([]string , len (c .endpoints ))
168
+ copy (eps , c .endpoints )
167
169
return eps
168
170
}
169
171
170
172
// SetEndpoints updates client's endpoints.
171
173
func (c * Client ) SetEndpoints (eps ... string ) {
172
- c .mu .Lock ()
173
- defer c .mu .Unlock ()
174
- c .cfg . Endpoints = eps
174
+ c .epMu .Lock ()
175
+ defer c .epMu .Unlock ()
176
+ c .endpoints = eps
175
177
176
178
c .resolver .SetEndpoints (eps )
177
179
}
@@ -296,7 +298,7 @@ func (c *Client) dial(creds grpccredentials.TransportCredentials, dopts ...grpc.
296
298
defer cancel () // TODO: Is this right for cases where grpc.WithBlock() is not set on the dial options?
297
299
}
298
300
299
- initialEndpoints := strings .Join (c .cfg . Endpoints , ";" )
301
+ initialEndpoints := strings .Join (c .Endpoints () , ";" )
300
302
target := fmt .Sprintf ("%s://%p/#initially=[%s]" , resolver .Schema , c , initialEndpoints )
301
303
conn , err := grpc .DialContext (dctx , target , opts ... )
302
304
if err != nil {
@@ -344,7 +346,7 @@ func newClient(cfg *Config) (*Client, error) {
344
346
creds : creds ,
345
347
ctx : ctx ,
346
348
cancel : cancel ,
347
- mu : new (sync.RWMutex ),
349
+ epMu : new (sync.RWMutex ),
348
350
callOpts : defaultCallOpts ,
349
351
lgMu : new (sync.RWMutex ),
350
352
}
@@ -390,6 +392,8 @@ func newClient(cfg *Config) (*Client, error) {
390
392
client .cancel ()
391
393
return nil , fmt .Errorf ("at least one Endpoint is required in client config" )
392
394
}
395
+ client .SetEndpoints (cfg .Endpoints ... )
396
+
393
397
// Use a provided endpoint target so that for https:// without any tls config given, then
394
398
// grpc will assume the certificate server name is the endpoint host.
395
399
conn , err := client .dialWithBalancer ()
0 commit comments