bug: return error before creating multi pools if lbs is invalid to avoid leaks (#317)

This commit is contained in:
Akshay Nanavare 2024-03-26 17:05:40 +05:30 committed by GitHub
parent 10d9975f10
commit ce28ca17d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 6 deletions

View File

@ -56,6 +56,9 @@ type MultiPool struct {
// NewMultiPool instantiates a MultiPool with a size of the pool list and a size // NewMultiPool instantiates a MultiPool with a size of the pool list and a size
// per pool, and the load-balancing strategy. // per pool, and the load-balancing strategy.
func NewMultiPool(size, sizePerPool int, lbs LoadBalancingStrategy, options ...Option) (*MultiPool, error) { func NewMultiPool(size, sizePerPool int, lbs LoadBalancingStrategy, options ...Option) (*MultiPool, error) {
if lbs != RoundRobin && lbs != LeastTasks {
return nil, ErrInvalidLoadBalancingStrategy
}
pools := make([]*Pool, size) pools := make([]*Pool, size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
pool, err := NewPool(sizePerPool, options...) pool, err := NewPool(sizePerPool, options...)
@ -64,9 +67,6 @@ func NewMultiPool(size, sizePerPool int, lbs LoadBalancingStrategy, options ...O
} }
pools[i] = pool pools[i] = pool
} }
if lbs != RoundRobin && lbs != LeastTasks {
return nil, ErrInvalidLoadBalancingStrategy
}
return &MultiPool{pools: pools, lbs: lbs}, nil return &MultiPool{pools: pools, lbs: lbs}, nil
} }

View File

@ -45,6 +45,9 @@ type MultiPoolWithFunc struct {
// NewMultiPoolWithFunc instantiates a MultiPoolWithFunc with a size of the pool list and a size // NewMultiPoolWithFunc instantiates a MultiPoolWithFunc with a size of the pool list and a size
// per pool, and the load-balancing strategy. // per pool, and the load-balancing strategy.
func NewMultiPoolWithFunc(size, sizePerPool int, fn func(interface{}), lbs LoadBalancingStrategy, options ...Option) (*MultiPoolWithFunc, error) { 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) pools := make([]*PoolWithFunc, size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
pool, err := NewPoolWithFunc(sizePerPool, fn, options...) pool, err := NewPoolWithFunc(sizePerPool, fn, options...)
@ -53,9 +56,6 @@ func NewMultiPoolWithFunc(size, sizePerPool int, fn func(interface{}), lbs LoadB
} }
pools[i] = pool pools[i] = pool
} }
if lbs != RoundRobin && lbs != LeastTasks {
return nil, ErrInvalidLoadBalancingStrategy
}
return &MultiPoolWithFunc{pools: pools, lbs: lbs}, nil return &MultiPoolWithFunc{pools: pools, lbs: lbs}, nil
} }