11 Star 37 Fork 7

magiclvzs/antnet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
func_go.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
magiclvzs 提交于 2020-07-25 15:39 . 优化
package antnet
import (
"sync/atomic"
)
func Go(fn func()) {
pc := Config.PoolSize + 1
select {
case poolChan <- fn:
return
default:
pc = atomic.AddInt32(&poolGoCount, 1)
if pc > Config.PoolSize {
atomic.AddInt32(&poolGoCount, -1)
}
}
waitAll.Add(1)
var debugStr string
id := atomic.AddUint32(&goid, 1)
c := atomic.AddInt32(&gocount, 1)
if DefLog.Level() <= LogLevelDebug {
debugStr = LogSimpleStack()
LogDebug("goroutine start id:%d count:%d from:%s", id, c, debugStr)
}
go func() {
Try(fn, nil)
for pc <= Config.PoolSize {
select {
case <-stopChanForGo:
pc = Config.PoolSize + 1
case nfn := <-poolChan:
Try(nfn, nil)
}
}
waitAll.Done()
c = atomic.AddInt32(&gocount, -1)
if DefLog.Level() <= LogLevelDebug {
LogDebug("goroutine end id:%d count:%d from:%s", id, c, debugStr)
}
}()
}
func Go2(fn func(cstop chan struct{})) {
Go(func() {
fn(stopChanForGo)
})
}
func GoArgs(fn func(...interface{}), args ...interface{}) {
Go(func() {
fn(args...)
})
}
func goForRedis(fn func()) {
waitAllForRedis.Add(1)
var debugStr string
id := atomic.AddUint32(&goid, 1)
c := atomic.AddInt32(&gocount, 1)
if DefLog.Level() <= LogLevelDebug {
debugStr = LogSimpleStack()
LogDebug("goroutine start id:%d count:%d from:%s", id, c, debugStr)
}
go func() {
Try(fn, nil)
waitAllForRedis.Done()
c = atomic.AddInt32(&gocount, -1)
if DefLog.Level() <= LogLevelDebug {
LogDebug("goroutine end id:%d count:%d from:%s", id, c, debugStr)
}
}()
}
func goForLog(fn func(cstop chan struct{})) bool {
if IsStop() {
return false
}
waitAllForLog.Add(1)
go func() {
fn(stopChanForLog)
waitAllForLog.Done()
}()
return true
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/magiclvzs/antnet.git
git@gitee.com:magiclvzs/antnet.git
magiclvzs
antnet
antnet
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385