-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathexample_pool_test.go
More file actions
119 lines (102 loc) · 2.95 KB
/
example_pool_test.go
File metadata and controls
119 lines (102 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package goroutines_test
import (
"fmt"
"time"
"github.com/viney-shih/goroutines"
)
func ExampleNewPool_withFixedSize() {
// allocate a pool with maximum size 5, and initialize all goroutines at the beginning.
p := goroutines.NewPool(5)
// don't forget to release the resource in the end
defer p.Release()
}
func ExampleNewPool_withIncreasingSize() {
// allocate a pool with maximum size 5, and initialize 2 goroutines.
// if necessary, the number of goroutines increase to 5 and never go down.
p := goroutines.NewPool(5, goroutines.WithPreAllocWorkers(2))
// don't forget to release the resource in the end
defer p.Release()
}
func ExampleNewPool_withAutoScaledSize() {
// allocate a pool with maximum size 5, and initialize 2 goroutines.
// if necessary, the number of goroutines increase to 5.
// if not busy ( by checking the running status every 10 seconds ), the number goes to 2.
p := goroutines.NewPool(
5,
goroutines.WithPreAllocWorkers(2),
goroutines.WithWorkerAdjustPeriod(time.Duration(time.Second*10)),
)
// don't forget to release the resource in the end
defer p.Release()
}
func ExampleNewPool_withFixedSizeAndQueues() {
// allocate a pool with maximum size 5, and initialize all goroutines at the beginning.
// at the same time, prepare a queue for buffering the tasks before sending to goroutines.
p := goroutines.NewPool(5, goroutines.WithTaskQueueLength(2))
// don't forget to release the resource in the end
defer p.Release()
}
func ExamplePool_Schedule() {
taskN := 7
rets := make(chan int, taskN)
// allocate a pool with 5 goroutines to deal with those tasks
p := goroutines.NewPool(5)
// don't forget to release the pool in the end
defer p.Release()
// assign tasks to asynchronous goroutine pool
for i := 0; i < taskN; i++ {
idx := i
p.Schedule(func() {
// sleep and return the index
time.Sleep(20 * time.Millisecond)
rets <- idx
})
}
// wait until all tasks done
for i := 0; i < taskN; i++ {
fmt.Println("index:", <-rets)
}
// Unordered output:
// index: 3
// index: 1
// index: 2
// index: 4
// index: 5
// index: 6
// index: 0
}
func ExamplePool_ScheduleWithTimeout() {
totalN, taskN := 5, 5
pause := make(chan struct{})
rets := make(chan int, taskN)
// allocate a pool with 5 goroutines to deal with those 5 tasks
p := goroutines.NewPool(totalN)
// don't forget to release the pool in the end
defer p.Release()
// full the workers which are stopped with the `pause`
for i := 0; i < taskN; i++ {
idx := i
p.ScheduleWithTimeout(50*time.Millisecond, func() {
<-pause
rets <- idx
})
}
// no more chance to add any task in Pool, and return `ErrScheduleTimeout`
if err := p.ScheduleWithTimeout(50*time.Millisecond, func() {
<-pause
rets <- taskN
}); err != nil {
fmt.Println(err.Error())
}
close(pause)
for i := 0; i < taskN; i++ {
fmt.Println("index:", <-rets)
}
// Unordered output:
// schedule timeout
// index: 0
// index: 3
// index: 2
// index: 4
// index: 1
}