letsfire / factory Goto Github PK
View Code? Open in Web Editor NEWGo语言的协程池 , 节省内存 , 减少GC压力
License: MIT License
Go语言的协程池 , 节省内存 , 减少GC压力
License: MIT License
func (m *Master) AdjustSize(newSize int) {
if int64(newSize) > m.maxNum {
newSize = int(m.maxNum)
}
m.Lock()
defer m.Unlock()
if diff := newSize - int(m.ingNum); diff > 0 {
for i := 0; i < diff; i++ {
m.workers[m.ingNum] = newWorker()
atomic.AddInt64(&m.ingNum, 1)
}
} else if diff < 0 {
atomic.StoreInt64(&m.ingNum, int64(newSize))
if cursor := atomic.LoadInt64(&m.cursor); cursor > int64(newSize) {
atomic.StoreInt64(&m.cursor, int64(newSize))
}
for _, w := range m.workers[newSize:] {
if w == nil {
break
}
w.shutdown()
}
m.workers = m.workers[0:newSize]
}
}
func (m *Master) Running() int64 {
return atomic.LoadInt64(&m.ingNum)
}
func (m *Master) Shutdown() {
m.AdjustSize(0) // 关闭所有worker
}
func (m *Master) getWorker() *worker {
atomic.CompareAndSwapInt64(&m.cursor, m.ingNum, 0)
idx := atomic.AddInt64(&m.cursor, 1)
w := m.workers[idx-1]
return w
}
当cursor与原ingNum相等时,你要进行缩容
这个时候,如果是先把ingNum改成了新的值,再
atomic.CompareAndSwapInt64(&m.cursor, m.ingNum, 0)
运行这行时,就不会修改成功,然后你对cursor,进行了累加,导致越界,或有可能拿到一个nil对象。
还有就是你的代码里,对workers初始化的时候,用的是maxNum
但后面修改的时候,又用了ingNum做为他的长度。
我原以为你是要一开始声明一个确定长度的数组。但后面又换了,如果按你后面的逻辑
你一开始应该是
workers: make([]*worker, initNum,maxNum)
才对。
package factory
import (
"fmt"
"runtime"
"sync"
"testing"
"time"
)
type hh struct {
mux *sync.Mutex
a int
}
//go test -v -test.run TestNewMaster
func TestNewMaster(t *testing.T) {
master := NewMaster(1000, 1000)
// 新建第一条工作流水线
var line1 = master.AddLine("demo.line.1", func(args interface{}) {
h := args.(hh)
h.mux.Lock()
h.a++
if h.a%10000 == 0 {
fmt.Println(h.a)
}
h.mux.Unlock()
time.Sleep(time.Millisecond*10)
})
// 根据业务场景将参数提交
mux := &sync.Mutex{}
h := hh{mux:mux}
for i := 0; i < 1000000; i++ {
h.a = i
line1.Submit(h)
}
// 协程池数量可动态调整
master.Running() // 正在运行的协程工人数量
//master.AdjustSize(100) // 指定数量进行扩容或缩容
master.Shutdown() // 等于 master.AdjustSize(0)
fmt.Println(runtime.NumGoroutine())
time.Sleep(time.Second*5)
fmt.Println(runtime.NumGoroutine())
}
/*
从上面代码执行结束后可以看到还有很多协程未退出,也就是说还有很多任务没有执行完,
也就无法判断什么时候所有任务都执行完了,如果我要拿到所有执行结果,你的协程池就没法用
*/
func newWorker() (w *worker) {
w = &worker{
params: make(chan interface{}),
}
go func(w *worker) {
for {
if w.process() {
break
}
atomic.StoreInt32(&w.isBusy, 0)
}
// 置为繁忙状态
atomic.StoreInt32(&w.isBusy, 1)
// 可能存在任务
select {
case params := <-w.params:
w.action(params)
default:
}
// 关闭任务通道
close(w.params)
}(w)
return
}
在退出的时候,你这边考虑了有可能有某个任务正在写入新的参数。但是在你的这段代码运行到select的时候,也有可能那边还没开始写入参数,导致你这边运行到了default然后就把通道关闭了,导致写入方出现异常。
line.go 31 陷入死循环,使用场景是协程池保存tsdb数据,当tsdb不可用时,报这个错误
runtime stack:
runtime.throw(0x16bbd89, 0xe)
/root/.gvm/gos/go1.13/src/runtime/panic.go:774 +0x72
runtime.newstack()
/root/.gvm/gos/go1.13/src/runtime/stack.go:1046 +0x6e9
runtime.morestack()
/root/.gvm/gos/go1.13/src/runtime/asm_amd64.s:449 +0x8f
goroutine 1 [running]:
runtime.heapBitsSetType(0xc0020564b0, 0x30, 0x30, 0x1583560)
/root/.gvm/gos/go1.13/src/runtime/mbitmap.go:938 +0xa4e fp=0xc02763a3a8 sp=0xc02763a3a0 pc=0x42428e
runtime.mallocgc(0x30, 0x1583560, 0xc02763a401, 0x262f7e0)
/root/.gvm/gos/go1.13/src/runtime/malloc.go:1052 +0x53e fp=0xc02763a448 sp=0xc02763a3a8 pc=0x41a00e
runtime.newobject(0x1583560, 0xc00040c0c0)
/root/.gvm/gos/go1.13/src/runtime/malloc.go:1151 +0x38 fp=0xc02763a478 sp=0xc02763a448 pc=0x41a658
github.com/letsfire/utils.newCall(...)
/go/pkg/mod/github.com/letsfire/[email protected]/guard.go:45
github.com/letsfire/utils.(*Guard).Run(0xc000158d80, 0x16b7ddf, 0xa, 0xc02763a510, 0x0, 0xc02763a500, 0x14adf00, 0xc02763a558)
/go/pkg/mod/github.com/letsfire/[email protected]/guard.go:27 +0xfe fp=0xc02763a4c8 sp=0xc02763a478 pc=0x1353e4e
github.com/letsfire/factory.(*Master).getWorker(0xc0001b4050, 0xc047639f90)
/go/pkg/mod/github.com/letsfire/[email protected]/master.go:81 +0xdf fp=0xc02763a530 sp=0xc02763a4c8 pc=0x1354e9f
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:20 +0x32 fp=0xc02763a580 sp=0xc02763a530 pc=0x13543f2
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a5d0 sp=0xc02763a580 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a620 sp=0xc02763a5d0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a670 sp=0xc02763a620 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a6c0 sp=0xc02763a670 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a710 sp=0xc02763a6c0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a760 sp=0xc02763a710 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a7b0 sp=0xc02763a760 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a800 sp=0xc02763a7b0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a850 sp=0xc02763a800 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a8a0 sp=0xc02763a850 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a8f0 sp=0xc02763a8a0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a940 sp=0xc02763a8f0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a990 sp=0xc02763a940 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763a9e0 sp=0xc02763a990 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763aa30 sp=0xc02763a9e0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763aa80 sp=0xc02763aa30 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763aad0 sp=0xc02763aa80 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763ab20 sp=0xc02763aad0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763ab70 sp=0xc02763ab20 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763abc0 sp=0xc02763ab70 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763ac10 sp=0xc02763abc0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763ac60 sp=0xc02763ac10 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763acb0 sp=0xc02763ac60 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763ad00 sp=0xc02763acb0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763ad50 sp=0xc02763ad00 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763ada0 sp=0xc02763ad50 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763adf0 sp=0xc02763ada0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763ae40 sp=0xc02763adf0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763ae90 sp=0xc02763ae40 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763aee0 sp=0xc02763ae90 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763af30 sp=0xc02763aee0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763af80 sp=0xc02763af30 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763afd0 sp=0xc02763af80 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b020 sp=0xc02763afd0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b070 sp=0xc02763b020 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b0c0 sp=0xc02763b070 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b110 sp=0xc02763b0c0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b160 sp=0xc02763b110 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b1b0 sp=0xc02763b160 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b200 sp=0xc02763b1b0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b250 sp=0xc02763b200 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b2a0 sp=0xc02763b250 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b2f0 sp=0xc02763b2a0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b340 sp=0xc02763b2f0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b390 sp=0xc02763b340 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b3e0 sp=0xc02763b390 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b430 sp=0xc02763b3e0 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
/go/pkg/mod/github.com/letsfire/[email protected]/line.go:31 +0x1ae fp=0xc02763b480 sp=0xc02763b430 pc=0x135456e
github.com/letsfire/factory.(*Line).Submit(0xc00022ad00, 0x145f5e0, 0xc000ee0aa0)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.