-
Notifications
You must be signed in to change notification settings - Fork 1k
vendor directory writes: add counts to verbose logging, limits writers, abort on error #1043
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"log" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
|
@@ -65,37 +66,91 @@ func WriteDepTree(basedir string, l Lock, sm SourceManager, sv bool, logger *log | |
return err | ||
} | ||
|
||
var wg sync.WaitGroup | ||
errCh := make(chan error, len(l.Projects())) | ||
lps := l.Projects() | ||
|
||
type resp struct { | ||
i int | ||
err error | ||
} | ||
respCh := make(chan resp, len(lps)) | ||
writeCh := make(chan int, len(lps)) | ||
cancel := make(chan struct{}) | ||
|
||
for _, p := range l.Projects() { | ||
wg.Add(1) | ||
go func(p LockedProject) { | ||
// Queue work. | ||
for i := range lps { | ||
writeCh <- i | ||
} | ||
close(writeCh) | ||
// Launch writers. | ||
writers := runtime.GOMAXPROCS(-1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps an optimal number is some factor of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm not sure that GOMAXPROCS is the right thing, here - the goroutines themselves aren't actually doing work, but end up mostly in a sleep mode waiting for their spawned subprocesses. limiting it to GOMAXPROCS seems likely to be lower than we want. i think i'd rather just pick an arbitrary number to begin with - let's say 16 - and see how we do. |
||
if len(lps) < writers { | ||
writers = len(lps) | ||
} | ||
var wg sync.WaitGroup | ||
wg.Add(writers) | ||
for i := 0; i < writers; i++ { | ||
go func() { | ||
defer wg.Done() | ||
to := filepath.FromSlash(filepath.Join(basedir, string(p.Ident().ProjectRoot))) | ||
logger.Printf("Writing out %s@%s", p.Ident().errString(), p.Version()) | ||
|
||
if err := sm.ExportProject(p.Ident(), p.Version(), to); err != nil { | ||
errCh <- errors.Wrapf(err, "failed to export %s", p.Ident().ProjectRoot) | ||
return | ||
} | ||
for i := range writeCh { | ||
select { | ||
case <-cancel: | ||
return | ||
default: | ||
} | ||
|
||
if sv { | ||
err := filepath.Walk(to, stripVendor) | ||
if err != nil { | ||
errCh <- errors.Wrapf(err, "failed to strip vendor from %s", p.Ident().ProjectRoot) | ||
p := lps[i] | ||
to := filepath.FromSlash(filepath.Join(basedir, string(p.Ident().ProjectRoot))) | ||
|
||
if err := sm.ExportProject(p.Ident(), p.Version(), to); err != nil { | ||
respCh <- resp{i, errors.Wrapf(err, "failed to export %s", p.Ident().ProjectRoot)} | ||
continue | ||
} | ||
|
||
if sv { | ||
select { | ||
case <-cancel: | ||
return | ||
default: | ||
} | ||
|
||
if err := filepath.Walk(to, stripVendor); err != nil { | ||
respCh <- resp{i, errors.Wrapf(err, "failed to strip vendor from %s", p.Ident().ProjectRoot)} | ||
continue | ||
} | ||
} | ||
|
||
respCh <- resp{i, nil} | ||
} | ||
}(p) | ||
}() | ||
} | ||
// Monitor writers | ||
go func() { | ||
wg.Wait() | ||
close(respCh) | ||
}() | ||
|
||
// Log results and collect errors | ||
var errs []error | ||
var cnt int | ||
for resp := range respCh { | ||
cnt++ | ||
msg := "Wrote" | ||
if resp.err != nil { | ||
if len(errs) == 0 { | ||
close(cancel) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to know to break out of the loop explicitly here. i believe there's a possibility of a double-close on the cancel channel:
note that because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gah, you're right, i just breezed right by that check. ok 👍 |
||
} | ||
errs = append(errs, resp.err) | ||
msg = "Failed to write" | ||
} | ||
p := lps[resp.i] | ||
logger.Printf("(%d/%d) %s %s@%s\n", cnt, len(lps), msg, p.Ident(), p.Version()) | ||
} | ||
|
||
wg.Wait() | ||
close(errCh) | ||
|
||
if len(errCh) > 0 { | ||
if len(errs) > 0 { | ||
logger.Println("Failed to write dep tree. The following errors occurred:") | ||
for err := range errCh { | ||
logger.Println(" * ", err) | ||
for i, err := range errs { | ||
logger.Printf("(%d/%d) %s\n", i+1, len(errs), err) | ||
} | ||
|
||
removeAll(basedir) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any objections to this
errString
format becoming the canonical exported String method format?fmt.Sprintf("%s (from %s)", i.ProjectRoot, i.Source)
errString()
has 36 usages, all of which are passed to formatting functions with%s
, so I'd like to do a follow up PR to just absorb that method into this one and let them all implicitly call this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmm yes, i like this. the individual properties are accessible already - callers can easily construct their own output if they so choose. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(separate PR, though)