优化concurrent,禁止重复打开

This commit is contained in:
boyce
2024-02-27 16:18:51 +08:00
parent bcbee6dd11
commit 4898116698

View File

@@ -5,6 +5,7 @@ import (
"runtime" "runtime"
"github.com/duanhf2012/origin/log" "github.com/duanhf2012/origin/log"
"sync/atomic"
) )
const defaultMaxTaskChannelNum = 1000000 const defaultMaxTaskChannelNum = 1000000
@@ -21,6 +22,7 @@ type Concurrent struct {
tasks chan task tasks chan task
cbChannel chan func(error) cbChannel chan func(error)
open int32
} }
/* /*
@@ -33,6 +35,10 @@ func (c *Concurrent) OpenConcurrentByNumCPU(cpuNumMul float32) {
} }
func (c *Concurrent) OpenConcurrent(minGoroutineNum int32, maxGoroutineNum int32, maxTaskChannelNum int) { func (c *Concurrent) OpenConcurrent(minGoroutineNum int32, maxGoroutineNum int32, maxTaskChannelNum int) {
if atomic.AddInt32(&c.open,1) > 1 {
panic("repeated calls to OpenConcurrent are not allowed!")
}
c.tasks = make(chan task, maxTaskChannelNum) c.tasks = make(chan task, maxTaskChannelNum)
c.cbChannel = make(chan func(error), maxTaskChannelNum) c.cbChannel = make(chan func(error), maxTaskChannelNum)