mirror of
https://github.com/anchore/syft.git
synced 2026-11-04 00:16:38 +01:00
make AllLocations accept a context (#2518)
The previous implementation would leak a goroutine if the caller of AllLocations stopped iterating early. Now, accept a context so that the caller can cancel the AllLocations iterator rather than leak the goroutine. Signed-off-by: Will Murphy <will.murphy@anchore.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -144,12 +145,17 @@ func (r MockResolver) RelativeFileByPath(_ Location, path string) *Location {
|
||||
return &paths[0]
|
||||
}
|
||||
|
||||
func (r MockResolver) AllLocations() <-chan Location {
|
||||
func (r MockResolver) AllLocations(ctx context.Context) <-chan Location {
|
||||
results := make(chan Location)
|
||||
go func() {
|
||||
defer close(results)
|
||||
for _, l := range r.locations {
|
||||
results <- l
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case results <- l:
|
||||
continue
|
||||
}
|
||||
}
|
||||
}()
|
||||
return results
|
||||
|
||||
Reference in New Issue
Block a user