mirror of https://github.com/panjf2000/ants.git
update
This commit is contained in:
parent
08e6a26217
commit
ff4b7d8a22
|
@ -72,7 +72,6 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
//b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAntsPoolWithFunc(b *testing.B) {
|
func BenchmarkAntsPoolWithFunc(b *testing.B) {
|
||||||
|
@ -88,9 +87,8 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) {
|
||||||
p.Serve(loop)
|
p.Serve(loop)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
//b.Logf("running goroutines: %d", p.Running())
|
b.Logf("running goroutines: %d", p.Running())
|
||||||
}
|
}
|
||||||
//b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkGoroutine(b *testing.B) {
|
func BenchmarkGoroutine(b *testing.B) {
|
||||||
|
@ -100,15 +98,14 @@ func BenchmarkGoroutine(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAntsPool(b *testing.B) {
|
func BenchmarkAntsPool(b *testing.B) {
|
||||||
|
b.N = 3
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
for j := 0; j < RunTimes; j++ {
|
for j := 0; j < RunTimes; j++ {
|
||||||
ants.Push(demoFunc)
|
ants.Push(demoFunc)
|
||||||
}
|
}
|
||||||
b.Logf("running goroutines: %d", ants.Running())
|
b.Logf("running goroutines: %d", ants.Running())
|
||||||
}
|
}
|
||||||
//b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,5 +69,6 @@ func main() {
|
||||||
p.Serve(str)
|
p.Serve(str)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
fmt.Printf("running goroutines: %d\n", p.Running())
|
||||||
fmt.Println("finish all tasks!")
|
fmt.Println("finish all tasks!")
|
||||||
}
|
}
|
||||||
|
|
40
pool_func.go
40
pool_func.go
|
@ -26,7 +26,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type pf func(interface{}) error
|
type pf func(interface{}) error
|
||||||
|
@ -55,10 +54,10 @@ type PoolWithFunc struct {
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
|
||||||
// closed is used to confirm whether this pool has been closed.
|
|
||||||
closed int32
|
|
||||||
|
|
||||||
poolFunc pf
|
poolFunc pf
|
||||||
|
|
||||||
|
once sync.Once
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPoolWithFunc generates a instance of ants pool with a specific function.
|
// NewPoolWithFunc generates a instance of ants pool with a specific function.
|
||||||
|
@ -69,8 +68,7 @@ func NewPoolWithFunc(size int, f pf) (*PoolWithFunc, error) {
|
||||||
p := &PoolWithFunc{
|
p := &PoolWithFunc{
|
||||||
capacity: int32(size),
|
capacity: int32(size),
|
||||||
freeSignal: make(chan sig, math.MaxInt32),
|
freeSignal: make(chan sig, math.MaxInt32),
|
||||||
release: make(chan sig),
|
release: make(chan sig, 1),
|
||||||
closed: 0,
|
|
||||||
poolFunc: f,
|
poolFunc: f,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,27 +77,12 @@ func NewPoolWithFunc(size int, f pf) (*PoolWithFunc, error) {
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
// scanAndClean is a goroutine who will periodically clean up
|
|
||||||
// after it is noticed that this pool is closed.
|
|
||||||
func (p *PoolWithFunc) scanAndClean() {
|
|
||||||
ticker := time.NewTicker(DefaultCleanIntervalTime * time.Second)
|
|
||||||
go func() {
|
|
||||||
ticker.Stop()
|
|
||||||
for range ticker.C {
|
|
||||||
if atomic.LoadInt32(&p.closed) == 1 {
|
|
||||||
p.lock.Lock()
|
|
||||||
for _, w := range p.workers {
|
|
||||||
w.stop()
|
|
||||||
}
|
|
||||||
p.lock.Unlock()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serve submit a task to pool
|
// Serve submit a task to pool
|
||||||
func (p *PoolWithFunc) Serve(args interface{}) error {
|
func (p *PoolWithFunc) Serve(args interface{}) error {
|
||||||
if atomic.LoadInt32(&p.closed) == 1 {
|
//if atomic.LoadInt32(&p.closed) == 1 {
|
||||||
|
// return ErrPoolClosed
|
||||||
|
//}
|
||||||
|
if len(p.release) > 0 {
|
||||||
return ErrPoolClosed
|
return ErrPoolClosed
|
||||||
}
|
}
|
||||||
w := p.getWorker()
|
w := p.getWorker()
|
||||||
|
@ -124,10 +107,9 @@ func (p *PoolWithFunc) Cap() int {
|
||||||
|
|
||||||
// Release Closed this pool
|
// Release Closed this pool
|
||||||
func (p *PoolWithFunc) Release() error {
|
func (p *PoolWithFunc) Release() error {
|
||||||
p.lock.Lock()
|
p.once.Do(func() {
|
||||||
atomic.StoreInt32(&p.closed, 1)
|
p.release<- sig{}
|
||||||
close(p.release)
|
})
|
||||||
p.lock.Unlock()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ func (w *WorkerWithFunc) run() {
|
||||||
atomic.AddInt32(&w.pool.running, 1)
|
atomic.AddInt32(&w.pool.running, 1)
|
||||||
go func() {
|
go func() {
|
||||||
for args := range w.args {
|
for args := range w.args {
|
||||||
if args == nil {
|
if args == nil || len(w.pool.release) > 0 {
|
||||||
atomic.AddInt32(&w.pool.running, -1)
|
atomic.AddInt32(&w.pool.running, -1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,26 @@ func (w *WorkerWithFunc) run() {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//func (w *WorkerWithFunc) run() {
|
||||||
|
// atomic.AddInt32(&w.pool.running, 1)
|
||||||
|
// go func() {
|
||||||
|
// for {
|
||||||
|
// select {
|
||||||
|
// case args := <-w.args:
|
||||||
|
// if args == nil {
|
||||||
|
// atomic.AddInt32(&w.pool.running, -1)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// w.pool.poolFunc(args)
|
||||||
|
// w.pool.putWorker(w)
|
||||||
|
// case <-w.pool.release:
|
||||||
|
// atomic.AddInt32(&w.pool.running, -1)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }()
|
||||||
|
//}
|
||||||
|
|
||||||
// stop this worker.
|
// stop this worker.
|
||||||
func (w *WorkerWithFunc) stop() {
|
func (w *WorkerWithFunc) stop() {
|
||||||
w.args <- nil
|
w.args <- nil
|
||||||
|
|
Loading…
Reference in New Issue