forked from mirror/ants
Merge branch 'master' into develop
This commit is contained in:
commit
2709c121aa
|
@ -19,7 +19,7 @@ Package ants implements a fixed goroutine pool for managing and recycling a mass
|
||||||
|
|
||||||
- Automatically managing and recycling a massive number of goroutines.
|
- Automatically managing and recycling a massive number of goroutines.
|
||||||
- Periodically clearing overdue goroutines.
|
- Periodically clearing overdue goroutines.
|
||||||
- Friendly interfaces: submitting tasks, getting the number of running goroutines, readjusting capacity of pool dynamically, closing pool.
|
- Friendly interfaces: submitting tasks, getting the number of running goroutines, readjusting capacity of pool dynamically, closing pool.
|
||||||
- Efficient in memory usage and it even achieves higher performance than unlimited goroutines in golang.
|
- Efficient in memory usage and it even achieves higher performance than unlimited goroutines in golang.
|
||||||
|
|
||||||
|
|
||||||
|
@ -182,9 +182,9 @@ There was only the test of `ants` Pool because my computer was crash when it rea
|
||||||
|
|
||||||
**As you can see, `ants` can up to 2x~6x faster than goroutines without pool and the memory consumption is reduced by 10 to 20 times.**
|
**As you can see, `ants` can up to 2x~6x faster than goroutines without pool and the memory consumption is reduced by 10 to 20 times.**
|
||||||
|
|
||||||
[1]: https://travis-ci.com/panjf2000/ants.svg?branch=develop
|
[1]: https://travis-ci.com/panjf2000/ants.svg?branch=master
|
||||||
[2]: https://travis-ci.com/panjf2000/ants
|
[2]: https://travis-ci.com/panjf2000/ants
|
||||||
[3]: https://codecov.io/gh/panjf2000/ants/branch/develop/graph/badge.svg
|
[3]: https://codecov.io/gh/panjf2000/ants/branch/master/graph/badge.svg
|
||||||
[4]: https://codecov.io/gh/panjf2000/ants
|
[4]: https://codecov.io/gh/panjf2000/ants
|
||||||
[5]: https://goreportcard.com/badge/github.com/panjf2000/ants
|
[5]: https://goreportcard.com/badge/github.com/panjf2000/ants
|
||||||
[6]: https://goreportcard.com/report/github.com/panjf2000/ants
|
[6]: https://goreportcard.com/report/github.com/panjf2000/ants
|
||||||
|
|
|
@ -180,9 +180,9 @@ Go1.9
|
||||||
|
|
||||||
**从该demo测试吞吐性能对比可以看出,使用ants的吞吐性能相较于原生goroutine可以保持在2-6倍的性能压制,而内存消耗则可以达到10-20倍的节省优势。**
|
**从该demo测试吞吐性能对比可以看出,使用ants的吞吐性能相较于原生goroutine可以保持在2-6倍的性能压制,而内存消耗则可以达到10-20倍的节省优势。**
|
||||||
|
|
||||||
[1]: https://travis-ci.com/panjf2000/ants.svg?branch=develop
|
[1]: https://travis-ci.com/panjf2000/ants.svg?branch=master
|
||||||
[2]: https://travis-ci.com/panjf2000/ants
|
[2]: https://travis-ci.com/panjf2000/ants
|
||||||
[3]: https://codecov.io/gh/panjf2000/ants/branch/develop/graph/badge.svg
|
[3]: https://codecov.io/gh/panjf2000/ants/branch/master/graph/badge.svg
|
||||||
[4]: https://codecov.io/gh/panjf2000/ants
|
[4]: https://codecov.io/gh/panjf2000/ants
|
||||||
[5]: https://goreportcard.com/badge/github.com/panjf2000/ants
|
[5]: https://goreportcard.com/badge/github.com/panjf2000/ants
|
||||||
[6]: https://goreportcard.com/report/github.com/panjf2000/ants
|
[6]: https://goreportcard.com/report/github.com/panjf2000/ants
|
||||||
|
|
|
@ -26,9 +26,9 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/panjf2000/ants"
|
"github.com/panjf2000/ants"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var n = 100000
|
var n = 100000
|
||||||
|
|
17
pool.go
17
pool.go
|
@ -129,6 +129,16 @@ func (p *Pool) Running() int {
|
||||||
return int(atomic.LoadInt32(&p.running))
|
return int(atomic.LoadInt32(&p.running))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IncrRunning increases the number of the currently running goroutines
|
||||||
|
func (p *Pool) IncrRunning() {
|
||||||
|
atomic.AddInt32(&p.running, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecrRunning decreases the number of the currently running goroutines
|
||||||
|
func (p *Pool) DecrRunning() {
|
||||||
|
atomic.AddInt32(&p.running, -1)
|
||||||
|
}
|
||||||
|
|
||||||
// Free returns the available goroutines to work
|
// Free returns the available goroutines to work
|
||||||
func (p *Pool) Free() int {
|
func (p *Pool) Free() int {
|
||||||
return int(atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running))
|
return int(atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running))
|
||||||
|
@ -181,11 +191,7 @@ func (p *Pool) getWorker() *Worker {
|
||||||
idleWorkers := p.workers
|
idleWorkers := p.workers
|
||||||
n := len(idleWorkers) - 1
|
n := len(idleWorkers) - 1
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
if p.Running() >= p.Cap() {
|
waiting = p.Running() >= p.Cap()
|
||||||
waiting = true
|
|
||||||
} else {
|
|
||||||
atomic.AddInt32(&p.running, 1)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
<-p.freeSignal
|
<-p.freeSignal
|
||||||
w = idleWorkers[n]
|
w = idleWorkers[n]
|
||||||
|
@ -209,6 +215,7 @@ func (p *Pool) getWorker() *Worker {
|
||||||
task: make(chan f, 1),
|
task: make(chan f, 1),
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
|
p.IncrRunning()
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
17
pool_func.go
17
pool_func.go
|
@ -134,6 +134,16 @@ func (p *PoolWithFunc) Running() int {
|
||||||
return int(atomic.LoadInt32(&p.running))
|
return int(atomic.LoadInt32(&p.running))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IncrRunning increases the number of the currently running goroutines
|
||||||
|
func (p *PoolWithFunc) IncrRunning() {
|
||||||
|
atomic.AddInt32(&p.running, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecrRunning decreases the number of the currently running goroutines
|
||||||
|
func (p *PoolWithFunc) DecrRunning() {
|
||||||
|
atomic.AddInt32(&p.running, -1)
|
||||||
|
}
|
||||||
|
|
||||||
// Free returns the available goroutines to work
|
// Free returns the available goroutines to work
|
||||||
func (p *PoolWithFunc) Free() int {
|
func (p *PoolWithFunc) Free() int {
|
||||||
return int(atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running))
|
return int(atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running))
|
||||||
|
@ -186,11 +196,7 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
||||||
idleWorkers := p.workers
|
idleWorkers := p.workers
|
||||||
n := len(idleWorkers) - 1
|
n := len(idleWorkers) - 1
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
if p.Running() >= p.Cap() {
|
waiting = p.Running() >= p.Cap()
|
||||||
waiting = true
|
|
||||||
} else {
|
|
||||||
atomic.AddInt32(&p.running, 1)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
<-p.freeSignal
|
<-p.freeSignal
|
||||||
w = idleWorkers[n]
|
w = idleWorkers[n]
|
||||||
|
@ -214,6 +220,7 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
||||||
args: make(chan interface{}, 1),
|
args: make(chan interface{}, 1),
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
|
p.IncrRunning()
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,6 @@
|
||||||
package ants
|
package ants
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,7 +46,7 @@ func (w *Worker) run() {
|
||||||
go func() {
|
go func() {
|
||||||
for f := range w.task {
|
for f := range w.task {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
atomic.AddInt32(&w.pool.running, -1)
|
w.pool.DecrRunning()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
f()
|
f()
|
||||||
|
|
|
@ -23,7 +23,6 @@
|
||||||
package ants
|
package ants
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,7 +46,7 @@ func (w *WorkerWithFunc) run() {
|
||||||
go func() {
|
go func() {
|
||||||
for args := range w.args {
|
for args := range w.args {
|
||||||
if args == nil {
|
if args == nil {
|
||||||
atomic.AddInt32(&w.pool.running, -1)
|
w.pool.DecrRunning()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.pool.poolFunc(args)
|
w.pool.poolFunc(args)
|
||||||
|
|
Loading…
Reference in New Issue