Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 712e606

Browse files
authored
Merge pull request #1043 from jmank88/verbose_counts
vendor directory writes: add counts to verbose logging, limits writers, abort on error
2 parents d8d1205 + 1b50999 commit 712e606

File tree

3 files changed

+85
-24
lines changed

3 files changed

+85
-24
lines changed

internal/gps/identifier.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ func (i ProjectIdentifier) errString() string {
148148
return fmt.Sprintf("%s (from %s)", i.ProjectRoot, i.Source)
149149
}
150150

151+
func (i ProjectIdentifier) String() string {
152+
return i.errString()
153+
}
154+
151155
func (i ProjectIdentifier) normalize() ProjectIdentifier {
152156
if i.Source == "" {
153157
i.Source = string(i.ProjectRoot)

internal/gps/result.go

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type solution struct {
4646
solv Solver
4747
}
4848

49+
const concurrentWriters = 16
50+
4951
// WriteDepTree takes a basedir and a Lock, and exports all the projects
5052
// listed in the lock to the appropriate target location within the basedir.
5153
//
@@ -65,37 +67,91 @@ func WriteDepTree(basedir string, l Lock, sm SourceManager, sv bool, logger *log
6567
return err
6668
}
6769

68-
var wg sync.WaitGroup
69-
errCh := make(chan error, len(l.Projects()))
70+
lps := l.Projects()
7071

71-
for _, p := range l.Projects() {
72-
wg.Add(1)
73-
go func(p LockedProject) {
72+
type resp struct {
73+
i int
74+
err error
75+
}
76+
respCh := make(chan resp, len(lps))
77+
writeCh := make(chan int, len(lps))
78+
cancel := make(chan struct{})
79+
80+
// Queue work.
81+
for i := range lps {
82+
writeCh <- i
83+
}
84+
close(writeCh)
85+
// Launch writers.
86+
writers := concurrentWriters
87+
if len(lps) < writers {
88+
writers = len(lps)
89+
}
90+
var wg sync.WaitGroup
91+
wg.Add(writers)
92+
for i := 0; i < writers; i++ {
93+
go func() {
7494
defer wg.Done()
75-
to := filepath.FromSlash(filepath.Join(basedir, string(p.Ident().ProjectRoot)))
76-
logger.Printf("Writing out %s@%s", p.Ident().errString(), p.Version())
7795

78-
if err := sm.ExportProject(p.Ident(), p.Version(), to); err != nil {
79-
errCh <- errors.Wrapf(err, "failed to export %s", p.Ident().ProjectRoot)
80-
return
81-
}
96+
for i := range writeCh {
97+
select {
98+
case <-cancel:
99+
return
100+
default:
101+
}
82102

83-
if sv {
84-
err := filepath.Walk(to, stripVendor)
85-
if err != nil {
86-
errCh <- errors.Wrapf(err, "failed to strip vendor from %s", p.Ident().ProjectRoot)
103+
p := lps[i]
104+
to := filepath.FromSlash(filepath.Join(basedir, string(p.Ident().ProjectRoot)))
105+
106+
if err := sm.ExportProject(p.Ident(), p.Version(), to); err != nil {
107+
respCh <- resp{i, errors.Wrapf(err, "failed to export %s", p.Ident().ProjectRoot)}
108+
continue
109+
}
110+
111+
if sv {
112+
select {
113+
case <-cancel:
114+
return
115+
default:
116+
}
117+
118+
if err := filepath.Walk(to, stripVendor); err != nil {
119+
respCh <- resp{i, errors.Wrapf(err, "failed to strip vendor from %s", p.Ident().ProjectRoot)}
120+
continue
121+
}
87122
}
123+
124+
respCh <- resp{i, nil}
88125
}
89-
}(p)
126+
}()
127+
}
128+
// Monitor writers
129+
go func() {
130+
wg.Wait()
131+
close(respCh)
132+
}()
133+
134+
// Log results and collect errors
135+
var errs []error
136+
var cnt int
137+
for resp := range respCh {
138+
cnt++
139+
msg := "Wrote"
140+
if resp.err != nil {
141+
if len(errs) == 0 {
142+
close(cancel)
143+
}
144+
errs = append(errs, resp.err)
145+
msg = "Failed to write"
146+
}
147+
p := lps[resp.i]
148+
logger.Printf("(%d/%d) %s %s@%s\n", cnt, len(lps), msg, p.Ident(), p.Version())
90149
}
91150

92-
wg.Wait()
93-
close(errCh)
94-
95-
if len(errCh) > 0 {
151+
if len(errs) > 0 {
96152
logger.Println("Failed to write dep tree. The following errors occurred:")
97-
for err := range errCh {
98-
logger.Println(" * ", err)
153+
for i, err := range errs {
154+
logger.Printf("(%d/%d) %s\n", i+1, len(errs), err)
99155
}
100156

101157
os.RemoveAll(basedir)

txn_writer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,9 @@ func (sw *SafeWriter) PrintPreparedActions(output *log.Logger, verbose bool) err
455455
if sw.writeVendor {
456456
if verbose {
457457
output.Printf("Would have written the following %d projects to the vendor directory:\n", len(sw.lock.Projects()))
458-
for _, project := range sw.lock.Projects() {
459-
output.Println(project)
458+
lps := sw.lock.Projects()
459+
for i, p := range lps {
460+
output.Printf("(%d/%d) %s@%s\n", i+1, len(lps), p.Ident(), p.Version())
460461
}
461462
} else {
462463
output.Printf("Would have written %d projects to the vendor directory.\n", len(sw.lock.Projects()))

0 commit comments

Comments
 (0)