File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11## ✒ 未来版本的新特性 (Features in future versions)
22
3+ ### v0.2.x
4+
5+ * [x] 增加轻量的协程池
6+ * [x] 支持轮训调度策略
7+ * [ ] 支持随机调度策略
8+ * [ ] 支持最少任务优先调度策略
9+ * [x] 支持设置 worker 任务队列大小
10+ * [x] 支持设置 panic 处理函数
11+ * [ ] 支持选择使用自旋锁还是互斥锁
12+ * [ ] 完善单元测试
13+
314### v0.1.0
415
516* [x] 增加支持退避策略的自旋锁
6- * [ ] 增加轻量协程库
717
818### v0.0.1
919
Original file line number Diff line number Diff line change 33all : test
44
55test :
6- go test -cover ./...
6+ go test -v - cover ./...
77
88bench :
99 go test -v ./_examples/performance_test.go -run=none -bench=. -benchmem -benchtime=1s
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ import (
1515
1616const (
1717 limit = 256
18- size = limit
18+ size = 256
1919 timeLoop = 100_0000
2020)
2121
@@ -64,8 +64,7 @@ func BenchmarkLimiterTime(b *testing.B) {
6464
6565// go test -v -run=none -bench=^BenchmarkPool$ -benchmem -benchtime=1s
6666func BenchmarkPool (b * testing.B ) {
67- workerLimit := 1024
68- pool := goes .NewPool (size , workerLimit )
67+ pool := goes .NewPool (size )
6968
7069 num := uint32 (0 )
7170 task := func () {
@@ -84,8 +83,7 @@ func BenchmarkPool(b *testing.B) {
8483
8584// go test -v -run=none -bench=^BenchmarkPoolTime$ -benchmem -benchtime=1s
8685func BenchmarkPoolTime (b * testing.B ) {
87- workerLimit := 1024
88- pool := goes .NewPool (size , workerLimit )
86+ pool := goes .NewPool (size )
8987
9088 num := uint32 (0 )
9189 task := func () {
Original file line number Diff line number Diff line change 1+ // Copyright 2025 FishGoddess. All rights reserved.
2+ // Use of this source code is governed by a MIT style
3+ // license that can be found in the LICENSE file.
4+
5+ package goes
6+
7+ type config struct {
8+ size int
9+ queueSize int
10+ recoverFunc func (r any )
11+ }
12+
13+ func newDefaultConfig (size int ) * config {
14+ return & config {
15+ size : size ,
16+ queueSize : 64 ,
17+ recoverFunc : nil ,
18+ }
19+ }
20+
21+ // Option is for setting config.
22+ type Option func (conf * config )
23+
24+ func (o Option ) applyTo (conf * config ) {
25+ o (conf )
26+ }
27+
28+ // WithQueueSize sets the queue size of worker.
29+ func WithQueueSize (size int ) Option {
30+ return func (conf * config ) {
31+ conf .queueSize = size
32+ }
33+ }
34+
35+ // WithRecoverFunc sets the recover function of worker.
36+ func WithRecoverFunc (f func (r any )) Option {
37+ return func (conf * config ) {
38+ conf .recoverFunc = f
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2025 FishGoddess. All rights reserved.
2+ // Use of this source code is governed by a MIT style
3+ // license that can be found in the LICENSE file.
4+
5+ package goes
6+
7+ import (
8+ "fmt"
9+ "testing"
10+ )
11+
12+ // go test -v -cover -run=^TestNewDefaultConfig$
13+ func TestNewDefaultConfig (t * testing.T ) {
14+ size := 16
15+ conf := newDefaultConfig (size )
16+
17+ if conf .size != size {
18+ t .Fatalf ("conf.size %d != size %d" , conf .size , size )
19+ }
20+ }
21+
22+ // go test -v -cover -run=^TestWithQueueSize$
23+ func TestWithQueueSize (t * testing.T ) {
24+ size := 16
25+ queueSize := 256
26+ conf := newDefaultConfig (size )
27+ WithQueueSize (queueSize )(conf )
28+
29+ if conf .queueSize != queueSize {
30+ t .Fatalf ("conf.queueSize %d != queueSize %d" , conf .queueSize , queueSize )
31+ }
32+ }
33+
34+ // go test -v -cover -run=^TestWithRecoverFunc$
35+ func TestWithRecoverFunc (t * testing.T ) {
36+ size := 16
37+ recoverFunc := func (r any ) {}
38+ conf := newDefaultConfig (size )
39+ WithRecoverFunc (recoverFunc )(conf )
40+
41+ if fmt .Sprintf ("%p" , conf .recoverFunc ) != fmt .Sprintf ("%p" , recoverFunc ) {
42+ t .Fatalf ("conf.recoverFunc %p != recoverFunc %p" , conf .recoverFunc , recoverFunc )
43+ }
44+ }
Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ import (
1111)
1212
1313type Pool struct {
14+ conf * config
15+
1416 workers []* worker
1517 index int
1618 closed bool
@@ -19,20 +21,30 @@ type Pool struct {
1921 lock sync.Locker
2022}
2123
22- func NewPool (size int , workerLimit int ) * Pool {
23- if size <= 0 {
24+ func NewPool (size int , opts ... Option ) * Pool {
25+ conf := newDefaultConfig (size )
26+ for _ , opt := range opts {
27+ opt .applyTo (conf )
28+ }
29+
30+ if conf .size <= 0 {
2431 panic ("goes: pool size <= 0" )
2532 }
2633
34+ if conf .queueSize <= 0 {
35+ panic ("goes: worker queue size <= 0" )
36+ }
37+
2738 pool := & Pool {
39+ conf : conf ,
2840 workers : make ([]* worker , 0 , size ),
2941 index : 0 ,
3042 closed : false ,
3143 lock : spinlock .New (),
3244 }
3345
3446 for range size {
35- worker := newWorker (pool , workerLimit )
47+ worker := newWorker (pool )
3648 pool .workers = append (pool .workers , worker )
3749 }
3850
Original file line number Diff line number Diff line change @@ -13,8 +13,8 @@ import (
1313// go test -v -cover -run=^TestPool$
1414func TestPool (t * testing.T ) {
1515 size := 16
16- workerLimit := 1024
17- pool := NewPool (size , workerLimit )
16+ queueSize := 1024
17+ pool := NewPool (size , WithQueueSize ( queueSize ) )
1818
1919 var countMap = make (map [int64 ]int , 16 )
2020 var lock sync.Mutex
Original file line number Diff line number Diff line change @@ -9,14 +9,10 @@ type worker struct {
99 tasks chan func ()
1010}
1111
12- func newWorker (pool * Pool , limit int ) * worker {
13- if limit <= 0 {
14- panic ("goes: worker limit <= 0" )
15- }
16-
12+ func newWorker (pool * Pool ) * worker {
1713 w := & worker {
1814 pool : pool ,
19- tasks : make (chan func (), limit ),
15+ tasks : make (chan func (), pool . conf . queueSize ),
2016 }
2117
2218 w .work ()
@@ -28,11 +24,13 @@ func (w *worker) handle(task func()) {
2824 return
2925 }
3026
31- defer func () {
32- if r := recover (); r != nil {
33- panic (r ) // TODO recover from panic
34- }
35- }()
27+ if w .pool .conf .recoverFunc != nil {
28+ defer func () {
29+ if r := recover (); r != nil {
30+ w .pool .conf .recoverFunc (r )
31+ }
32+ }()
33+ }
3634
3735 task ()
3836}
You can’t perform that action at this time.
0 commit comments