Skip to content

Commit

Permalink
clientv3/ordering: adjust violation count.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncming committed Mar 23, 2019
1 parent 7a5acb4 commit ebb559a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 7 additions & 5 deletions clientv3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}

Expand Down
7 changes: 6 additions & 1 deletion clientv3/ordering/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ var ErrNoGreaterRev = errors.New("etcdclient: no cluster members have a revision
func NewOrderViolationSwitchEndpointClosure(c clientv3.Client) OrderViolationFunc {
var mu sync.Mutex
violationCount := 0
prev := 0
return func(op clientv3.Op, resp clientv3.OpResponse, prevRev int64) error {
if violationCount > len(c.Endpoints()) {
if prev != len(c.Endpoints()) {
prev = len(c.Endpoints())
violationCount = 0
}
if violationCount >= len(c.Endpoints()) {
return ErrNoGreaterRev
}
mu.Lock()
Expand Down

0 comments on commit ebb559a

Please sign in to comment.