Skip to content

Commit 619906c

Browse files
committed
调整代码
1 parent a3180d1 commit 619906c

16 files changed

Lines changed: 252 additions & 176 deletions

FUTURE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
### v0.2.x
44

5-
* [x] 增加轻量的异步执行器
5+
* [x] 增加异步任务执行器
66
* [x] 支持轮询调度策略
77
* [ ] 支持随机调度策略
88
* [ ] 支持任务数量优先调度策略
99
* [ ] 支持任务时间优先调度策略
1010
* [x] 支持设置 worker 任务队列大小
1111
* [x] 支持设置 panic 处理函数
1212
* [x] 支持自定义 sync.Locker 实现
13-
* [x] 完善单元测试,将覆盖率提高到 90% 以上
13+
* [ ] 完善单元测试,将覆盖率提高到 95% 以上
1414

1515
### v0.1.0
1616

HISTORY.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
## ✒ 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.2.0-alpha
4+
5+
> 此版本发布于 2025-06-25
6+
7+
* 增加异步任务执行器
8+
* 支持轮询调度策略
9+
* 支持设置 worker 任务队列大小
10+
* 支持设置 panic 处理函数
11+
* 支持自定义 sync.Locker 实现
12+
* 单元测试覆盖率提高到 94%
13+
314
### v0.1.0
415

516
> 此版本发布于 2025-06-21

