Skip to content

Instantly share code, notes, and snippets.

@maxbeutel
Created April 28, 2020 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxbeutel/78bd89afceb585dd6fdbb12a74f266d8 to your computer and use it in GitHub Desktop.
Save maxbeutel/78bd89afceb585dd6fdbb12a74f266d8 to your computer and use it in GitHub Desktop.
// walkEvents walks an event list applying a hook function.
// This is intended to be parallelized. Apply only when the
// computation has no dependency on the order in which commits
// are processed.
// Source: http://esr.ibiblio.org/?p=8607#comment-2365265
func walkEvents(events []Event, hook func(int, Event)) {
if control.flagOptions["serial"] {
for i, e := range events {
hook(i, e)
}
return
}
var (
maxWorkers = runtime.GOMAXPROCS(0)
channel = make(chan int, maxWorkers)
done = make(chan bool, maxWorkers)
)
// Create the workers that will loop though events
// Note: Each loop iteration we create a new worker
// Note: The queue "channel" is populated after this loop.
for n := 0; n < maxWorkers; n++ {
go func() {
// The for loop will stop when channel is closed
for i := range channel {
hook(i, events[i])
}
// Note: After finishing all events in its queue, the worker is done.
done <- true
}()
}
// Populate the channel with the events
for i := range events {
channel <- i
}
close(channel)
// Wait for all workers to finish
for n := 0; n < maxWorkers; n++ {
<-done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment