-
Notifications
You must be signed in to change notification settings - Fork 51
/
thread_pool.go
50 lines (39 loc) · 733 Bytes
/
thread_pool.go
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
package main
import (
"strconv"
"github.com/reconquest/hierr-go"
)
type threadPool struct {
available chan struct{}
size int
}
func newThreadPool(size int) *threadPool {
available := make(chan struct{}, size)
for i := 0; i < size; i++ {
available <- struct{}{}
}
return &threadPool{
available,
size,
}
}
func (pool *threadPool) run(task func()) {
<-pool.available
defer func() {
pool.available <- struct{}{}
}()
task()
}
func parseThreadPoolSize(args map[string]interface{}) (int, error) {
var (
poolSizeRaw = args["--threads"].(string)
)
poolSize, err := strconv.Atoi(poolSizeRaw)
if err != nil {
return 0, hierr.Errorf(
err,
`can't parse threads count`,
)
}
return poolSize, nil
}