mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-20 04:50:26 +08:00
刷新/预热缓存任务可以并行处理
This commit is contained in:
52
internal/goman/task_group.go
Normal file
52
internal/goman/task_group.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package goman
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type TaskGroup struct {
|
||||
semi chan zero.Zero
|
||||
wg *sync.WaitGroup
|
||||
locker *sync.RWMutex
|
||||
}
|
||||
|
||||
func NewTaskGroup() *TaskGroup {
|
||||
var concurrent = runtime.NumCPU()
|
||||
if concurrent <= 1 {
|
||||
concurrent = 2
|
||||
}
|
||||
return &TaskGroup{
|
||||
semi: make(chan zero.Zero, concurrent),
|
||||
wg: &sync.WaitGroup{},
|
||||
locker: &sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
func (this *TaskGroup) Run(f func()) {
|
||||
this.wg.Add(1)
|
||||
go func() {
|
||||
defer this.wg.Done()
|
||||
|
||||
this.semi <- zero.Zero{}
|
||||
|
||||
f()
|
||||
|
||||
<-this.semi
|
||||
}()
|
||||
}
|
||||
|
||||
func (this *TaskGroup) Wait() {
|
||||
this.wg.Wait()
|
||||
}
|
||||
|
||||
func (this *TaskGroup) Lock() {
|
||||
this.locker.Lock()
|
||||
}
|
||||
|
||||
func (this *TaskGroup) Unlock() {
|
||||
this.locker.Unlock()
|
||||
}
|
||||
Reference in New Issue
Block a user