feat(sdk): 心跳失败指数退避(1m..15m)

- heartbeatLoop 失败后指数退避、成功后恢复 normal interval
- 新增退避计算测试
This commit is contained in:
iceking2nd committed 2026-11-14 21:40:31 +08:00
1 parent 5cc2d73cdd
commit dd0b290500
3 files changed
+90 -9

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
package sdk
import (
"testing"
"time"
)
func TestBackoffDelay_ExponentialAndCap(t *testing.T) {
c := &Client{}
cases := []struct {
failures int
want time.Duration
}{
{1, time.Minute},
{2, 2 * time.Minute},
{3, 4 * time.Minute},
{4, 8 * time.Minute},
{5, 15 * time.Minute}, // 16m 封顶 15m
{10, 15 * time.Minute},
}
for _, tc := range cases {
if got := c.backoffDelay(tc.failures); got != tc.want {
t.Errorf("failures=%d got=%s want=%s", tc.failures, got, tc.want)
}
}
}
func TestHeartbeatDelay_NormalInterval(t *testing.T) {
c := &Client{}
c.heartbeatEvery = 900
if got := c.heartbeatDelay(0); got != 15*time.Minute {
t.Errorf("正常间隔: %s", got)
}
if got := c.heartbeatDelay(1); got != time.Minute {
t.Errorf("首次失败退避: %s", got)
}
}
+2
View File
@@ -188,6 +188,8 @@ type Client struct {
lastWall atomic.Int64 // 最近观测到的本地墙钟(回拨检测) lastWall atomic.Int64 // 最近观测到的本地墙钟(回拨检测)
noCheckWarn sync.Once noCheckWarn sync.Once
fromCache bool fromCache bool
backoffBase time.Duration // 测试可缩短
backoffMax time.Duration
} }
// trustedNow 返回经 server_time 校正的可信时间。 // trustedNow 返回经 server_time 校正的可信时间。
+51 -9
View File
@@ -268,26 +268,68 @@ func (c *Client) enterGrace(gracePeriod int64, cause error) {
c.transition(s, StateGrace, c.opts.clock.Now()+gracePeriod) c.transition(s, StateGrace, c.opts.clock.Now()+gracePeriod)
} }
// 心跳失败后的指数退避(docs/design/07-sdk.md §6.3):1m 起、15m 封顶。
const (
defaultBackoffBase = time.Minute
defaultBackoffMax = 15 * time.Minute
)
func (c *Client) heartbeatLoop(ctx context.Context) { func (c *Client) heartbeatLoop(ctx context.Context) {
failures := 0
delay := c.heartbeatDelay(failures)
for { for {
select {
case <-c.stop:
return
case <-ctx.Done():
return
case <-time.After(delay):
if err := c.heartbeatOnce(ctx); err != nil {
failures++
delay = c.backoffDelay(failures)
continue
}
failures = 0
delay = c.heartbeatDelay(0)
}
}
}
// heartbeatDelay 返回正常心跳间隔(0 失败时)。
func (c *Client) heartbeatDelay(failures int) time.Duration {
c.mu.Lock() c.mu.Lock()
every := c.heartbeatEvery every := c.heartbeatEvery
c.mu.Unlock() c.mu.Unlock()
if every <= 0 { if every <= 0 {
every = 900 every = 900
} }
select { if failures == 0 {
case <-c.stop: return time.Duration(every) * time.Second
return }
case <-ctx.Done(): return c.backoffDelay(failures)
return }
case <-time.After(time.Duration(every) * time.Second):
if err := c.heartbeatOnce(ctx); err != nil { // backoffDelay 计算第 failures 次失败后的重试间隔(指数增长,封顶 15m)。
// 指数退避已由 GRACE deadline 兜底;循环内静默重试 func (c *Client) backoffDelay(failures int) time.Duration {
_ = err base := c.backoffBase
if base <= 0 {
base = defaultBackoffBase
}
maxDelay := c.backoffMax
if maxDelay <= 0 {
maxDelay = defaultBackoffMax
}
d := base
for i := 1; i < failures; i++ {
d *= 2
if d >= maxDelay {
return maxDelay
} }
} }
if d > maxDelay {
d = maxDelay
} }
return d
} }
func (c *Client) currentHash() string { func (c *Client) currentHash() string {