feat: add automatic Worker runtime with lease recovery and bounded shutdown

This commit is contained in:
2026-09-17 14:29:28 +08:00
parent 1753fcbccb
commit da123bba69
22 changed files with 1784 additions and 17 deletions

View File

@@ -49,17 +49,24 @@ func integrationWorkerClient(t *testing.T) (*Client, string, string) {
// 只丢响应,不伪造任务状态,测试凭据不进入 URL 或输出。
type workerFaultGateway struct {
drop atomic.Value
offline atomic.Bool
mu sync.Mutex
active map[*websocket.Conn]struct{}
dropped chan string
server *httptest.Server
}
func newWorkerFaultGateway(t *testing.T, upstream string) *workerFaultGateway {
t.Helper()
g := &workerFaultGateway{dropped: make(chan string, 4)}
g := &workerFaultGateway{dropped: make(chan string, 4), active: make(map[*websocket.Conn]struct{})}
g.drop.Store("")
ctx, cancel := context.WithCancel(context.Background())
var handlers sync.WaitGroup
g.server = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if g.offline.Load() {
http.Error(w, "gateway offline", 503)
return
}
handlers.Add(1)
defer handlers.Done()
bridge, stop := context.WithCancel(ctx)
@@ -78,6 +85,13 @@ func newWorkerFaultGateway(t *testing.T, upstream string) *workerFaultGateway {
return
}
defer down.CloseNow()
g.mu.Lock()
g.active[down] = struct{}{}
g.mu.Unlock()
defer func() { g.mu.Lock(); delete(g.active, down); g.mu.Unlock() }()
if g.offline.Load() {
return
}
down.SetReadLimit(512 << 10)
var mu sync.Mutex
var forwarding sync.WaitGroup
@@ -141,6 +155,17 @@ func newWorkerFaultGateway(t *testing.T, upstream string) *workerFaultGateway {
t.Cleanup(func() { cancel(); g.server.Close(); handlers.Wait() })
return g
}
func (g *workerFaultGateway) disconnectActive() {
g.mu.Lock()
active := make([]*websocket.Conn, 0, len(g.active))
for c := range g.active {
active = append(active, c)
}
g.mu.Unlock()
for _, c := range active {
c.CloseNow()
}
}
func (g *workerFaultGateway) expectDrop(t *testing.T, operation string) {
t.Helper()
select {