Skip to content

Commit 51f09b1

Browse files
committed
Add Wait method to goPool and update usage in README and tests
- Add a new Wait method to the goPool struct to allow waiting for all tasks to finish before releasing resources. - Update the README to reflect the new usage of the Wait method before calling the Release method. - Update all test cases to use the new Wait method before calling the Release method. - Update the go get command in the README to fetch a specific version of the library. Signed-off-by: Daniel Hu <[email protected]>
1 parent c219d43 commit 51f09b1

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ GoPool is a high-performance, feature-rich, and easy-to-use worker pool library
3131
To install GoPool, use `go get`:
3232

3333
```bash
34-
go get -u github.com/devchat-ai/gopool
34+
go get github.com/devchat-ai/gopool@v0.2.0
3535
```
3636

3737
## Usage
@@ -50,12 +50,14 @@ import (
5050

5151
func main() {
5252
pool := gopool.NewGoPool(100)
53+
defer pool.Release()
54+
5355
for i := 0; i < 1000; i++ {
5456
pool.AddTask(func() {
5557
time.Sleep(10 * time.Millisecond)
5658
})
5759
}
58-
pool.Release()
60+
pool.Wait()
5961
}
6062
```
6163

@@ -73,12 +75,14 @@ import (
7375

7476
func main() {
7577
pool := gopool.NewGoPool(100, gopool.WithLock(new(spinlock.SpinLock)))
78+
defer pool.Release()
79+
7680
for i := 0; i < 1000; i++ {
7781
pool.AddTask(func() {
7882
time.Sleep(10 * time.Millisecond)
7983
})
8084
}
81-
pool.Release()
85+
pool.Wait()
8286
}
8387
```
8488

@@ -99,12 +103,14 @@ import (
99103

100104
func main() {
101105
pool := gopool.NewGoPool(100, gopool.WithMinWorkers(50))
106+
defer pool.Release()
107+
102108
for i := 0; i < 1000; i++ {
103109
pool.AddTask(func() {
104110
time.Sleep(10 * time.Millisecond)
105111
})
106112
}
107-
pool.Release()
113+
pool.Wait()
108114
}
109115
```
110116

@@ -127,13 +133,15 @@ import (
127133

128134
func main() {
129135
pool := gopool.NewGoPool(100, gopool.WithTimeout(1*time.Second))
136+
defer pool.Release()
137+
130138
for i := 0; i < 1000; i++ {
131139
pool.AddTask(func() (interface{}, error) {
132140
time.Sleep(2 * time.Second)
133141
return nil, nil
134142
})
135143
}
136-
pool.Release()
144+
pool.Wait()
137145
}
138146
```
139147

@@ -159,12 +167,14 @@ func main() {
159167
pool := gopool.NewGoPool(100, gopool.WithErrorCallback(func(err error) {
160168
fmt.Println("Task error:", err)
161169
}))
170+
defer pool.Release()
171+
162172
for i := 0; i < 1000; i++ {
163173
pool.AddTask(func() (interface{}, error) {
164174
return nil, errors.New("task error")
165175
})
166176
}
167-
pool.Release()
177+
pool.Wait()
168178
}
169179
```
170180

@@ -189,12 +199,14 @@ func main() {
189199
pool := gopool.NewGoPool(100, gopool.WithResultCallback(func(result interface{}) {
190200
fmt.Println("Task result:", result)
191201
}))
202+
defer pool.Release()
203+
192204
for i := 0; i < 1000; i++ {
193205
pool.AddTask(func() (interface{}, error) {
194206
return "task result", nil
195207
})
196208
}
197-
pool.Release()
209+
pool.Wait()
198210
}
199211
```
200212

gopool.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func (p *goPool) AddTask(t task) {
7070
p.taskQueue <- t
7171
}
7272

73+
// Wait waits for all tasks to be dispatched.
74+
func (p *goPool) Wait() {
75+
for len(p.taskQueue) > 0 {
76+
time.Sleep(100 * time.Millisecond)
77+
}
78+
}
79+
7380
// Release stops all workers and releases resources.
7481
func (p *goPool) Release() {
7582
close(p.taskQueue)

gopool_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,33 @@ import (
1111

1212
func TestGoPoolWithMutex(t *testing.T) {
1313
pool := NewGoPool(100, WithLock(new(sync.Mutex)))
14+
defer pool.Release()
1415
for i := 0; i < 1000; i++ {
1516
pool.AddTask(func() (interface{}, error) {
1617
time.Sleep(10 * time.Millisecond)
1718
return nil, nil
1819
})
1920
}
20-
pool.Release()
21+
pool.Wait()
2122
}
2223

2324
func TestGoPoolWithSpinLock(t *testing.T) {
2425
pool := NewGoPool(100, WithLock(new(spinlock.SpinLock)))
26+
defer pool.Release()
2527
for i := 0; i < 1000; i++ {
2628
pool.AddTask(func() (interface{}, error) {
2729
time.Sleep(10 * time.Millisecond)
2830
return nil, nil
2931
})
3032
}
31-
pool.Release()
33+
pool.Wait()
3234
}
3335

3436
func BenchmarkGoPoolWithMutex(b *testing.B) {
3537
var wg sync.WaitGroup
3638
var taskNum = int(1e6)
3739
pool := NewGoPool(1e4, WithLock(new(sync.Mutex)))
40+
defer pool.Release()
3841

3942
b.ResetTimer()
4043
for i := 0; i < b.N; i++ {
@@ -46,16 +49,16 @@ func BenchmarkGoPoolWithMutex(b *testing.B) {
4649
return nil, nil
4750
})
4851
}
52+
wg.Wait()
4953
}
50-
wg.Wait()
5154
b.StopTimer()
52-
pool.Release()
5355
}
5456

5557
func BenchmarkGoPoolWithSpinLock(b *testing.B) {
5658
var wg sync.WaitGroup
5759
var taskNum = int(1e6)
5860
pool := NewGoPool(1e4, WithLock(new(spinlock.SpinLock)))
61+
defer pool.Release()
5962

6063
b.ResetTimer()
6164
for i := 0; i < b.N; i++ {
@@ -67,10 +70,9 @@ func BenchmarkGoPoolWithSpinLock(b *testing.B) {
6770
return nil, nil
6871
})
6972
}
73+
wg.Wait()
7074
}
71-
wg.Wait()
7275
b.StopTimer()
73-
pool.Release()
7476
}
7577

