forked from rosa/hakurei
internal/pkg: move concurrent cure implementation
This is useful independent of cure and might replace the Collect hack. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
+23
-17
@@ -1922,6 +1922,28 @@ func (c *Cache) tryExtern(
|
||||
return checksum, err
|
||||
}
|
||||
|
||||
// cureMany concurrently collects outcome of multiple [Artifact].
|
||||
func (c *Cache) cureMany(inputs []Artifact, r map[Artifact]cureRes) error {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(inputs))
|
||||
res := make([]cureRes, len(inputs))
|
||||
errs := make(DependencyCureError, 0, len(inputs))
|
||||
var errsMu sync.Mutex
|
||||
for i, d := range inputs {
|
||||
pending := pendingArtifactDep{d, &res[i], &errs, &errsMu, &wg}
|
||||
go pending.cure(c)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if len(errs) > 0 {
|
||||
return &errs
|
||||
}
|
||||
for i, p := range res {
|
||||
r[inputs[i]] = p
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// cure implements Cure without acquiring a read lock on abortMu. cure must not
|
||||
// be entered during Abort.
|
||||
func (c *Cache) cure(a Artifact, curesExempt bool) (
|
||||
@@ -2182,25 +2204,9 @@ func (c *Cache) cure(a Artifact, curesExempt bool) (
|
||||
case FloodArtifact:
|
||||
inputs := a.Inputs()
|
||||
f := FContext{t, make(map[Artifact]cureRes, len(inputs))}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(inputs))
|
||||
res := make([]cureRes, len(inputs))
|
||||
errs := make(DependencyCureError, 0, len(inputs))
|
||||
var errsMu sync.Mutex
|
||||
for i, d := range inputs {
|
||||
pending := pendingArtifactDep{d, &res[i], &errs, &errsMu, &wg}
|
||||
go pending.cure(c)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if len(errs) > 0 {
|
||||
err = &errs
|
||||
if err = c.cureMany(inputs, f.inputs); err != nil {
|
||||
return
|
||||
}
|
||||
for i, p := range res {
|
||||
f.inputs[inputs[i]] = p
|
||||
}
|
||||
|
||||
sh := sha512.New384()
|
||||
err = c.encode(sh, a, f.inputs)
|
||||
|
||||
Reference in New Issue
Block a user