feat: 添加过载保护、背压和熔断机制
This commit is contained in:
+12
-6
@@ -19,6 +19,7 @@ const (
|
||||
StateCompleted TaskState = "COMPLETED"
|
||||
StateFailed TaskState = "FAILED"
|
||||
StateCancelled TaskState = "CANCELLED"
|
||||
StateTimedOut TaskState = "TIMED_OUT"
|
||||
)
|
||||
|
||||
// TaskPriority levels (P0 highest, P4 lowest).
|
||||
@@ -53,6 +54,10 @@ type Task struct {
|
||||
OutputTokens int
|
||||
NodeID string
|
||||
Degraded bool
|
||||
QueueMs int
|
||||
FirstTokenMs int
|
||||
InferenceMs int
|
||||
TotalMs int
|
||||
cancelCh chan struct{}
|
||||
cancelOnce sync.Once
|
||||
mu sync.RWMutex
|
||||
@@ -76,12 +81,13 @@ func NewTask(id, requestID, appID, tenantID, logicalModel string, priority TaskP
|
||||
|
||||
// AllowedTransitions defines valid state transitions.
|
||||
var allowedTransitions = map[TaskState][]TaskState{
|
||||
StateQueued: {StateRunning, StateFailed, StateCancelled},
|
||||
StateRunning: {StateStreaming, StateCompleted, StateFailed, StateCancelled},
|
||||
StateStreaming: {StateCompleted, StateFailed, StateCancelled},
|
||||
StateQueued: {StateRunning, StateFailed, StateCancelled, StateTimedOut},
|
||||
StateRunning: {StateStreaming, StateCompleted, StateFailed, StateCancelled, StateTimedOut},
|
||||
StateStreaming: {StateCompleted, StateFailed, StateCancelled, StateTimedOut},
|
||||
StateCompleted: {},
|
||||
StateFailed: {},
|
||||
StateCancelled: {},
|
||||
StateTimedOut: {},
|
||||
}
|
||||
|
||||
// Transition changes the task state if the transition is valid.
|
||||
@@ -112,7 +118,7 @@ func (t *Task) Transition(to TaskState) error {
|
||||
switch to {
|
||||
case StateRunning:
|
||||
t.StartedAt = &now
|
||||
case StateCompleted, StateFailed, StateCancelled:
|
||||
case StateCompleted, StateFailed, StateCancelled, StateTimedOut:
|
||||
t.CompletedAt = &now
|
||||
}
|
||||
|
||||
@@ -129,7 +135,7 @@ func (t *Task) Cancel(reason string) error {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
if t.State == StateCompleted || t.State == StateFailed || t.State == StateCancelled {
|
||||
if t.State == StateCompleted || t.State == StateFailed || t.State == StateCancelled || t.State == StateTimedOut {
|
||||
return errors.New("task already in terminal state")
|
||||
}
|
||||
|
||||
@@ -165,7 +171,7 @@ func (t *Task) GetState() TaskState {
|
||||
// IsTerminal returns true if the task is in a terminal state.
|
||||
func (t *Task) IsTerminal() bool {
|
||||
s := t.GetState()
|
||||
return s == StateCompleted || s == StateFailed || s == StateCancelled
|
||||
return s == StateCompleted || s == StateFailed || s == StateCancelled || s == StateTimedOut
|
||||
}
|
||||
|
||||
// StateMachineLogger logs state transitions.
|
||||
|
||||
Reference in New Issue
Block a user