7678
func BenchmarkGoroutines(b *testing.B) {
@@ -86,6 +88,7 @@ func BenchmarkGoroutines(b *testing.B) {
8688
return nil, nil
8789
}()
8890
}
91+
wg.Wait()
8992
}
9093
}
9194

@@ -96,12 +99,14 @@ func TestGoPoolWithError(t *testing.T) {
9699
t.Errorf("Expected error %v, but got %v", errTaskError, err)
97100
}
98101
}))
102+
defer pool.Release()
103+
99104
for i := 0; i< 1000; i++ {
100105
pool.AddTask(func() (interface{}, error) {
101106
return nil, errTaskError
102107
})
103108
}
104-
pool.Release()
109+
pool.Wait()
105110
}
106111

107112
func TestGoPoolWithResult(t *testing.T) {
@@ -111,10 +116,12 @@ func TestGoPoolWithResult(t *testing.T) {
111116
t.Errorf("Expected result %v, but got %v", expectedResult, result)
112117
}
113118
}))
119+
defer pool.Release()
120+
114121
for i := 0; i< 1000; i++ {
115122
pool.AddTask(func() (interface{}, error) {
116123
return expectedResult, nil
117124
})
118125
}
119-
pool.Release()
126+
pool.Wait()
120127
}

0 commit comments

Comments
 (0)