forked from mirror/ants
Fixed #45 and Releasing v2 module
This commit is contained in:
parent
2c15b60572
commit
cbf8ddf048
|
@ -1,7 +1,6 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- "1.8.x"
|
||||
- "1.9.x"
|
||||
- "1.10.x"
|
||||
- "1.11.x"
|
||||
|
|
16
README.md
16
README.md
|
@ -9,7 +9,7 @@ A goroutine pool for Go
|
|||
<a title="Ants on Sourcegraph" target="_blank" href="https://sourcegraph.com/github.com/panjf2000/ants?badge"><img src="https://sourcegraph.com/github.com/panjf2000/ants/-/badge.svg?style=flat-square"></a>
|
||||
<br/>
|
||||
<a title="" target="_blank" href="https://golangci.com/r/github.com/panjf2000/ants"><img src="https://golangci.com/badges/github.com/panjf2000/ants.svg"></a>
|
||||
<a title="Godoc for ants" target="_blank" href="https://godoc.org/github.com/panjf2000/ants"><img src="https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square"></a>
|
||||
<a title="Godoc for ants" target="_blank" href="https://godoc.org/github.com/panjf2000/ants"><img src="https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square"></a>
|
||||
<a title="Release" target="_blank" href="https://github.com/panjf2000/ants/releases"><img src="https://img.shields.io/github/release/panjf2000/ants.svg?style=flat-square"></a>
|
||||
<a title="License" target="_blank" href="https://opensource.org/licenses/mit-license.php"><img src="https://img.shields.io/aur/license/pac?style=flat-square"></a>
|
||||
</p>
|
||||
|
@ -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
|
||||
```
|
||||
|
||||
<div align="center"><img src="https://user-images.githubusercontent.com/7496278/51515466-c7ce9e00-1e4e-11e9-89c4-bd3785b3c667.png"/></div>
|
||||
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.
|
||||
|
||||
|
|
18
README_ZH.md
18
README_ZH.md
|
@ -9,7 +9,7 @@ A goroutine pool for Go
|
|||
<a title="Ants on Sourcegraph" target="_blank" href="https://sourcegraph.com/github.com/panjf2000/ants?badge"><img src="https://sourcegraph.com/github.com/panjf2000/ants/-/badge.svg?style=flat-square"></a>
|
||||
<br/>
|
||||
<a title="" target="_blank" href="https://golangci.com/r/github.com/panjf2000/ants"><img src="https://golangci.com/badges/github.com/panjf2000/ants.svg"></a>
|
||||
<a title="Godoc for ants" target="_blank" href="https://godoc.org/github.com/panjf2000/ants"><img src="https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square"></a>
|
||||
<a title="Godoc for ants" target="_blank" href="https://godoc.org/github.com/panjf2000/ants"><img src="https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square"></a>
|
||||
<a title="Release" target="_blank" href="https://github.com/panjf2000/ants/releases"><img src="https://img.shields.io/github/release/panjf2000/ants.svg?style=flat-square"></a>
|
||||
<a title="License" target="_blank" href="https://opensource.org/licenses/mit-license.php"><img src="https://img.shields.io/aur/license/pac?style=flat-square"></a>
|
||||
</p>
|
||||
|
@ -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
|
||||
```
|
||||
|
||||
|
||||
|
||||
<div align="center"><img src="https://user-images.githubusercontent.com/7496278/51515466-c7ce9e00-1e4e-11e9-89c4-bd3785b3c667.png"/></div>
|
||||
上图中的前两个 benchmark 测试结果是基于100w 任务量的条件,剩下的几个是基于 1000w 任务量的测试结果,`ants`的默认池容量是 5w。
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/panjf2000/ants"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
|
@ -29,7 +29,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/panjf2000/ants"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
|
@ -28,7 +28,7 @@ import (
|
|||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/panjf2000/ants"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
)
|
||||
|
||||
var sum int32
|
||||
|
|
12
pool.go
12
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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue