union: fix crash when using epff policy - fixes #5000
Before this fix using the epff policy could double close a channel. The fix refactors the code to make that impossible and cancels any running queries when the first query is found.
This commit is contained in:
parent
687a3b1832
commit
6b9e4f939d
1 changed files with 5 additions and 9 deletions
|
@ -17,7 +17,9 @@ func init() {
|
||||||
type EpFF struct{}
|
type EpFF struct{}
|
||||||
|
|
||||||
func (p *EpFF) epff(ctx context.Context, upstreams []*upstream.Fs, filePath string) (*upstream.Fs, error) {
|
func (p *EpFF) epff(ctx context.Context, upstreams []*upstream.Fs, filePath string) (*upstream.Fs, error) {
|
||||||
ch := make(chan *upstream.Fs)
|
ch := make(chan *upstream.Fs, len(upstreams))
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
for _, u := range upstreams {
|
for _, u := range upstreams {
|
||||||
u := u // Closure
|
u := u // Closure
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -30,16 +32,10 @@ func (p *EpFF) epff(ctx context.Context, upstreams []*upstream.Fs, filePath stri
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
var u *upstream.Fs
|
var u *upstream.Fs
|
||||||
for i := 0; i < len(upstreams); i++ {
|
for range upstreams {
|
||||||
u = <-ch
|
u = <-ch
|
||||||
if u != nil {
|
if u != nil {
|
||||||
// close remaining goroutines
|
break
|
||||||
go func(num int) {
|
|
||||||
defer close(ch)
|
|
||||||
for i := 0; i < num; i++ {
|
|
||||||
<-ch
|
|
||||||
}
|
|
||||||
}(len(upstreams) - 1 - i)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if u == nil {
|
if u == nil {
|
||||||
|
|
Loading…
Reference in a new issue