forked from mirror/ants
optimization for timed task to clear idle workers
This commit is contained in:
parent
8e369baa4c
commit
2e7d823141
|
@ -84,7 +84,7 @@ func main() {
|
|||
|
||||
// use the pool with a function
|
||||
// set 10 the size of goroutine pool and 1 second for expired duration
|
||||
p, _ := ants.NewPoolWithFunc(10, 1, func(i interface{}) error {
|
||||
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
|
||||
myFunc(i)
|
||||
wg.Done()
|
||||
return nil
|
||||
|
|
|
@ -85,7 +85,7 @@ func main() {
|
|||
|
||||
// use the pool with a function
|
||||
// set 10 the size of goroutine pool and 1 second for expired duration
|
||||
p, _ := ants.NewPoolWithFunc(10, 1, func(i interface{}) error {
|
||||
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
|
||||
myFunc(i)
|
||||
wg.Done()
|
||||
return nil
|
||||
|
|
2
ants.go
2
ants.go
|
@ -36,7 +36,7 @@ const (
|
|||
)
|
||||
|
||||
// Init a instance pool when importing ants
|
||||
var defaultPool, _ = NewPool(DefaultPoolSize, DefaultCleanIntervalTime)
|
||||
var defaultPool, _ = NewPool(DefaultPoolSize)
|
||||
|
||||
// Submit submit a task to pool
|
||||
func Submit(task f) error {
|
||||
|
|
|
@ -78,7 +78,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
|
|||
|
||||
func BenchmarkAntsPoolWithFunc(b *testing.B) {
|
||||
var wg sync.WaitGroup
|
||||
p, _ := ants.NewPoolWithFunc(50000, 1, func(i interface{}) error {
|
||||
p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error {
|
||||
demoPoolFunc(i)
|
||||
wg.Done()
|
||||
return nil
|
||||
|
@ -104,7 +104,7 @@ func BenchmarkGoroutine(b *testing.B) {
|
|||
}
|
||||
|
||||
func BenchmarkAntsPool(b *testing.B) {
|
||||
p, _ := ants.NewPoolWithFunc(50000, 1, demoPoolFunc)
|
||||
p, _ := ants.NewPoolWithFunc(50000, demoPoolFunc)
|
||||
defer p.Release()
|
||||
|
||||
b.ResetTimer()
|
||||
|
|
|
@ -67,7 +67,7 @@ func main() {
|
|||
|
||||
// use the pool with a function
|
||||
// set 10 the size of goroutine pool and 1 second for expired duration
|
||||
p, _ := ants.NewPoolWithFunc(10, 1, func(i interface{}) error {
|
||||
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
|
||||
myFunc(i)
|
||||
wg.Done()
|
||||
return nil
|
||||
|
|
11
pool.go
11
pool.go
|
@ -62,9 +62,9 @@ type Pool struct {
|
|||
}
|
||||
|
||||
func (p *Pool) monitorAndClear() {
|
||||
heartbeat := time.NewTicker(p.expiryDuration)
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(p.expiryDuration)
|
||||
for range heartbeat.C{
|
||||
currentTime := time.Now()
|
||||
p.lock.Lock()
|
||||
idleWorkers := p.workers
|
||||
|
@ -88,7 +88,12 @@ func (p *Pool) monitorAndClear() {
|
|||
}
|
||||
|
||||
// NewPool generates a instance of ants pool
|
||||
func NewPool(size, expiry int) (*Pool, error) {
|
||||
func NewPool(size int) (*Pool, error) {
|
||||
return NewTimingPool(size, DefaultCleanIntervalTime)
|
||||
}
|
||||
|
||||
// NewTimingPool generates a instance of ants pool with a custom timed task
|
||||
func NewTimingPool(size, expiry int) (*Pool, error) {
|
||||
if size <= 0 {
|
||||
return nil, ErrPoolSizeInvalid
|
||||
}
|
||||
|
|
|
@ -88,8 +88,13 @@ func (p *PoolWithFunc) monitorAndClear() {
|
|||
}()
|
||||
}
|
||||
|
||||
// NewPoolWithFunc generates a instance of ants pool with a specific function.
|
||||
func NewPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) {
|
||||
// NewTimingPoolWithFunc generates a instance of ants pool with a specific function and a custom timed task
|
||||
func NewPoolWithFunc(size int, f pf) (*PoolWithFunc, error) {
|
||||
return NewTimingPoolWithFunc(size, DefaultCleanIntervalTime, f)
|
||||
}
|
||||
|
||||
// NewPoolWithFunc generates a instance of ants pool with a specific function
|
||||
func NewTimingPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) {
|
||||
if size <= 0 {
|
||||
return nil, ErrPoolSizeInvalid
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue