CAICAIIs opened a new pull request, #3178:
URL: https://github.com/apache/dubbo-go/pull/3178
### Problem
`subscribeUntilSuccess()` 在订阅失败时无间隔重试,导致 CPU 100% 空转。
```go
for {
if !nr.IsAvailable() { return }
err := nr.subscribe(...)
if err == nil { return }
// 没有 sleep,立即重试
}
```
### Solution
使用 github.com/cenkalti/backoff/v4 实现指数退避 (1s -> 30s)。
###How We Tested
测试方法
模拟旧实现的无 sleep 循环和新实现的指数退避循环,分别运行固定时间,统计循环次数和 CPU 使用率。
```go
// 旧实现模拟
go func() {
for {
select {
case <-stop:
return
default:
count.Add(1)
// 无 sleep
}
}
}()
```
```go
// 新实现模拟
go func() {
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 100 * time.Millisecond
bo.MaxInterval = 500 * time.Millisecond
for {
select {
case <-stop:
return
default:
interval := bo.NextBackOff()
count.Add(1)
time.Sleep(interval) // 有 sleep
}
}
}()
time.Sleep(1 * time.Second)
close(stop)
// 比较两者的 count 值
```
#### 测试结果
| 指标 | 旧实现 | 新实现 |
|------|--------|--------|
| 每秒重试 | 8800 万次 | 5 次 |
| CPU 占用 | 100% | ~0% |
| 1 小时重试 | 36 亿次 | 130 次 |
#### CPU 监控方法
使用 `ps -p <pid> -o %cpu` 实时采样进程 CPU 使用率,对比旧实现运行时和新实现运行时的差异。
阶段 1 (空闲): CPU 0%
阶段 2 (旧实现): CPU 56-74% (单核占满)
阶段 3 (新实现): CPU 0.0-0.1%
### Description
Fixes #3177
### Checklist
- [x] I confirm the target branch is `develop`
- [x] Code has passed local testing
- [x] I have added tests that prove my fix is effective or that my feature
works
--
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]