Created
April 28, 2020 05:18
-
-
Save maxbeutel/78bd89afceb585dd6fdbb12a74f266d8 to your computer and use it in GitHub Desktop.
This file contains 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
// 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