forked from ethereum/go-ethereum
/
pool.go
60 lines (49 loc) · 1.17 KB
/
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
51
52
53
54
55
56
57
58
59
60
package gopool
import (
"runtime"
"time"
"github.com/panjf2000/ants/v2"
)
var (
// Init a instance pool when importing ants.
defaultPool, _ = ants.NewPool(ants.DefaultAntsPoolSize, ants.WithExpiryDuration(10*time.Second))
minNumberPerTask = 5
)
// Logger is used for logging formatted messages.
type Logger interface {
// Printf must have the same semantics as log.Printf.
Printf(format string, args ...interface{})
}
// Submit submits a task to pool.
func Submit(task func()) error {
return defaultPool.Submit(task)
}
// Running returns the number of the currently running goroutines.
func Running() int {
return defaultPool.Running()
}
// Cap returns the capacity of this default pool.
func Cap() int {
return defaultPool.Cap()
}
// Free returns the available goroutines to work.
func Free() int {
return defaultPool.Free()
}
// Release Closes the default pool.
func Release() {
defaultPool.Release()
}
// Reboot reboots the default pool.
func Reboot() {
defaultPool.Reboot()
}
func Threads(tasks int) int {
threads := tasks / minNumberPerTask
if threads > runtime.NumCPU() {
threads = runtime.NumCPU()
} else if threads == 0 {
threads = 1
}
return threads
}