Codebase list golang-github-vbauerster-mpb / 19a7db7
new drawer goroutine for each bar; print stack if panic Vladimir Bauer 9 years ago
1 changed file(s) with 21 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
55 "io"
66 "log"
77 "os"
8 "runtime"
89 "sync"
910 "time"
1011 "unicode/utf8"
205206 t.Stop()
206207 close(p.done)
207208 }()
208 const numDrawers = 3
209 bars := make([]*Bar, 0, 4)
209 bars := make([]*Bar, 0, 3)
210210 var beforeRender BeforeRender
211211 var wg sync.WaitGroup
212212 recoverIfPanic := func() {
213 if e := recover(); e != nil {
214 logger.Printf("unexpected panic: %+v\n", e)
213 if p := recover(); p != nil {
214 logger.Printf("unexpected panic: %+v\n", p)
215 var buf [4096]byte
216 n := runtime.Stack(buf[:], false)
217 os.Stderr.Write(buf[:n])
215218 }
216219 wg.Done()
217220 }
244247 respCh <- len(bars)
245248 case beforeRender = <-p.brCh:
246249 case <-t.C:
250 numBars := len(bars)
251
252 if numBars == 0 {
253 break
254 }
255
247256 if beforeRender != nil {
248257 beforeRender(bars)
249258 }
250259
251260 width, _, _ := cwriter.GetTermSize()
252261 ibars := iBarsGen(bars, width)
253 c := make(chan indexedBarBuffer)
254 wg.Add(numDrawers)
255 for i := 0; i < numDrawers; i++ {
262 ibbCh := make(chan indexedBarBuffer)
263 wg.Add(numBars)
264 for i := 0; i < numBars; i++ {
256265 go func() {
257266 defer recoverIfPanic()
258 drawer(ibars, c)
267 drawer(ibars, ibbCh)
259268 }()
260269 }
261270 go func() {
262271 wg.Wait()
263 close(c)
272 close(ibbCh)
264273 }()
265274
266275 m := make(map[int][]byte, len(bars))
267 for r := range c {
276 for r := range ibbCh {
268277 m[r.index] = r.buf
269278 }
270279 for i := 0; i < len(bars); i++ {
285294 }
286295 }
287296
288 func drawer(ibars <-chan indexedBar, c chan<- indexedBarBuffer) {
297 func drawer(ibars <-chan indexedBar, ibbCh chan<- indexedBarBuffer) {
289298 for b := range ibars {
290299 buf := b.bar.bytes(b.termWidth)
291300 buf = append(buf, '\n')
292 c <- indexedBarBuffer{b.index, buf}
301 ibbCh <- indexedBarBuffer{b.index, buf}
293302 }
294303 }
295304