github.com/phillinzzz/newBsc@v1.1.6/common/gopool/pool.go (about)

     1  package gopool
     2  
     3  import (
     4  	"runtime"
     5  	"time"
     6  
     7  	"github.com/panjf2000/ants/v2"
     8  )
     9  
    10  var (
    11  	// Init a instance pool when importing ants.
    12  	defaultPool, _   = ants.NewPool(ants.DefaultAntsPoolSize, ants.WithExpiryDuration(10*time.Second))
    13  	minNumberPerTask = 5
    14  )
    15  
    16  // Logger is used for logging formatted messages.
    17  type Logger interface {
    18  	// Printf must have the same semantics as log.Printf.
    19  	Printf(format string, args ...interface{})
    20  }
    21  
    22  // Submit submits a task to pool.
    23  func Submit(task func()) error {
    24  	return defaultPool.Submit(task)
    25  }
    26  
    27  // Running returns the number of the currently running goroutines.
    28  func Running() int {
    29  	return defaultPool.Running()
    30  }
    31  
    32  // Cap returns the capacity of this default pool.
    33  func Cap() int {
    34  	return defaultPool.Cap()
    35  }
    36  
    37  // Free returns the available goroutines to work.
    38  func Free() int {
    39  	return defaultPool.Free()
    40  }
    41  
    42  // Release Closes the default pool.
    43  func Release() {
    44  	defaultPool.Release()
    45  }
    46  
    47  // Reboot reboots the default pool.
    48  func Reboot() {
    49  	defaultPool.Reboot()
    50  }
    51  
    52  func Threads(tasks int) int {
    53  	threads := tasks / minNumberPerTask
    54  	if threads > runtime.NumCPU() {
    55  		threads = runtime.NumCPU()
    56  	} else if threads == 0 {
    57  		threads = 1
    58  	}
    59  	return threads
    60  }