sync.Pool opt

This commit is contained in:
Andy Pan 2019-10-24 22:32:12 +08:00
parent 5a77e7a59d
commit 3bfc4f2ebd
2 changed files with 10 additions and 14 deletions

View File

@ -115,13 +115,11 @@ func NewPool(size int, options ...Option) (*Pool, error) {
lock: internal.NewSpinLock(), lock: internal.NewSpinLock(),
options: opts, options: opts,
} }
p.workerCache = sync.Pool{ p.workerCache.New = func() interface{} {
New: func() interface{} {
return &goWorker{ return &goWorker{
pool: p, pool: p,
task: make(chan func(), workerChanCap), task: make(chan func(), workerChanCap),
} }
},
} }
if p.options.PreAlloc { if p.options.PreAlloc {
p.workers = newWorkerArray(loopQueueType, size) p.workers = newWorkerArray(loopQueueType, size)

View File

@ -137,13 +137,11 @@ func NewPoolWithFunc(size int, pf func(interface{}), options ...Option) (*PoolWi
lock: internal.NewSpinLock(), lock: internal.NewSpinLock(),
options: opts, options: opts,
} }
p.workerCache = sync.Pool{ p.workerCache.New = func() interface{} {
New: func() interface{} {
return &goWorkerWithFunc{ return &goWorkerWithFunc{
pool: p, pool: p,
args: make(chan interface{}, workerChanCap), args: make(chan interface{}, workerChanCap),
} }
},
} }
if p.options.PreAlloc { if p.options.PreAlloc {
p.workers = make([]*goWorkerWithFunc, 0, size) p.workers = make([]*goWorkerWithFunc, 0, size)