mirror of https://github.com/panjf2000/ants.git
bug: return error before creating multi pools if lbs is invalid to avoid leaks (#317)
This commit is contained in:
parent
10d9975f10
commit
ce28ca17d1
|
@ -56,6 +56,9 @@ type MultiPool struct {
|
|||
// NewMultiPool instantiates a MultiPool with a size of the pool list and a size
|
||||
// per pool, and the load-balancing strategy.
|
||||
func NewMultiPool(size, sizePerPool int, lbs LoadBalancingStrategy, options ...Option) (*MultiPool, error) {
|
||||
if lbs != RoundRobin && lbs != LeastTasks {
|
||||
return nil, ErrInvalidLoadBalancingStrategy
|
||||
}
|
||||
pools := make([]*Pool, size)
|
||||
for i := 0; i < size; i++ {
|
||||
pool, err := NewPool(sizePerPool, options...)
|
||||
|
@ -64,9 +67,6 @@ func NewMultiPool(size, sizePerPool int, lbs LoadBalancingStrategy, options ...O
|
|||
}
|
||||
pools[i] = pool
|
||||
}
|
||||
if lbs != RoundRobin && lbs != LeastTasks {
|
||||
return nil, ErrInvalidLoadBalancingStrategy
|
||||
}
|
||||
return &MultiPool{pools: pools, lbs: lbs}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -45,6 +45,9 @@ type MultiPoolWithFunc struct {
|
|||
// NewMultiPoolWithFunc instantiates a MultiPoolWithFunc with a size of the pool list and a size
|
||||
// per pool, and the load-balancing strategy.
|
||||
func NewMultiPoolWithFunc(size, sizePerPool int, fn func(interface{}), lbs LoadBalancingStrategy, options ...Option) (*MultiPoolWithFunc, error) {
|
||||
if lbs != RoundRobin && lbs != LeastTasks {
|
||||
return nil, ErrInvalidLoadBalancingStrategy
|
||||
}
|
||||
pools := make([]*PoolWithFunc, size)
|
||||
for i := 0; i < size; i++ {
|
||||
pool, err := NewPoolWithFunc(sizePerPool, fn, options...)
|
||||
|
@ -53,9 +56,6 @@ func NewMultiPoolWithFunc(size, sizePerPool int, fn func(interface{}), lbs LoadB
|
|||
}
|
||||
pools[i] = pool
|
||||
}
|
||||
if lbs != RoundRobin && lbs != LeastTasks {
|
||||
return nil, ErrInvalidLoadBalancingStrategy
|
||||
}
|
||||
return &MultiPoolWithFunc{pools: pools, lbs: lbs}, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue