feat: 添加过载保护、背压和熔断机制
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled
CI / security-scan (push) Has been cancelled

This commit is contained in:
freedakgmail
2026-08-03 08:12:28 +08:00
parent 5bcfbdb88d
commit e7e98271d4
8 changed files with 704 additions and 36 deletions
+25 -9
View File
@@ -14,15 +14,15 @@ import (
// Scheduler manages task queuing and execution with priority-based scheduling.
type Scheduler struct {
mu sync.Mutex
queue *priorityQueue
running map[string]*task.Task
maxRunning int
maxQueued int
notifyCh chan struct{}
logger *observability.Logger
ctx context.Context
cancel context.CancelFunc
mu sync.Mutex
queue *priorityQueue
running map[string]*task.Task
maxRunning int
maxQueued int
notifyCh chan struct{}
logger *observability.Logger
ctx context.Context
cancel context.CancelFunc
}
// NewScheduler creates a new scheduler.
@@ -114,6 +114,22 @@ func (s *Scheduler) RunningCount() int {
return len(s.running)
}
// GetTask returns a running task by ID, or a queued task by ID.
func (s *Scheduler) GetTask(taskID string) (*task.Task, bool) {
s.mu.Lock()
defer s.mu.Unlock()
if t, ok := s.running[taskID]; ok {
return t, true
}
for i := 0; i < s.queue.Len(); i++ {
t := (*s.queue)[i]
if t.ID == taskID {
return t, true
}
}
return nil, false
}
// Stop shuts down the scheduler.
func (s *Scheduler) Stop() {
s.cancel()