阶段三:完成生命周期、Engine 和 Automation 重新验收
This commit is contained in:
1 parent
3cbe73b560
commit
4227e3dd56
20 files changed
+398
-127
No files matched your search
@@ -344,7 +344,6 @@ func New(cfg Config) *Account {
|
||||
Reply: a.reply,
|
||||
Logger: logger,
|
||||
BeginTask: a.lifecycle.beginTask,
|
||||
FinishTask: a.lifecycle.finishTask,
|
||||
RecordMessage: a.recordMessageReceived,
|
||||
})
|
||||
// connection 保存绑定当前账号 facade 的连接编排组件;它只在构造完成后才可被 Run 调用。
|
||||
@@ -449,8 +448,8 @@ func (a *Account) StopContext(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// beginTask 封装begin任务业务协调。
|
||||
func (a *Account) beginTask() (context.Context, bool) {
|
||||
// beginTask 封装账户所属任务的 Context、释放函数和生命周期接纳结果。
|
||||
func (a *Account) beginTask() (context.Context, func(), bool) {
|
||||
return a.lifecycle.beginTask()
|
||||
}
|
||||
|
||||
|
||||
@@ -1093,6 +1093,38 @@ func TestUpdateCookie_AcceptsAuthoritativeEmptySnapshot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateCookieContextRejectsCanceledCall 验证请求取消后不会继续收口运行时 Cookie 或清理 Token。
|
||||
func TestUpdateCookieContextRejectsCanceledCall(t *testing.T) {
|
||||
// acc、store、cleanup 用于本次流程后续判断的账号、数据库和资源释放函数。
|
||||
acc, _, store, cleanup := newAccountForTest(t)
|
||||
defer cleanup()
|
||||
// original 保存取消前的运行时 Cookie,作为拒绝副作用的基线。
|
||||
original := acc.currentCookieStr()
|
||||
// saveErr 保存预置旧 Token 缓存失败的原因;取消同步不得清理此缓存。
|
||||
saveErr := store.Tokens.Save(context.Background(), acc.CookieID, "device", "old-token", time.Now().Add(time.Hour).Unix())
|
||||
if saveErr != nil {
|
||||
t.Fatal(saveErr)
|
||||
}
|
||||
// ctx、cancel 保存已取消的调用上下文,模拟 HTTP 请求或应用任务提前结束。
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
// err 保存凭证协调器响应取消后的同步错误。
|
||||
err := acc.UpdateCookieContext(ctx, "unb=123; _m_h5_tk=late-update;")
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("取消的 Cookie 同步错误=%v,期望 context.Canceled", err)
|
||||
}
|
||||
// got 保存取消后的运行时 Cookie,必须保持为同步前的快照。
|
||||
got := acc.currentCookieStr()
|
||||
if got != original {
|
||||
t.Fatalf("取消的 Cookie 同步不应改写内存凭证: got %q want %q", got, original)
|
||||
}
|
||||
// token、tokenErr 保存取消后读取的 Token 缓存及其查询错误。
|
||||
token, tokenErr := store.Tokens.Get(context.Background(), acc.CookieID)
|
||||
if tokenErr != nil || token.AccessToken != "old-token" {
|
||||
t.Fatalf("取消的 Cookie 同步不应触碰 Token 缓存: token=%+v err=%v", token, tokenErr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReloadCookieFromDBDetectsMetadataOnlyCredentialRotation 封装TestReload登录凭证FromDBDetectsMetadataOnlyCredentialRotation业务协调。
|
||||
func TestReloadCookieFromDBDetectsMetadataOnlyCredentialRotation(t *testing.T) {
|
||||
// acc、store、cleanup 用于本次流程后续判断的acc、store、cleanup
|
||||
|
||||
@@ -2,9 +2,14 @@ package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// bootstrapTaskTimeout 是账号尚未 Run 时同步初始化任务的最大存活时间;它不能替代正式运行生命周期。
|
||||
const bootstrapTaskTimeout = time.Minute
|
||||
|
||||
// accountLifecycle 管理单账号运行上下文和业务任务生命周期。
|
||||
// mu 保护 stopFn、stopped、runtimeCtx、accepting、任务计数和停止完成信号;
|
||||
// 持有 mu 时只做内存操作,不执行 I/O。Stop 先禁止新任务、再取消上下文,最后等待已有任务。
|
||||
@@ -50,7 +55,7 @@ func (l *accountLifecycle) start(ctx context.Context, cancel context.CancelFunc)
|
||||
// stopContext 原子地禁止新任务并取出取消函数;并发停止调用会受 ctx 限制地等待首次清理完成。
|
||||
func (l *accountLifecycle) stopContext(ctx context.Context) (context.CancelFunc, bool, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
return nil, false, errors.New("停止账号需要关闭 Context")
|
||||
}
|
||||
l.mu.Lock()
|
||||
if l.stopped {
|
||||
@@ -80,22 +85,40 @@ func (l *accountLifecycle) stopContext(ctx context.Context) (context.CancelFunc,
|
||||
}
|
||||
|
||||
// beginTask 在生命周期锁内登记业务任务,避免 Stop 与 WaitGroup.Add 竞争。
|
||||
func (l *accountLifecycle) beginTask() (context.Context, bool) {
|
||||
// 返回的 finish 必须由获准任务恰好调用一次;它同时释放 bootstrap Context 的计时器。
|
||||
func (l *accountLifecycle) beginTask() (context.Context, func(), bool) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if !l.accepting {
|
||||
return nil, false
|
||||
l.mu.Unlock()
|
||||
return nil, nil, false
|
||||
}
|
||||
// ctx 是新业务任务继承的账号运行上下文。
|
||||
ctx := l.runtimeCtx
|
||||
// cancel 是仅在尚未进入 Run 的兼容任务中停止有限 Context 定时器的函数。
|
||||
cancel := func() {}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
// bootstrapCtx 是仅供尚未进入 Run 的同步初始化与确定性测试使用的有限任务 Context。
|
||||
bootstrapCtx, bootstrapCancel := context.WithTimeout(context.Background(), bootstrapTaskTimeout)
|
||||
ctx = bootstrapCtx
|
||||
cancel = bootstrapCancel
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return nil, false
|
||||
l.mu.Unlock()
|
||||
cancel()
|
||||
return nil, nil, false
|
||||
}
|
||||
l.activeTasks++
|
||||
return ctx, true
|
||||
l.mu.Unlock()
|
||||
// once 保证重复 defer 或错误路径不会重复递减任务计数或关闭 stopDone。
|
||||
var once sync.Once
|
||||
// finish 结束当前任务的生命周期登记,并释放仅供该任务使用的 bootstrap 计时器。
|
||||
finish := func() {
|
||||
once.Do(func() {
|
||||
cancel()
|
||||
l.finishTask()
|
||||
})
|
||||
}
|
||||
return ctx, finish, true
|
||||
}
|
||||
|
||||
// finishTask 标记一个已登记的业务任务退出。
|
||||
@@ -115,7 +138,7 @@ func (l *accountLifecycle) finishTask() {
|
||||
// stopDone 由最后一个任务关闭,因而超时返回不遗留等待协程;调用方可用新的上下文再次等待同一信号。
|
||||
func (l *accountLifecycle) waitContext(ctx context.Context) bool {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
return false
|
||||
}
|
||||
l.mu.Lock()
|
||||
// done 是 Stop 为当前收束周期创建、由最后一个已登记任务关闭的共享信号。
|
||||
|
||||
@@ -15,8 +15,8 @@ func TestAccountStopWaitsForTaskAndConcurrentStop(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
account.lifecycle.start(ctx, cancel)
|
||||
// taskCtx 是已登记任务拿到的上下文;ok 表示任务成功进入生命周期。
|
||||
taskCtx, ok := account.beginTask()
|
||||
// taskCtx、finish、ok 分别是已登记任务的上下文、释放函数与生命周期接纳结果。
|
||||
taskCtx, finish, ok := account.beginTask()
|
||||
if !ok || taskCtx == nil {
|
||||
t.Fatal("业务任务应在 Stop 前成功登记")
|
||||
}
|
||||
@@ -39,7 +39,7 @@ func TestAccountStopWaitsForTaskAndConcurrentStop(t *testing.T) {
|
||||
t.Fatal("并发 Stop 在已登记任务完成前提前返回")
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
account.lifecycle.finishTask()
|
||||
finish()
|
||||
select {
|
||||
case <-firstDone:
|
||||
case <-time.After(time.Second):
|
||||
@@ -50,9 +50,9 @@ func TestAccountStopWaitsForTaskAndConcurrentStop(t *testing.T) {
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("并发 Stop 未等待第一次 Stop 完成")
|
||||
}
|
||||
// stoppedCtx 是 Stop 后尝试登记任务得到的上下文;stoppedOK 必须为 false。
|
||||
stoppedCtx, stoppedOK := account.beginTask()
|
||||
if stoppedOK || stoppedCtx != nil {
|
||||
// stoppedCtx、stoppedFinish、stoppedOK 分别是停止后任务的上下文、释放函数与接纳结果。
|
||||
stoppedCtx, stoppedFinish, stoppedOK := account.beginTask()
|
||||
if stoppedOK || stoppedCtx != nil || stoppedFinish != nil {
|
||||
t.Fatal("Stop 后不应再接受业务任务")
|
||||
}
|
||||
}
|
||||
@@ -65,8 +65,9 @@ func TestAccountStopContextBoundsTaskWait(t *testing.T) {
|
||||
runCtx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
account.lifecycle.start(runCtx, cancel)
|
||||
// ok 表示测试业务任务是否成功登记。
|
||||
if _, ok := account.beginTask(); !ok {
|
||||
// taskCtx、finish、ok 分别是测试业务任务的上下文、释放函数与接纳结果。
|
||||
taskCtx, finish, ok := account.beginTask()
|
||||
if !ok || taskCtx == nil || finish == nil {
|
||||
t.Fatal("业务任务应成功登记")
|
||||
}
|
||||
// stopCtx 是刻意很短的停止上下文,用于验证有界等待。
|
||||
@@ -93,7 +94,7 @@ func TestAccountStopContextBoundsTaskWait(t *testing.T) {
|
||||
t.Fatal("第一次 Stop 超时后,重试不应在任务完成前返回")
|
||||
case <-time.After(20 * time.Millisecond):
|
||||
}
|
||||
account.lifecycle.finishTask()
|
||||
finish()
|
||||
select {
|
||||
// retryErr 表示第一次超时后再次停止账号的等待结果。
|
||||
case retryErr := <-retryDone:
|
||||
@@ -115,9 +116,9 @@ func TestAccountLifecycleWaitContextCanRetryAfterTimeout(t *testing.T) {
|
||||
if !lifecycle.start(runCtx, cancel) {
|
||||
t.Fatal("生命周期应允许首次启动")
|
||||
}
|
||||
// taskCtx 是登记任务继承的运行上下文;accepted 表示任务已成功进入停止收束范围。
|
||||
taskCtx, accepted := lifecycle.beginTask()
|
||||
if !accepted || taskCtx == nil {
|
||||
// taskCtx、finish、accepted 分别是登记任务的上下文、释放函数与停止收束接纳结果。
|
||||
taskCtx, finish, accepted := lifecycle.beginTask()
|
||||
if !accepted || taskCtx == nil || finish == nil {
|
||||
t.Fatal("业务任务应成功登记")
|
||||
}
|
||||
// shouldStop 表示本次调用创建了停止收束信号;stopErr 表示停止切换错误。
|
||||
@@ -141,7 +142,7 @@ func TestAccountLifecycleWaitContextCanRetryAfterTimeout(t *testing.T) {
|
||||
t.Fatal("任务未完成时,重试等待不应提前返回")
|
||||
case <-time.After(20 * time.Millisecond):
|
||||
}
|
||||
lifecycle.finishTask()
|
||||
finish()
|
||||
select {
|
||||
// completed 表示第二次等待在任务收束后得到正常完成结果。
|
||||
case completed := <-waited:
|
||||
@@ -168,9 +169,9 @@ func TestAccountLifecycleRejectsLateStart(t *testing.T) {
|
||||
if lifecycle.start(runCtx, cancel) {
|
||||
t.Fatal("Stop 先于 Run 时不应重新启动生命周期")
|
||||
}
|
||||
// taskCtx 是停止后尝试登记业务任务得到的上下文,必须被拒绝。
|
||||
taskCtx, accepted := lifecycle.beginTask()
|
||||
if accepted || taskCtx != nil {
|
||||
// taskCtx、finish、accepted 分别是停止后任务的上下文、释放函数与接纳结果。
|
||||
taskCtx, finish, accepted := lifecycle.beginTask()
|
||||
if accepted || taskCtx != nil || finish != nil {
|
||||
t.Fatalf("停止后的生命周期不应接收任务:ctx=%v accepted=%v", taskCtx, accepted)
|
||||
}
|
||||
}
|
||||
@@ -139,9 +139,28 @@ func (a *Account) replaceCredentialState(cookieStr, credentialFP string) {
|
||||
a.credentials.replaceCredentialState(cookieStr, credentialFP)
|
||||
}
|
||||
|
||||
// UpdateCookie 是外部刷新 Cookie 同步的稳定导出入口,并委托给凭证协调器。
|
||||
// runtimeCookieUpdateTimeout 是旧无 Context Cookie 同步入口的最长数据库与刷新门等待预算。
|
||||
const runtimeCookieUpdateTimeout = 10 * time.Second
|
||||
|
||||
// UpdateCookie 是外部刷新 Cookie 同步的兼容入口。新调用方应使用 UpdateCookieContext
|
||||
// 传入请求或应用任务的 Context;该入口只为尚未迁移的内部回调提供受限预算。
|
||||
func (a *Account) UpdateCookie(cookieStr string) {
|
||||
a.credentials.updateCookie(cookieStr)
|
||||
// updateCtx、updateCancel 为历史无 Context 调用创建有限同步预算,避免等待刷新门时无限阻塞。
|
||||
updateCtx, updateCancel := context.WithTimeout(context.Background(), runtimeCookieUpdateTimeout)
|
||||
defer updateCancel()
|
||||
// updateErr 保存受限兼容同步在取消、数据库读取或刷新门等待时的失败原因。
|
||||
if updateErr := a.UpdateCookieContext(updateCtx, cookieStr); updateErr != nil {
|
||||
a.logger.Warn("同步运行时 Cookie 失败", "err", updateErr)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateCookieContext 用调用方 Context 同步已持久化的 Cookie、内存凭证快照与 Token 缓存。
|
||||
// 它不创建后台 worker;调用返回前完成全部状态收口,因此不受账号 Run 停止 fencing 影响。
|
||||
func (a *Account) UpdateCookieContext(ctx context.Context, cookieStr string) error {
|
||||
if ctx == nil {
|
||||
return errors.New("同步运行时 Cookie 需要调用 Context")
|
||||
}
|
||||
return a.credentials.updateCookie(ctx, cookieStr)
|
||||
}
|
||||
|
||||
// tryLoginStatusCheck 调用 mtop.taobao.idlemessage.pc.loginuser.get 做轻量登录态确认。
|
||||
@@ -499,7 +518,7 @@ func (c *credentialCoordinator) persistRenewFlatCookie(ctx context.Context, newC
|
||||
func (c *credentialCoordinator) watchPendingAPIRenew(parent context.Context, result *renew.Result) {
|
||||
// a 是本凭证协调器绑定的账号 facade,提供生命周期任务登记。
|
||||
a := c.account
|
||||
a.pendingRenewal.watch(parent, a.beginTask, a.lifecycle.finishTask, result, a.persistPendingRenewCookies, a.logger)
|
||||
a.pendingRenewal.watch(parent, a.beginTask, result, a.persistPendingRenewCookies, a.logger)
|
||||
}
|
||||
|
||||
// persistPendingRenewCookies 封装persistPendingRenewCookies业务协调。
|
||||
@@ -1099,18 +1118,17 @@ func (c *credentialCoordinator) replaceCredentialState(cookieStr, credentialFP s
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateCookie 用外部刷新得到的新 cookie 更新运行时状态。
|
||||
func (c *credentialCoordinator) updateCookie(cookieStr string) {
|
||||
// updateCookie 用外部刷新得到的新 Cookie 更新运行时状态,并在调用方 Context 到期时停止等待。
|
||||
func (c *credentialCoordinator) updateCookie(ctx context.Context, cookieStr string) error {
|
||||
// a 是本凭证协调器绑定的账号 facade,提供权威 Cookie repository 与刷新门。
|
||||
a := c.account
|
||||
if strings.TrimSpace(cookieStr) == "" && (a.store == nil || a.store.Cookies == nil) {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
// releaseRefreshGate 释放运行时 Cookie 同步占用的通道令牌。
|
||||
releaseRefreshGate, gateErr := a.acquireRefreshGate(context.Background())
|
||||
releaseRefreshGate, gateErr := a.acquireRefreshGate(ctx)
|
||||
if gateErr != nil {
|
||||
a.logger.Warn("同步运行时 Cookie 时刷新通道被取消", "err", gateErr)
|
||||
return
|
||||
return fmt.Errorf("获取运行时 Cookie 同步门: %w", gateErr)
|
||||
}
|
||||
defer releaseRefreshGate()
|
||||
// credentialUnlock 用于本次流程后续判断的credentialUnlock
|
||||
@@ -1125,16 +1143,15 @@ func (c *credentialCoordinator) updateCookie(cookieStr string) {
|
||||
// metadataJSON 用于本次流程后续判断的metadataJSON
|
||||
metadataJSON := ""
|
||||
if a.store != nil && a.store.Cookies != nil {
|
||||
runtimeData, err := a.store.Cookies.GetCookieRuntimeData(context.Background(), a.CookieID) // runtimeData 只包含同步运行时 Cookie 所需的 Cookie 与 metadata。
|
||||
// runtimeData、err 分别是账号运行时的权威 Cookie 快照及读取失败;查询继承调用方取消。
|
||||
runtimeData, err := a.store.Cookies.GetCookieRuntimeData(ctx, a.CookieID)
|
||||
if err != nil {
|
||||
a.logger.Warn("同步运行时 Cookie 前读取数据库失败", "err", err)
|
||||
return
|
||||
return fmt.Errorf("读取运行时 Cookie: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(runtimeData.Value) == "" {
|
||||
if // complete 用于本次流程后续判断的complete
|
||||
_, complete := cookierefresh.SnapshotFromMetadataOK(runtimeData.MetadataJSON); !complete {
|
||||
a.logger.Warn("同步运行时 Cookie 时数据库值为空且无权威 Jar")
|
||||
return
|
||||
return errors.New("同步运行时 Cookie 时数据库值为空且无权威 Jar")
|
||||
}
|
||||
}
|
||||
cookieStr = runtimeData.Value
|
||||
@@ -1147,10 +1164,11 @@ func (c *credentialCoordinator) updateCookie(cookieStr string) {
|
||||
changed := credentialFP != a.credentialFP
|
||||
a.mu.Unlock()
|
||||
if !changed {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
a.replaceCredentialState(cookieStr, credentialFP)
|
||||
// Cookie Jar 的普通更新不会打断已经认证的 IMPaaS 连接。新 Cookie
|
||||
// 会在下一次自然重连前被重新读取并用于获取新的 accessToken。
|
||||
a.clearTokenCache(context.Background())
|
||||
a.clearTokenCache(ctx)
|
||||
return nil
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -58,8 +59,7 @@ func newRefreshGate() chan struct{} {
|
||||
// 因此停止账号不会被另一个尚未完成的续期流程无限阻塞。
|
||||
func (s *credentialState) acquireRefreshGate(ctx context.Context) (func(), error) {
|
||||
if ctx == nil {
|
||||
// ctx 使用 Background 保持历史未传取消信号调用方的可用性。
|
||||
ctx = context.Background()
|
||||
return nil, errors.New("获取账号刷新门需要生命周期 Context")
|
||||
}
|
||||
s.mu.Lock()
|
||||
if s.refreshGate == nil {
|
||||
|
||||
@@ -34,10 +34,8 @@ type messageDispatcher struct {
|
||||
reply *ReplyService
|
||||
// logger 记录消息分发和防抖错误。
|
||||
logger *slog.Logger
|
||||
// beginTask 登记账号生命周期任务。
|
||||
beginTask func() (context.Context, bool)
|
||||
// finishTask 完成账号生命周期任务登记。
|
||||
finishTask func()
|
||||
// beginTask 登记账号生命周期任务,并返回该任务唯一的释放函数。
|
||||
beginTask func() (context.Context, func(), bool)
|
||||
// recordMessage 更新账号最近收消息时间。
|
||||
recordMessage func(time.Time)
|
||||
}
|
||||
@@ -54,10 +52,8 @@ type messageDispatcherConfig struct {
|
||||
Reply *ReplyService
|
||||
// Logger 记录分发过程中的错误和诊断信息。
|
||||
Logger *slog.Logger
|
||||
// BeginTask 登记账号生命周期任务。
|
||||
BeginTask func() (context.Context, bool)
|
||||
// FinishTask 完成账号生命周期任务登记。
|
||||
FinishTask func()
|
||||
// BeginTask 登记账号生命周期任务,并返回该任务唯一的释放函数。
|
||||
BeginTask func() (context.Context, func(), bool)
|
||||
// RecordMessage 更新最近收到消息时间。
|
||||
RecordMessage func(time.Time)
|
||||
}
|
||||
@@ -77,12 +73,12 @@ func newMessageDispatcher(config messageDispatcherConfig) messageDispatcher {
|
||||
// beginTask 用于本次流程后续判断的begin任务
|
||||
beginTask := config.BeginTask
|
||||
if beginTask == nil {
|
||||
beginTask = func() (context.Context, bool) { return context.Background(), true }
|
||||
}
|
||||
// finishTask 用于本次流程后续判断的finish任务
|
||||
finishTask := config.FinishTask
|
||||
if finishTask == nil {
|
||||
finishTask = func() {}
|
||||
// 缺少生命周期登记器时使用受限同步兼容预算,测试构造不能产生长期游离任务。
|
||||
beginTask = func() (context.Context, func(), bool) {
|
||||
// fallbackCtx 是没有账号 owner 时仅供一次消息分发使用的有限 Context。
|
||||
fallbackCtx, fallbackCancel := context.WithTimeout(context.Background(), bootstrapTaskTimeout)
|
||||
return fallbackCtx, fallbackCancel, true
|
||||
}
|
||||
}
|
||||
// recordMessage 用于本次流程后续判断的record消息
|
||||
recordMessage := config.RecordMessage
|
||||
@@ -104,15 +100,14 @@ func newMessageDispatcher(config messageDispatcherConfig) messageDispatcher {
|
||||
reply: config.Reply,
|
||||
logger: logger,
|
||||
beginTask: beginTask,
|
||||
finishTask: finishTask,
|
||||
recordMessage: recordMessage,
|
||||
}
|
||||
}
|
||||
|
||||
// dispatch 接收一条解密消息并安排系统事件或聊天消息处理。
|
||||
func (d *messageDispatcher) dispatch(decrypted map[string]any) {
|
||||
// ctx、ok 用于本次流程后续判断的ctx、ok
|
||||
ctx, ok := d.beginTask()
|
||||
// ctx、finish、ok 分别是当前消息任务的上下文、释放函数与生命周期接纳结果。
|
||||
ctx, finish, ok := d.beginTask()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -125,11 +120,11 @@ func (d *messageDispatcher) dispatch(decrypted map[string]any) {
|
||||
select {
|
||||
case d.sem <- struct{}{}:
|
||||
case <-ctx.Done():
|
||||
d.finishTask()
|
||||
finish()
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
defer d.finishTask()
|
||||
defer finish()
|
||||
defer func() { <-d.sem }()
|
||||
d.handleMessageContext(ctx, decrypted)
|
||||
}()
|
||||
@@ -139,12 +134,12 @@ func (d *messageDispatcher) dispatch(decrypted map[string]any) {
|
||||
select {
|
||||
case d.sem <- struct{}{}:
|
||||
default:
|
||||
d.finishTask()
|
||||
finish()
|
||||
d.logger.Warn("消息处理并发达上限,丢弃消息", "limit", MessageSemaphoreSize)
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
defer d.finishTask()
|
||||
defer finish()
|
||||
defer func() { <-d.sem }()
|
||||
d.handleMessageContext(ctx, decrypted)
|
||||
}()
|
||||
@@ -152,7 +147,13 @@ func (d *messageDispatcher) dispatch(decrypted map[string]any) {
|
||||
|
||||
// handleMessage 分类并投递消息,供 Account facade 和测试调用。
|
||||
func (d *messageDispatcher) handleMessage(decrypted map[string]any) {
|
||||
d.handleMessageContext(context.Background(), decrypted)
|
||||
// ctx、finish、accepted 分别是同步分发任务的上下文、释放函数及生命周期接纳结果。
|
||||
ctx, finish, accepted := d.beginTask()
|
||||
if !accepted {
|
||||
return
|
||||
}
|
||||
defer finish()
|
||||
d.handleMessageContext(ctx, decrypted)
|
||||
}
|
||||
|
||||
// handleMessageContext 将系统事件和聊天消息分别交给对应业务链。
|
||||
@@ -285,12 +286,12 @@ func (d *messageDispatcher) scheduleDebouncedReply(chat ChatMessage) {
|
||||
// lastMessage 用于本次流程后续判断的last消息
|
||||
lastMessage := current.lastMsg
|
||||
d.debounceMu.Unlock()
|
||||
// ctx、ok 用于本次流程后续判断的ctx、ok
|
||||
ctx, ok := d.beginTask()
|
||||
// ctx、finish、ok 分别是防抖回复任务的上下文、释放函数及生命周期接纳结果。
|
||||
ctx, finish, ok := d.beginTask()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
defer d.finishTask()
|
||||
defer finish()
|
||||
if d.reply != nil {
|
||||
if // err 用于本次流程后续判断的err
|
||||
err := d.reply.Handle(ctx, lastMessage); err != nil {
|
||||
|
||||
@@ -59,7 +59,7 @@ func (c *pendingRenewalCoordinator) waitContext(ctx context.Context) bool {
|
||||
return true
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
return false
|
||||
}
|
||||
c.mu.Lock()
|
||||
// done 保存当前任务轮次的共享完成信号;nil 表示没有任何待收束的迟到续期任务。
|
||||
@@ -79,8 +79,7 @@ func (c *pendingRenewalCoordinator) waitContext(ctx context.Context) bool {
|
||||
// watch 登记并异步等待一条迟到续期响应,所有持久化工作继承账号生命周期 Context。
|
||||
func (c *pendingRenewalCoordinator) watch(
|
||||
parent context.Context,
|
||||
beginTask func() (context.Context, bool),
|
||||
finishTask func(),
|
||||
beginTask func() (context.Context, func(), bool),
|
||||
result *renew.Result,
|
||||
persist func(context.Context, *renew.Result) error,
|
||||
logger *slog.Logger,
|
||||
@@ -88,8 +87,8 @@ func (c *pendingRenewalCoordinator) watch(
|
||||
if c == nil || result == nil || !result.HasPending() {
|
||||
return
|
||||
}
|
||||
// taskCtx 是账号生命周期任务上下文,Stop 会先取消它再等待 Join。
|
||||
taskCtx, accepted := beginTask()
|
||||
// taskCtx、finish、accepted 分别是账号生命周期任务上下文、释放函数与接纳结果。
|
||||
taskCtx, finish, accepted := beginTask()
|
||||
if !accepted {
|
||||
return
|
||||
}
|
||||
@@ -102,7 +101,7 @@ func (c *pendingRenewalCoordinator) watch(
|
||||
c.beginTask()
|
||||
go func() {
|
||||
defer c.finishTask()
|
||||
defer finishTask()
|
||||
defer finish()
|
||||
// ctx、cancel 保存迟到响应等待的限时上下文及其取消函数。
|
||||
ctx, cancel := context.WithTimeout(taskCtx, 35*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -138,7 +138,7 @@ func (r *wsRecorder) waitContext(ctx context.Context) bool {
|
||||
return true
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
return false
|
||||
}
|
||||
// done 表示 recorder worker 已经退出,供当前等待调用选择上下文超时或正常完成。
|
||||
done := r.done
|
||||
|
||||
Reference in new issue
Block a user