README.en.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
[![Coverage](_icons/coverage.svg)](./_icons/coverage.svg)
66
![Test](https://github.com/FishGoddess/goes/actions/workflows/test.yml/badge.svg)
77

8-
**Goes** is a easy-to-use and lightweight lib for limiting goroutines.
8+
**Goes** is a easy-to-use and lightweight lib for executing async tasks.
99

1010
[阅读中文版的文档](./README.md)
1111

1212
### 🥇 Features
1313

14-
* Spin lock with backoff strategy.
15-
* Limiter only limits the number of simultaneous goroutines, not reuses goroutines.
14+
* Supports spin lock with backoff strategy.
15+
* Limits the number of simultaneous goroutines and not reuses them by Limiter.
16+
* Limits the number of simultaneous goroutines and reuses them by Executor.
1617

1718
_Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to know about more information._
1819

@@ -33,16 +34,28 @@ import (
3334
)
3435

3536
func main() {
37+
// Limits the number of simultaneous goroutines and not reuses them.
3638
limiter := goes.NewLimiter(4)
3739

38-
for i := 0; i < 100; i++ {
40+
for i := 0; i < 20; i++ {
3941
limiter.Go(func() {
40-
fmt.Println(time.Now())
41-
time.Sleep(100 * time.Millisecond)
42+
fmt.Println("limiter --> ", time.Now())
43+
time.Sleep(time.Second)
4244
})
4345
}
4446

4547
limiter.Wait()
48+
49+
// Limits the number of simultaneous goroutines and reuses them.
50+
executor := goes.NewExecutor(4)
51+
defer executor.Close()
52+
53+
for i := 0; i < 20; i++ {
54+
executor.Submit(func() {
55+
fmt.Println("executor --> ", time.Now())
56+
time.Sleep(time.Second)
57+
})
58+
}
4659
}
4760
```
4861

@@ -60,15 +73,15 @@ goarch: amd64
6073
cpu: AMD EPYC 7K62 48-Core Processor
6174

6275
BenchmarkLimiter-2 2417040 498.5 ns/op 24 B/op 1 allocs/op
63-
BenchmarkPool-2 23793781 49.9 ns/op 0 B/op 0 allocs/op
76+
BenchmarkExecutor-2 23793781 49.9 ns/op 0 B/op 0 allocs/op
6477
BenchmarkAntsPool-2 4295964 271.7 ns/op 0 B/op 0 allocs/op
6578

6679
BenchmarkLimiterTime-2: num is 1000000, cost is 300.936441ms
67-
BenchmarkPoolTime-2: num is 1000000, cost is 51.350509ms
80+
BenchmarkExecutorTime-2: num is 1000000, cost is 51.350509ms
6881
BenchmarkAntsPoolTime-2: num is 999744, cost is 346.972287ms
6982
```
7083

71-
> Obviously, goes.Pool is 5x faster than ants.Pool which has more features, so try goes if you prefer a lightweight and faster pool.
84+
> Obviously, goes.Executor is 5x faster than ants.Pool which has more features, so try goes if you prefer a lightweight and faster executor.
7285
7386
> Benchmarks: [_examples/performance_test.go](./_examples/performance_test.go).
7487

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
[![Coverage](_icons/coverage.svg)](./_icons/coverage.svg)
66
![Test](https://github.com/FishGoddess/goes/actions/workflows/test.yml/badge.svg)
77

8-
**Goes** 是一个简单易用且轻量的协程数限制库
8+
**Goes** 是一个简单易用且轻量的异步任务执行库
99

1010
[Read me in English](./README.en.md)
1111

1212
### 🥇 功能特性
1313

14-
* 支持退避策略的自旋锁。
15-
* Limiter 只限制同时执行的协程数,不复用协程。
14+
* 内置退避策略的自旋锁。
15+
* 支持只限制同时执行的协程数,但不复用协程,使用 Limiter。
16+
* 支持限制同时执行的协程数,且复用协程,使用 Executor。
1617

1718
_历史版本的特性请查看 [HISTORY.md](./HISTORY.md)。未来版本的新特性和计划请查看 [FUTURE.md](./FUTURE.md)_
1819

@@ -33,16 +34,28 @@ import (
3334
)
3435

3536
func main() {
37+
// Limits the number of simultaneous goroutines and not reuses them.
3638
limiter := goes.NewLimiter(4)
3739

38-
for i := 0; i < 100; i++ {
40+
for i := 0; i < 20; i++ {
3941
limiter.Go(func() {
40-
fmt.Println(time.Now())
41-
time.Sleep(100 * time.Millisecond)
42+
fmt.Println("limiter --> ", time.Now())
43+
time.Sleep(time.Second)
4244
})
4345
}
4446

4547
limiter.Wait()
48+
49+
// Limits the number of simultaneous goroutines and reuses them.
50+
executor := goes.NewExecutor(4)
51+
defer executor.Close()
52+
53+
for i := 0; i < 20; i++ {
54+
executor.Submit(func() {
55+
fmt.Println("executor --> ", time.Now())
56+
time.Sleep(time.Second)
57+
})
58+
}
4659
}
4760
```
4861

@@ -60,15 +73,15 @@ goarch: amd64
6073
cpu: AMD EPYC 7K62 48-Core Processor
6174

6275
BenchmarkLimiter-2 2417040 498.5 ns/op 24 B/op 1 allocs/op
63-
BenchmarkPool-2 23793781 49.9 ns/op 0 B/op 0 allocs/op
76+
BenchmarkExecutor-2 23793781 49.9 ns/op 0 B/op 0 allocs/op
6477
BenchmarkAntsPool-2 4295964 271.7 ns/op 0 B/op 0 allocs/op
6578

6679
BenchmarkLimiterTime-2: num is 1000000, cost is 300.936441ms
67-
BenchmarkPoolTime-2: num is 1000000, cost is 51.350509ms
80+
BenchmarkExecutorTime-2: num is 1000000, cost is 51.350509ms
6881
BenchmarkAntsPoolTime-2: num is 999744, cost is 346.972287ms
6982
```
7083

71-
> 很明显,goes.Pool 的性能比功能更丰富的 ants.Pool 要高出 5 倍左右,所以当你需要一个更轻量且性能更高的协程池时,可以尝试下 goes。
84+
> 很明显,goes.Executor 的性能比功能更丰富的 ants.Pool 要高出 5 倍左右,所以当你需要一个很轻量且高性能的异步任务执行器时,可以尝试下 goes。
7285
7386
> 测试文件:[_examples/performance_test.go](./_examples/performance_test.go)
7487

_examples/basic.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@ import (
1212
)
1313

1414
func main() {
15+
// Limits the number of simultaneous goroutines and not reuses them.
1516
limiter := goes.NewLimiter(4)
1617

17-
for i := 0; i < 100; i++ {
18+
for i := 0; i < 20; i++ {
1819
limiter.Go(func() {
19-
fmt.Println(time.Now())
20-
time.Sleep(100 * time.Millisecond)
20+
fmt.Println("limiter --> ", time.Now())
21+
time.Sleep(time.Second)
2122
})
2223
}
2324

2425
limiter.Wait()
26+
27+
// Limits the number of simultaneous goroutines and reuses them.
28+
executor := goes.NewExecutor(4)
29+
defer executor.Close()
30+
31+
for i := 0; i < 20; i++ {
32+
executor.Submit(func() {
33+
fmt.Println("executor --> ", time.Now())
34+
time.Sleep(time.Second)
35+
})
36+
}
2537
}

_examples/performance_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import (
1414
)
1515

1616
const (
17-
limit = 256
18-
size = 256
19-
timeLoop = 100_0000
17+
limit = 256
18+
workerNum = limit
19+
size = limit
20+
timeLoop = 100_0000
2021
)
2122

2223
func bench(num *uint32) {
@@ -62,9 +63,9 @@ func BenchmarkLimiterTime(b *testing.B) {
6263
b.Logf("num is %d, cost is %s", num, cost)
6364
}
6465

65-
// go test -v -run=none -bench=^BenchmarkPool$ -benchmem -benchtime=1s
66-
func BenchmarkPool(b *testing.B) {
67-
pool := goes.NewPool(size)
66+
// go test -v -run=none -bench=^BenchmarkExecutor$ -benchmem -benchtime=1s
67+
func BenchmarkExecutor(b *testing.B) {
68+
executor := goes.NewExecutor(workerNum)
6869

6970
num := uint32(0)
7071
task := func() {
@@ -73,17 +74,17 @@ func BenchmarkPool(b *testing.B) {
7374

7475
b.RunParallel(func(pb *testing.PB) {
7576
for pb.Next() {
76-
pool.Submit(task)
77+
executor.Submit(task)
7778
}
7879
})
7980

80-
pool.Close()
81+
executor.Close()
8182
b.Logf("num is %d", num)
8283
}
8384

84-
// go test -v -run=none -bench=^BenchmarkPoolTime$ -benchmem -benchtime=1s
85-
func BenchmarkPoolTime(b *testing.B) {
86-
pool := goes.NewPool(size)
85+
// go test -v -run=none -bench=^BenchmarkExecutorTime$ -benchmem -benchtime=1s
86+
func BenchmarkExecutorTime(b *testing.B) {
87+
executor := goes.NewExecutor(size)
8788

8889
num := uint32(0)
8990
task := func() {
@@ -92,10 +93,10 @@ func BenchmarkPoolTime(b *testing.B) {
9293

9394
beginTime := time.Now()
9495
for range timeLoop {
95-
pool.Submit(task)
96+
executor.Submit(task)
9697
}
9798

98-
pool.Close()
99+
executor.Close()
99100

100101
cost := time.Since(beginTime)
101102
b.Logf("num is %d, cost is %s", num, cost)

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

config.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import (
1111
)
1212

1313
type config struct {
14-
size int
15-
queueSize int
16-
recoverFunc func(r any)
17-
newLockerFunc func() sync.Locker
14+
workerNum int
15+
workerQueueSize int
16+
recoverFunc func(r any)
17+
newLockerFunc func() sync.Locker
1818
}
1919

20-
func newDefaultConfig(size int) *config {
20+
func newDefaultConfig(workerNum int) *config {
2121
return &config{
22-
size: size,
23-
queueSize: 64,
24-
recoverFunc: nil,
25-
newLockerFunc: nil,
22+
workerNum: workerNum,
23+
workerQueueSize: 64,
24+
recoverFunc: nil,
25+
newLockerFunc: nil,
2626
}
2727
}
2828

config_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ import (
1212

1313
// go test -v -cover -run=^TestNewDefaultConfig$
1414
func TestNewDefaultConfig(t *testing.T) {
15-
size := 16
16-
conf := newDefaultConfig(size)
15+
workerNum := 16
16+
conf := newDefaultConfig(workerNum)
1717

18-
if conf.size != size {
19-
t.Fatalf("conf.size %d != size %d", conf.size, size)
18+
if conf.workerNum != workerNum {
19+
t.Fatalf("conf.workerNum %d != workerNum %d", conf.workerNum, workerNum)
2020
}
2121
}
2222

2323
// go test -v -cover -run=^TestConfigRecover$
2424
func TestConfigRecover(t *testing.T) {
25-
size := 16
26-
conf := newDefaultConfig(size)
25+
workerNum := 16
26+
conf := newDefaultConfig(workerNum)
2727
conf.recover(0)
2828

2929
got := 0
@@ -41,8 +41,8 @@ func TestConfigRecover(t *testing.T) {
4141

4242
// go test -v -cover -run=^TestConfigNewLocker$
4343
func TestConfigNewLocker(t *testing.T) {
44-
size := 16
45-
conf := newDefaultConfig(size)
44+
workerNum := 16
45+
conf := newDefaultConfig(workerNum)
4646
conf.newLocker()
4747

4848
want := &sync.Mutex{}

0 commit comments

Comments
 (0)