Created
August 3, 2025 22:32
-
-
Save alexrios/68b8b380f0c1ed05ec2079021d5f3c13 to your computer and use it in GitHub Desktop.
Iterator-fed worker pool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ProcessConcurrently[T any]( | |
input iter.Seq[T], | |
workerCount int, | |
processor func(T) error, | |
) error { | |
jobs := make(chan T, workerCount) | |
errors := make(chan error, workerCount) | |
// Start worker goroutines | |
var wg sync.WaitGroup | |
for i := 0; i < workerCount; i++ { | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
for job := range jobs { | |
if err := processor(job); err != nil { | |
errors <- err | |
return | |
} | |
} | |
}() | |
} | |
// Feed iterator to jobs channel | |
go func() { | |
defer close(jobs) | |
for item := range input { | |
jobs <- item | |
} | |
}() | |
wg.Wait() | |
return checkErrors(errors) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment