feat(sdk): 心跳失败指数退避(1m..15m)
- heartbeatLoop 失败后指数退避、成功后恢复 normal interval - 新增退避计算测试
This commit is contained in:
3 files changed
+90
-9
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -188,6 +188,8 @@ type Client struct {
|
||||
lastWall atomic.Int64 // 最近观测到的本地墙钟(回拨检测)
|
||||
noCheckWarn sync.Once
|
||||
fromCache bool
|
||||
backoffBase time.Duration // 测试可缩短
|
||||
backoffMax time.Duration
|
||||
}
|
||||
|
||||
// trustedNow 返回经 server_time 校正的可信时间。
|
||||
|
||||
+51
-9
@@ -268,26 +268,68 @@ func (c *Client) enterGrace(gracePeriod int64, cause error) {
|
||||
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) {
|
||||
failures := 0
|
||||
delay := c.heartbeatDelay(failures)
|
||||
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()
|
||||
every := c.heartbeatEvery
|
||||
c.mu.Unlock()
|
||||
if every <= 0 {
|
||||
every = 900
|
||||
}
|
||||
select {
|
||||
case <-c.stop:
|
||||
return
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(time.Duration(every) * time.Second):
|
||||
if err := c.heartbeatOnce(ctx); err != nil {
|
||||
// 指数退避已由 GRACE deadline 兜底;循环内静默重试
|
||||
_ = err
|
||||
if failures == 0 {
|
||||
return time.Duration(every) * time.Second
|
||||
}
|
||||
return c.backoffDelay(failures)
|
||||
}
|
||||
|
||||
// backoffDelay 计算第 failures 次失败后的重试间隔(指数增长,封顶 15m)。
|
||||
func (c *Client) backoffDelay(failures int) time.Duration {
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user