mirror of
https://github.com/tweag/gomod2nix.git
synced 2024-11-13 22:04:39 +00:00
53270b071e
This calculates the output hashes in pure Go and removes Nix from being a run time dependency of gomod2nix. Additionally it should be faster, but that's untested.
68 lines
967 B
Go
68 lines
967 B
Go
package lib
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// ParallellExecutor - Execute callback functions in parallell
|
|
type ParallellExecutor struct {
|
|
errChan chan error
|
|
wg *sync.WaitGroup
|
|
mux *sync.Mutex
|
|
|
|
// Error returned by Wait(), cached for other Wait() invocations
|
|
err error
|
|
done bool
|
|
}
|
|
|
|
func NewParallellExecutor() *ParallellExecutor {
|
|
return &ParallellExecutor{
|
|
errChan: make(chan error),
|
|
mux: new(sync.Mutex),
|
|
wg: new(sync.WaitGroup),
|
|
|
|
err: nil,
|
|
done: false,
|
|
}
|
|
}
|
|
|
|
func (e *ParallellExecutor) Add(fn func() error) {
|
|
e.wg.Add(1)
|
|
|
|
go func() {
|
|
defer e.wg.Done()
|
|
|
|
err := fn()
|
|
if err != nil {
|
|
e.errChan <- err
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (e *ParallellExecutor) Wait() error {
|
|
e.mux.Lock()
|
|
defer e.mux.Unlock()
|
|
|
|
if e.done {
|
|
return e.err
|
|
}
|
|
|
|
var err error
|
|
|
|
// Ensure channel is closed
|
|
go func() {
|
|
e.wg.Wait()
|
|
close(e.errChan)
|
|
}()
|
|
|
|
for err = range e.errChan {
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
|
|
e.done = true
|
|
e.err = err
|
|
|
|
return err
|
|
}
|