Skip to content

Commit

Permalink
Reuse backoff waiting code in etcd clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Feb 27, 2024
1 parent a68454c commit bde0b08
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.
19 changes: 10 additions & 9 deletions backend_storage_etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,24 @@ func (s *backendStorageEtcd) EtcdClientCreated(client *EtcdClient) {
}()

go func() {
client.WaitForConnection()
if err := client.WaitForConnection(context.Background()); err != nil {
panic(err)
}

waitDelay := initialWaitDelay
backoff, err := NewExponentialBackoff(initialWaitDelay, maxWaitDelay)
if err != nil {
panic(err)
}
for {
response, err := s.getBackends(client, s.keyPrefix)
if err != nil {
if err == context.DeadlineExceeded {
log.Printf("Timeout getting initial list of backends, retry in %s", waitDelay)
log.Printf("Timeout getting initial list of backends, retry in %s", backoff.NextWait())
} else {
log.Printf("Could not get initial list of backends, retry in %s: %s", waitDelay, err)
log.Printf("Could not get initial list of backends, retry in %s: %s", backoff.NextWait(), err)
}

time.Sleep(waitDelay)
waitDelay = waitDelay * 2
if waitDelay > maxWaitDelay {
waitDelay = maxWaitDelay
}
backoff.Wait(context.Background())
continue
}

Expand Down
24 changes: 14 additions & 10 deletions etcd_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,26 +212,30 @@ func (c *EtcdClient) RemoveListener(listener EtcdClientListener) {
delete(c.listeners, listener)
}

func (c *EtcdClient) WaitForConnection() {
waitDelay := initialWaitDelay
func (c *EtcdClient) WaitForConnection(ctx context.Context) error {
backoff, err := NewExponentialBackoff(initialWaitDelay, maxWaitDelay)
if err != nil {
return err
}

for {
if err := ctx.Err(); err != nil {
return err
}

if err := c.syncClient(); err != nil {
if err == context.DeadlineExceeded {
log.Printf("Timeout waiting for etcd client to connect to the cluster, retry in %s", waitDelay)
log.Printf("Timeout waiting for etcd client to connect to the cluster, retry in %s", backoff.NextWait())
} else {
log.Printf("Could not sync etcd client with the cluster, retry in %s: %s", waitDelay, err)
log.Printf("Could not sync etcd client with the cluster, retry in %s: %s", backoff.NextWait(), err)
}

time.Sleep(waitDelay)
waitDelay = waitDelay * 2
if waitDelay > maxWaitDelay {
waitDelay = maxWaitDelay
}
backoff.Wait(ctx)
continue
}

log.Printf("Client synced, using endpoints %+v", c.getEtcdClient().Endpoints())
return
return nil
}
}

Expand Down
5 changes: 4 additions & 1 deletion etcd_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ func (l *EtcdClientTestListener) EtcdClientCreated(client *EtcdClient) {

go func() {
defer close(l.initial)
client.WaitForConnection()
if err := client.WaitForConnection(l.ctx); err != nil {
l.t.Errorf("error waiting for connection: %s", err)
return
}

ctx, cancel := context.WithTimeout(l.ctx, time.Second)
defer cancel()
Expand Down
4 changes: 3 additions & 1 deletion grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,9 @@ func (c *GrpcClients) EtcdClientCreated(client *EtcdClient) {
}()

go func() {
client.WaitForConnection()
if err := client.WaitForConnection(context.Background()); err != nil {
panic(err)
}

backoff, _ := NewExponentialBackoff(initialWaitDelay, maxWaitDelay)
for {
Expand Down
19 changes: 10 additions & 9 deletions proxy_config_etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,24 @@ func (p *proxyConfigEtcd) EtcdClientCreated(client *EtcdClient) {
}()

go func() {
client.WaitForConnection()
if err := client.WaitForConnection(context.Background()); err != nil {
panic(err)
}

waitDelay := initialWaitDelay
backoff, err := NewExponentialBackoff(initialWaitDelay, maxWaitDelay)
if err != nil {
panic(err)
}
for {
response, err := p.getProxyUrls(client, p.keyPrefix)
if err != nil {
if err == context.DeadlineExceeded {
log.Printf("Timeout getting initial list of proxy URLs, retry in %s", waitDelay)
log.Printf("Timeout getting initial list of proxy URLs, retry in %s", backoff.NextWait())
} else {
log.Printf("Could not get initial list of proxy URLs, retry in %s: %s", waitDelay, err)
log.Printf("Could not get initial list of proxy URLs, retry in %s: %s", backoff.NextWait(), err)
}

time.Sleep(waitDelay)
waitDelay = waitDelay * 2
if waitDelay > maxWaitDelay {
waitDelay = maxWaitDelay
}
backoff.Wait(context.Background())
continue
}

Expand Down

0 comments on commit bde0b08

Please sign in to comment.