Skip to content

Commit

Permalink
Replace expectedResultCount with counting running jobs.
Browse files Browse the repository at this point in the history
expectedResultCount was wrong as soon one run of walk results in several additionas to jobQueue.
Counting the running jobs is more robust and also easier to understand.
  • Loading branch information
sfleiter authored and tg44 committed Oct 2, 2023
1 parent eac93a7 commit 10630bd
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pkg/walker/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,27 @@ func Run(jobs []WalkJob, par int, bufferSize int, verbose int) []string {
results := make(chan []string, bufferSize)
jobQueue := make(chan WalkJob, bufferSize)
spawn := make(chan WalkJob, bufferSize)
runningJobCount := 0

for i, job := range jobs {
if (i + 1) <= par {
runningJobCount += 1
spawn <- job
} else {
jobQueue <- job
}
}

globalResult := []string{}
expectedResultCount := initialJobCount
maxId := 0
for expectedResultCount > 0 {
for runningJobCount > 0 {
select {
case singleResult := <-results:
globalResult = append(globalResult, singleResult...)
expectedResultCount -= 1
runningJobCount -= 1
if len(jobQueue) > 0 {
// check then act is safe since only this one thread received from jobQueue
runningJobCount += 1
spawn <- <-jobQueue
}
case j := <-spawn:
Expand Down

0 comments on commit 10630bd

Please sign in to comment.