2021-07-29 17:16:53 +08:00
|
|
|
package gopool
|
|
|
|
|
|
|
|
import (
|
2021-09-28 16:03:38 +08:00
|
|
|
"runtime"
|
2021-07-29 17:16:53 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Init a instance pool when importing ants.
|
2021-09-28 16:03:38 +08:00
|
|
|
defaultPool, _ = ants.NewPool(ants.DefaultAntsPoolSize, ants.WithExpiryDuration(10*time.Second))
|
|
|
|
minNumberPerTask = 5
|
2021-07-29 17:16:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
2021-09-28 16:03:38 +08:00
|
|
|
|
|
|
|
func Threads(tasks int) int {
|
|
|
|
threads := tasks / minNumberPerTask
|
|
|
|
if threads > runtime.NumCPU() {
|
|
|
|
threads = runtime.NumCPU()
|
|
|
|
} else if threads == 0 {
|
|
|
|
threads = 1
|
|
|
|
}
|
|
|
|
return threads
|
|
|
|
}
|