mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-21 02:47:01 +00:00
fix race condition when shutting down
This commit is contained in:
parent
3a2d4705f5
commit
94f8e9e631
2 changed files with 9 additions and 15 deletions
|
|
@ -43,21 +43,18 @@ func isIgnored(path string, ignoreFilePatterns []string) (bool, error) {
|
||||||
func FromFiles(ctx context.Context, ignoreFilePatterns []string, paths ...string) (<-chan Resource, <-chan error) {
|
func FromFiles(ctx context.Context, ignoreFilePatterns []string, paths ...string) (<-chan Resource, <-chan error) {
|
||||||
resources := make(chan Resource)
|
resources := make(chan Resource)
|
||||||
errors := make(chan error)
|
errors := make(chan error)
|
||||||
stop := false
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
<-ctx.Done()
|
|
||||||
stop = true
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
// we handle errors in the walk function directly
|
// we handle errors in the walk function directly
|
||||||
// so it should be safe to discard the outer error
|
// so it should be safe to discard the outer error
|
||||||
err := filepath.Walk(path, func(p string, i os.FileInfo, err error) error {
|
err := filepath.Walk(path, func(p string, i os.FileInfo, err error) error {
|
||||||
if stop == true {
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
return io.EOF
|
return io.EOF
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,20 +42,17 @@ func SplitYAMLDocument(data []byte, atEOF bool) (advance int, token []byte, err
|
||||||
func FromStream(ctx context.Context, path string, r io.Reader) (<-chan Resource, <-chan error) {
|
func FromStream(ctx context.Context, path string, r io.Reader) (<-chan Resource, <-chan error) {
|
||||||
resources := make(chan Resource)
|
resources := make(chan Resource)
|
||||||
errors := make(chan error)
|
errors := make(chan error)
|
||||||
stop := false
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
<-ctx.Done()
|
|
||||||
stop = true
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
scanner := bufio.NewScanner(r)
|
scanner := bufio.NewScanner(r)
|
||||||
scanner.Split(SplitYAMLDocument)
|
scanner.Split(SplitYAMLDocument)
|
||||||
|
|
||||||
|
SCAN:
|
||||||
for res := scanner.Scan(); res != false; res = scanner.Scan() {
|
for res := scanner.Scan(); res != false; res = scanner.Scan() {
|
||||||
if stop == true {
|
select {
|
||||||
break
|
case <-ctx.Done():
|
||||||
|
break SCAN
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
resources <- Resource{Path: path, Bytes: scanner.Bytes()}
|
resources <- Resource{Path: path, Bytes: scanner.Bytes()}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue