nanjiek commented on code in PR #3178:
URL: https://github.com/apache/dubbo-go/pull/3178#discussion_r2725498873
##########
registry/nacos/registry.go:
##########
@@ -200,15 +202,24 @@ func (nr *nacosRegistry) Subscribe(url *common.URL,
notifyListener registry.Noti
}
func (nr *nacosRegistry) subscribeUntilSuccess(url *common.URL, notifyListener
registry.NotifyListener) {
- // retry forever
- for {
+ bo := backoff.NewExponentialBackOff()
+ bo.InitialInterval = 1 * time.Second
+ bo.MaxInterval = 30 * time.Second
+ bo.MaxElapsedTime = 0 // never timeout, retry forever
+
+ operation := func() error {
if !nr.IsAvailable() {
- return
- }
- err := nr.subscribe(getSubscribeName(url), notifyListener)
- if err == nil {
- return
+ return backoff.Permanent(perrors.New("registry
unavailable"))
}
+ return nr.subscribe(getSubscribeName(url), notifyListener)
+ }
+
+ notify := func(err error, duration time.Duration) {
+ logger.Warnf("[Nacos Registry] subscribe failed, retry after
%v: %v", duration, err)
+ }
+
Review Comment:
“考虑到 nacosRegistry 有 done channel,建议配合 backoff.WithContext
使用,防止在重试休眠期间无法及时响应系统关闭信号。” 如果正处于 30s 的退避休眠期,即使进程关闭,它也要睡完这
30s。建议引入context,RetryNotifyWithContext 会监听 Context,一旦 done 关闭,立即退出。
```suggestion
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-nr.done
cancel()
}()
```
##########
registry/nacos/registry.go:
##########
@@ -200,15 +202,24 @@ func (nr *nacosRegistry) Subscribe(url *common.URL,
notifyListener registry.Noti
}
func (nr *nacosRegistry) subscribeUntilSuccess(url *common.URL, notifyListener
registry.NotifyListener) {
- // retry forever
- for {
+ bo := backoff.NewExponentialBackOff()
+ bo.InitialInterval = 1 * time.Second
+ bo.MaxInterval = 30 * time.Second
+ bo.MaxElapsedTime = 0 // never timeout, retry forever
+
+ operation := func() error {
if !nr.IsAvailable() {
- return
- }
- err := nr.subscribe(getSubscribeName(url), notifyListener)
- if err == nil {
- return
+ return backoff.Permanent(perrors.New("registry
unavailable"))
}
+ return nr.subscribe(getSubscribeName(url), notifyListener)
+ }
+
+ notify := func(err error, duration time.Duration) {
+ logger.Warnf("[Nacos Registry] subscribe failed, retry after
%v: %v", duration, err)
+ }
+
Review Comment:
“考虑到 nacosRegistry 有 done channel,建议配合 backoff.WithContext
使用,防止在重试休眠期间无法及时响应系统关闭信号。” 如果正处于 30s 的退避休眠期,即使进程关闭,它也要睡完这
30s。建议引入context,RetryNotifyWithContext 会监听 Context,一旦 done 关闭,立即退出。
```suggestion
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-nr.done
cancel()
}()
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]