diff --git a/.travis.yml b/.travis.yml index 469e02e..a7f3c00 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: go go: - - "1.8.x" - "1.9.x" - "1.10.x" - "1.11.x" diff --git a/README.md b/README.md index db59cb9..eb9f1ab 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A goroutine pool for Go
- +

@@ -53,7 +53,7 @@ import ( "sync/atomic" "time" - "github.com/panjf2000/ants" + "github.com/panjf2000/ants/v2" ) var sum int32 @@ -114,7 +114,7 @@ import ( "io/ioutil" "net/http" - "github.com/panjf2000/ants" + "github.com/panjf2000/ants/v2" ) type Request struct { @@ -222,7 +222,7 @@ func WithPanicHandler(panicHandler func(interface{})) Option { } ``` -`ants.Options`contains all optional configurations of ants pool, which allow you to customize the goroutine pool by invoking option functions to set up each configuration in `NewPool`/`NewPoolWithFunc`method. +`ants.Options`contains all optional configurations of ants pool, which allows you to customize the goroutine pool by invoking option functions to set up each configuration in `NewPool`/`NewPoolWithFunc`method. ## Customize limited pool @@ -269,14 +269,6 @@ All tasks submitted to `ants` pool will not be guaranteed to be addressed in ord ## Benchmarks -``` -OS: macOS High Sierra -Processor: 2.7 GHz Intel Core i5 -Memory: 8 GB 1867 MHz DDR3 - -Go Version: 1.9 -``` -
In that benchmark-picture, the first and second benchmarks performed test cases with 1M tasks and the rest of benchmarks performed test cases with 10M tasks, both in unlimited goroutines and `ants` pool, and the capacity of this `ants` goroutine-pool was limited to 50K. diff --git a/README_ZH.md b/README_ZH.md index aaf3473..e2f056d 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -9,7 +9,7 @@ A goroutine pool for Go
- +

@@ -53,7 +53,7 @@ import ( "sync/atomic" "time" - "github.com/panjf2000/ants" + "github.com/panjf2000/ants/v2" ) var sum int32 @@ -114,7 +114,7 @@ import ( "io/ioutil" "net/http" - "github.com/panjf2000/ants" + "github.com/panjf2000/ants/v2" ) type Request struct { @@ -269,18 +269,6 @@ pool.Release() ## Benchmarks -系统参数: - -``` -OS: macOS High Sierra -Processor: 2.7 GHz Intel Core i5 -Memory: 8 GB 1867 MHz DDR3 - -Go Version: 1.9 -``` - - -
上图中的前两个 benchmark 测试结果是基于100w 任务量的条件,剩下的几个是基于 1000w 任务量的测试结果,`ants`的默认池容量是 5w。 diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index fa9c423..7076222 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -28,7 +28,7 @@ import ( "testing" "time" - "github.com/panjf2000/ants" + "github.com/panjf2000/ants/v2" ) const ( diff --git a/ants_test.go b/ants_test.go index b51df3c..854a20b 100644 --- a/ants_test.go +++ b/ants_test.go @@ -29,7 +29,7 @@ import ( "testing" "time" - "github.com/panjf2000/ants" + "github.com/panjf2000/ants/v2" ) const ( diff --git a/examples/main.go b/examples/main.go index f9c73c8..49aba1a 100644 --- a/examples/main.go +++ b/examples/main.go @@ -28,7 +28,7 @@ import ( "sync/atomic" "time" - "github.com/panjf2000/ants" + "github.com/panjf2000/ants/v2" ) var sum int32 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..201c358 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/panjf2000/ants/v2 + +go 1.12 diff --git a/pool.go b/pool.go index a6fbe11..03c1ae5 100644 --- a/pool.go +++ b/pool.go @@ -196,15 +196,11 @@ func (p *Pool) Cap() int { } // Tune changes the capacity of this pool. -func (p *Pool) Tune(size uint) { - if p.Cap() == int(size) { +func (p *Pool) Tune(size int) { + if p.Cap() == size { return } atomic.StoreInt32(&p.capacity, int32(size)) - diff := p.Running() - int(size) - for i := 0; i < diff; i++ { - p.retrieveWorker().task <- nil - } } // Release Closes this pool. @@ -292,12 +288,14 @@ func (p *Pool) retrieveWorker() *goWorker { // revertWorker puts a worker back into free pool, recycling the goroutines. func (p *Pool) revertWorker(worker *goWorker) bool { - if atomic.LoadInt32(&p.release) == CLOSED { + if atomic.LoadInt32(&p.release) == CLOSED || p.Running() > p.Cap() { + worker.task <- nil return false } worker.recycleTime = time.Now() p.lock.Lock() p.workers = append(p.workers, worker) + // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. p.cond.Signal() p.lock.Unlock() diff --git a/pool_func.go b/pool_func.go index 95fce62..7567db3 100644 --- a/pool_func.go +++ b/pool_func.go @@ -210,10 +210,6 @@ func (p *PoolWithFunc) Tune(size int) { return } atomic.StoreInt32(&p.capacity, int32(size)) - diff := p.Running() - size - for i := 0; i < diff; i++ { - p.retrieveWorker().args <- nil - } } // Release Closed this pool. @@ -301,12 +297,14 @@ func (p *PoolWithFunc) retrieveWorker() *goWorkerWithFunc { // revertWorker puts a worker back into free pool, recycling the goroutines. func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool { - if atomic.LoadInt32(&p.release) == CLOSED { + if atomic.LoadInt32(&p.release) == CLOSED || p.Running() > p.Cap() { + worker.args <- nil return false } worker.recycleTime = time.Now() p.lock.Lock() p.workers = append(p.workers, worker) + // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. p.cond.Signal() p.lock.Unlock()