Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Sep 30, 2023
1 parent 3c24c0d commit 40428fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
11 changes: 0 additions & 11 deletions etcd/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,6 @@ After the service is registered to `ETCD`, it will regularly check the status of

### Default Retry Config

```go
type retryCfg struct {
// The maximum number of call attempt times, including the initial call
maxAttemptTimes uint
// observeDelay is the delay time for checking the service status under normal conditions
observeDelay time.Duration
// The delay time of observing etcd key
retryDelay time.Duration
}
```

| Config Name | Default Value | Description |
|:--------------------|:-----------------|:------------------------------------------------------------------------------------------|
| WithMaxAttemptTimes | 5 | Used to set the maximum number of attempts, if 0, it means infinite attempts |
Expand Down
23 changes: 18 additions & 5 deletions etcd/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (e *etcdRegistry) Deregister(info *registry.Info) error {
}

func (e *etcdRegistry) grantLease() (clientv3.LeaseID, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*100)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
resp, err := e.etcdClient.Grant(ctx, e.leaseTTL)
if err != nil {
Expand Down Expand Up @@ -195,7 +195,15 @@ func (e *etcdRegistry) keepalive(meta registerMeta) error {

// keepRegister keep register service by retryConfig
func (e *etcdRegistry) keepRegister(key, val string, retryConfig *retryCfg) {
var failedTimes uint
var (
failedTimes uint
resp *clientv3.GetResponse
err error
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
)

delay := retryConfig.observeDelay
// if maxAttemptTimes is 0, keep register forever
for retryConfig.maxAttemptTimes == 0 || failedTimes < retryConfig.maxAttemptTimes {
Expand All @@ -209,9 +217,14 @@ func (e *etcdRegistry) keepRegister(key, val string, retryConfig *retryCfg) {
case <-time.After(delay):
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
resp, err := e.etcdClient.Get(ctx, key)
cancel()
wg.Add(1)
go func() {
defer wg.Done()
ctx, cancel = context.WithTimeout(context.Background(), time.Second*3)
resp, err = e.etcdClient.Get(ctx, key)
defer cancel()
}()
wg.Wait()

if err != nil {
hlog.Warnf("keep register get %s failed with err: %v", key, err)
Expand Down

0 comments on commit 40428fb

Please sign in to comment.