taskPool loop

This commit is contained in:
ken 2025-04-06 07:18:00 +08:00
parent b5ca131619
commit 4d85ba7259

View File

@ -30,7 +30,7 @@ func InitTaskPool(ctx context.Context) {
for model, g := range llm.LLMGroups { for model, g := range llm.LLMGroups {
pool := newTaskPool(ctx, g) pool := newTaskPool(ctx, g)
TaskPools[model] = pool TaskPools[model] = pool
go pool.loop() pool.startloop()
} }
}) })
} }
@ -38,8 +38,8 @@ func InitTaskPool(ctx context.Context) {
func newTaskPool(ctx context.Context, g *llm.LLMGroup) *TaskPool { func newTaskPool(ctx context.Context, g *llm.LLMGroup) *TaskPool {
var queueCap, workers int var queueCap, workers int
if g.IsLocal() { if g.IsLocal() {
queueCap = 100*g.Cap() queueCap = 20*g.Cap()
workers = 100 workers = 20
} else { } else {
workers = 500 workers = 500
queueCap = 10_000*g.Cap() queueCap = 10_000*g.Cap()
@ -72,20 +72,12 @@ func (t *TaskPool) remove(client common.WsClient) {
} }
func (t *TaskPool) loop() { func (t *TaskPool) startloop() {
done := t.ctx.Done() done := t.ctx.Done()
tick := time.NewTicker(PendingTimeOut) tick := time.NewTicker(PendingTimeOut)
go func(){
var task *Task var task *Task
for { for {
select {
case <-done: return
case <-tick.C:
// 清除pending太久的task
t.queue.RemoveTimeout(PendingTimeOut)
log.Info("[TaskPool] stat", "queueLen", t.queue.Len())
default:
}
task = t.queue.WaitForNew(t.ctx) task = t.queue.WaitForNew(t.ctx)
select { select {
case <-done: return case <-done: return
@ -96,4 +88,21 @@ func (t *TaskPool) loop() {
} }
} }
}()
go func(){
for {
select {
case <-done: return
case <-tick.C:
t.stat()
default:
}
}
}()
}
func (t *TaskPool) stat() {
// 清除pending太久的task
t.queue.RemoveTimeout(PendingTimeOut)
log.Info("[TaskPool] stat", "queueLen", t.queue.Len())
} }