收口调度器与通知工作器关闭边界
This commit is contained in:
1 parent
c1c065e023
commit
e609f7b39a
8 files changed
+129
-10
No files matched your search
@@ -55,11 +55,25 @@ func (s *Scheduler) Run(ctx context.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// Wait 负责Wait相关处理。
|
||||
// Wait 等待调度器完成,并兼容不需要超时的旧调用方。
|
||||
func (s *Scheduler) Wait() {
|
||||
_ = s.WaitContext(context.Background())
|
||||
}
|
||||
|
||||
// WaitContext 在 ctx 约束内等待调度器完成,避免关闭流程无限阻塞。
|
||||
func (s *Scheduler) WaitContext(ctx context.Context) error {
|
||||
if s != nil && s.done != nil {
|
||||
<-s.done
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
select {
|
||||
case <-s.done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// scan 负责scan相关处理。
|
||||
|
||||
@@ -2,6 +2,7 @@ package automation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -390,3 +391,21 @@ func TestAutomationSchedulerWaitsForShutdown(t *testing.T) {
|
||||
t.Fatal("自动化调度器关闭后没有退出")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationSchedulerWaitContextHonorsDeadline 验证自动化调度器等待受关闭上下文限制。
|
||||
func TestAutomationSchedulerWaitContextHonorsDeadline(t *testing.T) {
|
||||
// scheduler 保存尚未完成的调度器,以验证等待超时不会永久阻塞。
|
||||
scheduler := &Scheduler{done: make(chan struct{})}
|
||||
// ctx、cancel 保存短时关闭上下文及其释放函数。
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
|
||||
defer cancel()
|
||||
// err 表示尚未完成调度器在超时上下文下的等待结果。
|
||||
if err := scheduler.WaitContext(ctx); !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("WaitContext error=%v, want deadline exceeded", err)
|
||||
}
|
||||
close(scheduler.done)
|
||||
// err 表示已完成调度器的等待结果。
|
||||
if err := scheduler.WaitContext(context.Background()); err != nil {
|
||||
t.Fatalf("completed WaitContext error=%v", err)
|
||||
}
|
||||
}
|
||||
@@ -61,6 +61,7 @@ type Notifier struct {
|
||||
httpc *http.Client
|
||||
started atomic.Bool
|
||||
workers sync.WaitGroup
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// newOutboundHTTPClient 保存newOutboundHTTPClient,供当前处理流程使用
|
||||
@@ -84,6 +85,7 @@ func NewWithRepository(cookieID string, repository Repository, logger *slog.Logg
|
||||
repository: repository,
|
||||
logger: logger.With("account", cookieID, "subsys", "notify"),
|
||||
httpc: newOutboundHTTPClient(),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,12 +99,34 @@ func (n *Notifier) Start(ctx context.Context) {
|
||||
n.workers.Add(1)
|
||||
go func() {
|
||||
defer n.workers.Done()
|
||||
defer close(n.done)
|
||||
n.runOutbox(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
// Wait 等待 outbox worker 随生命周期 context 退出。
|
||||
func (n *Notifier) Wait() { n.workers.Wait() }
|
||||
// Wait 等待 outbox worker 随生命周期 context 退出,并兼容旧调用方。
|
||||
func (n *Notifier) Wait() {
|
||||
_ = n.WaitContext(context.Background())
|
||||
}
|
||||
|
||||
// WaitContext 在 ctx 约束内等待 outbox worker 退出。
|
||||
func (n *Notifier) WaitContext(ctx context.Context) error {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
if !n.started.Load() || n.done == nil {
|
||||
return nil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
select {
|
||||
case <-n.done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// NotifyDelivery 发送发货结果通知。
|
||||
// accountID 为 cookie_id。向该账号所有已启用渠道发送发货通知。
|
||||
|
||||
@@ -3,15 +3,36 @@ package notify
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// TestNotifierWaitContextHonorsDeadline 验证通知 worker 等待受关闭上下文限制。
|
||||
func TestNotifierWaitContextHonorsDeadline(t *testing.T) {
|
||||
// notifier 保存已启动但尚未完成的通知器,以验证等待超时不会永久阻塞。
|
||||
notifier := &Notifier{done: make(chan struct{})}
|
||||
notifier.started.Store(true)
|
||||
// ctx、cancel 保存短时关闭上下文及其释放函数。
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
|
||||
defer cancel()
|
||||
// err 表示尚未完成 worker 在超时上下文下的等待结果。
|
||||
if err := notifier.WaitContext(ctx); !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("WaitContext error=%v, want deadline exceeded", err)
|
||||
}
|
||||
close(notifier.done)
|
||||
// err 表示已完成 worker 的等待结果。
|
||||
if err := notifier.WaitContext(context.Background()); err != nil {
|
||||
t.Fatalf("completed WaitContext error=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// newNotifyStore 负责newNotifyStore相关处理。
|
||||
func newNotifyStore(t *testing.T) (*db.Store, func()) {
|
||||
t.Helper()
|
||||
|
||||
@@ -127,10 +127,23 @@ func (s *Scheduler) Run(ctx context.Context) {
|
||||
|
||||
// Wait 等待定时循环和迟到响应 watcher 完成。
|
||||
func (s *Scheduler) Wait() {
|
||||
_ = s.WaitContext(context.Background())
|
||||
}
|
||||
|
||||
// WaitContext 在 ctx 约束内等待定时循环和 watcher 完成。
|
||||
func (s *Scheduler) WaitContext(ctx context.Context) error {
|
||||
if s == nil || s.done == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
select {
|
||||
case <-s.done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
<-s.done
|
||||
}
|
||||
|
||||
// runAPICookieRenewFixed 负责运行API登录凭证RenewFixed相关处理。
|
||||
|
||||
@@ -331,6 +331,24 @@ func TestPendingAPIRenewStopsWithSchedulerContext(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRenewalSchedulerWaitContextHonorsDeadline 验证续期调度器等待受关闭上下文限制。
|
||||
func TestRenewalSchedulerWaitContextHonorsDeadline(t *testing.T) {
|
||||
// scheduler 保存尚未完成的调度器,以验证等待超时不会永久阻塞。
|
||||
scheduler := &Scheduler{done: make(chan struct{})}
|
||||
// ctx、cancel 保存短时关闭上下文及其释放函数。
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
|
||||
defer cancel()
|
||||
// err 表示尚未完成调度器在超时上下文下的等待结果。
|
||||
if err := scheduler.WaitContext(ctx); !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("WaitContext error=%v, want deadline exceeded", err)
|
||||
}
|
||||
close(scheduler.done)
|
||||
// err 表示已完成调度器的等待结果。
|
||||
if err := scheduler.WaitContext(context.Background()); err != nil {
|
||||
t.Fatalf("completed WaitContext error=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPendingAPIRenewRestartFailureIsFinalFailure 负责TestPendingAPIRenewRestartFailureIsFinalFailure相关处理。
|
||||
func TestPendingAPIRenewRestartFailureIsFinalFailure(t *testing.T) {
|
||||
// store、cleanup 保存store、cleanup,供当前处理流程使用
|
||||
|
||||
Reference in new issue
Block a user