mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-21 06:20:31 +00:00
merger: Pass reports via closure, instead of by reference in args
This is an alternate way of solving the same problem as 4007a902a264e5ff2c3be6b269ade515c9c1c145, but in a nicer way. Compared to using pointers, this approach more obviously preserves the original behaviour, and is arguably more readable than the original code. Credit to @rade for this approach.
This commit is contained in:
@@ -48,15 +48,15 @@ func (smartMerger) Merge(reports []report.Report) report.Report {
|
||||
case 1:
|
||||
return reports[0]
|
||||
}
|
||||
c := make(chan *report.Report, l)
|
||||
for i := range reports {
|
||||
c <- &reports[i]
|
||||
c := make(chan report.Report, l)
|
||||
for _, r := range reports {
|
||||
c <- r
|
||||
}
|
||||
for ; l > 1; l-- {
|
||||
go func(left, right *report.Report) {
|
||||
r := left.Merge(*right)
|
||||
c <- &r
|
||||
}(<-c, <-c)
|
||||
left, right := <-c, <-c
|
||||
go func() {
|
||||
c <- left.Merge(right)
|
||||
}()
|
||||
}
|
||||
return *<-c
|
||||
return <-c
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user