初始化项目
This commit is contained in:
39
queue.go
Normal file
39
queue.go
Normal file
@@ -0,0 +1,39 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user