Files
butterfliu/queue.go
lzw-723 a8d976b97e
All checks were successful
Go CI / test-and-build (push) Successful in 43s
清理代码库
2026-04-06 13:17:57 +08:00

42 lines
717 B
Go

// Package main - Task queue for background processing.
// Currently unused but kept for future async task support (e.g., background scanning).
package main
import (
"context"
"log"
)
type Task func() error
type Queue struct {
ch chan Task
done chan struct{}
}
func NewQueue(buf int) *Queue { return &Queue{ch: make(chan Task, buf)} }
func (q *Queue) Push(t Task) { q.ch <- t }
func (q *Queue) Run(ctx context.Context) {
q.done = make(chan struct{})
go func() {
defer close(q.done)
for {
select {
case t := <-q.ch:
if err := t(); err != nil {
log.Printf("task failed: %v", err)
}
case <-ctx.Done():
return
}
}
}()
}
func (q *Queue) Stop() {
close(q.ch)
<-q.done
}