address issue #115
Vladimir Bauer
3 years ago
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | func (s *pState) flush(cw *cwriter.Writer, height int) error { |
| 286 | var wg sync.WaitGroup | |
| 286 | 287 | var popCount int |
| 287 | 288 | rows := make([]io.Reader, 0, height) |
| 288 | 289 | pool := make([]*Bar, 0, s.bHeap.Len()) |
| 291 | 292 | b := heap.Pop(&s.bHeap).(*Bar) |
| 292 | 293 | frame := <-b.frameCh |
| 293 | 294 | for i := len(frame.rows) - 1; i >= 0; i-- { |
| 294 | if len(rows) == height { | |
| 295 | break | |
| 296 | } | |
| 297 | rows = append(rows, frame.rows[i]) | |
| 298 | frameRowsUsed++ | |
| 295 | if len(rows) < height { | |
| 296 | rows = append(rows, frame.rows[i]) | |
| 297 | frameRowsUsed++ | |
| 298 | } else { | |
| 299 | wg.Add(1) | |
| 300 | go func(discardRow io.Reader) { | |
| 301 | _, _ = io.Copy(io.Discard, discardRow) | |
| 302 | wg.Done() | |
| 303 | }(frame.rows[i]) | |
| 304 | } | |
| 299 | 305 | } |
| 300 | 306 | if frame.shutdown != 0 { |
| 301 | 307 | b.Wait() // waiting for b.done, so it's safe to read b.bs |
| 333 | 339 | } |
| 334 | 340 | } |
| 335 | 341 | |
| 342 | wg.Wait() | |
| 336 | 343 | return cw.Flush(len(rows) - popCount) |
| 337 | 344 | } |
| 338 | 345 | |