This commit is contained in:
Andy Pan 2018-07-06 20:24:47 +08:00
parent 339aaa4475
commit 6da1112dff
5 changed files with 19 additions and 21 deletions

View File

@ -68,4 +68,3 @@ var (
ErrPoolSizeInvalid = errors.New("invalid size for pool") ErrPoolSizeInvalid = errors.New("invalid size for pool")
ErrPoolClosed = errors.New("this pool has been closed") ErrPoolClosed = errors.New("this pool has been closed")
) )

View File

@ -32,14 +32,14 @@ import (
const ( const (
_ = 1 << (10 * iota) _ = 1 << (10 * iota)
KiB // 1024 KiB // 1024
MiB // 1048576 MiB // 1048576
GiB // 1073741824 GiB // 1073741824
TiB // 1099511627776 (超过了int32的范围) TiB // 1099511627776 (超过了int32的范围)
PiB // 1125899906842624 PiB // 1125899906842624
EiB // 1152921504606846976 EiB // 1152921504606846976
ZiB // 1180591620717411303424 (超过了int64的范围) ZiB // 1180591620717411303424 (超过了int64的范围)
YiB // 1208925819614629174706176 YiB // 1208925819614629174706176
) )
const RunTimes = 1000000 const RunTimes = 1000000
const loop = 10 const loop = 10
@ -78,7 +78,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
func BenchmarkAntsPoolWithFunc(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) {
var wg sync.WaitGroup var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(50000, 1,func(i interface{}) error { p, _ := ants.NewPoolWithFunc(50000, 1, func(i interface{}) error {
demoPoolFunc(i) demoPoolFunc(i)
wg.Done() wg.Done()
return nil return nil

View File

@ -87,16 +87,15 @@ func (p *Pool) monitorAndClear() {
}() }()
} }
// NewPool generates a instance of ants pool // NewPool generates a instance of ants pool
func NewPool(size, expiry int) (*Pool, error) { func NewPool(size, expiry int) (*Pool, error) {
if size <= 0 { if size <= 0 {
return nil, ErrPoolSizeInvalid return nil, ErrPoolSizeInvalid
} }
p := &Pool{ p := &Pool{
capacity: int32(size), capacity: int32(size),
freeSignal: make(chan sig, math.MaxInt32), freeSignal: make(chan sig, math.MaxInt32),
release: make(chan sig, 1), release: make(chan sig, 1),
expiryDuration: time.Duration(expiry) * time.Second, expiryDuration: time.Duration(expiry) * time.Second,
} }
p.monitorAndClear() p.monitorAndClear()
@ -138,7 +137,7 @@ func (p *Pool) Release() error {
for i := 0; i < running; i++ { for i := 0; i < running; i++ {
p.getWorker().stop() p.getWorker().stop()
} }
for i := range p.workers{ for i := range p.workers {
p.workers[i] = nil p.workers[i] = nil
} }
}) })

View File

@ -94,11 +94,11 @@ func NewPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) {
return nil, ErrPoolSizeInvalid return nil, ErrPoolSizeInvalid
} }
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, 1), release: make(chan sig, 1),
expiryDuration: time.Duration(expiry) * time.Second, expiryDuration: time.Duration(expiry) * time.Second,
poolFunc: f, poolFunc: f,
} }
p.MonitorAndClear() p.MonitorAndClear()
return p, nil return p, nil
@ -142,7 +142,7 @@ func (p *PoolWithFunc) Release() error {
for i := 0; i < running; i++ { for i := 0; i < running; i++ {
p.getWorker().stop() p.getWorker().stop()
} }
for i := range p.workers{ for i := range p.workers {
p.workers[i] = nil p.workers[i] = nil
} }
}) })