pass &wg to decoratorEwmaUpdate
Vladimir Bauer
1 year, 10 months ago
| 244 | 244 | if current < 0 { |
| 245 | 245 | return |
| 246 | 246 | } |
| 247 | select { | |
| 248 | case b.operateState <- func(s *bState) { | |
| 249 | s.decoratorEwmaUpdate(current-s.current, iterDur) | |
| 247 | result := make(chan *sync.WaitGroup) | |
| 248 | select { | |
| 249 | case b.operateState <- func(s *bState) { | |
| 250 | var wg sync.WaitGroup | |
| 251 | s.decoratorEwmaUpdate(current-s.current, iterDur, &wg) | |
| 250 | 252 | s.current = current |
| 251 | 253 | if s.triggerComplete && s.current >= s.total { |
| 252 | 254 | s.current = s.total |
| 253 | 255 | s.completed = true |
| 254 | 256 | b.triggerCompletion(s) |
| 255 | 257 | } |
| 256 | }: | |
| 258 | result <- &wg | |
| 259 | }: | |
| 260 | wg := <-result | |
| 261 | wg.Wait() | |
| 257 | 262 | case <-b.ctx.Done(): |
| 258 | 263 | } |
| 259 | 264 | } |
| 296 | 301 | // EwmaIncrInt64 increments progress by amount of n and updates EWMA based |
| 297 | 302 | // decorators by dur of a single iteration. |
| 298 | 303 | func (b *Bar) EwmaIncrInt64(n int64, iterDur time.Duration) { |
| 299 | select { | |
| 300 | case b.operateState <- func(s *bState) { | |
| 301 | s.decoratorEwmaUpdate(n, iterDur) | |
| 304 | result := make(chan *sync.WaitGroup) | |
| 305 | select { | |
| 306 | case b.operateState <- func(s *bState) { | |
| 307 | var wg sync.WaitGroup | |
| 308 | s.decoratorEwmaUpdate(n, iterDur, &wg) | |
| 302 | 309 | s.current += n |
| 303 | 310 | if s.triggerComplete && s.current >= s.total { |
| 304 | 311 | s.current = s.total |
| 305 | 312 | s.completed = true |
| 306 | 313 | b.triggerCompletion(s) |
| 307 | 314 | } |
| 308 | }: | |
| 315 | result <- &wg | |
| 316 | }: | |
| 317 | wg := <-result | |
| 318 | wg.Wait() | |
| 309 | 319 | case <-b.ctx.Done(): |
| 310 | 320 | } |
| 311 | 321 | } |
| 554 | 564 | return table |
| 555 | 565 | } |
| 556 | 566 | |
| 557 | func (s bState) decoratorEwmaUpdate(n int64, dur time.Duration) { | |
| 558 | var wg sync.WaitGroup | |
| 567 | func (s bState) decoratorEwmaUpdate(n int64, dur time.Duration, wg *sync.WaitGroup) { | |
| 559 | 568 | for i := 0; i < len(s.ewmaDecorators); i++ { |
| 569 | wg.Add(1) | |
| 560 | 570 | switch d := s.ewmaDecorators[i]; i { |
| 561 | 571 | case len(s.ewmaDecorators) - 1: |
| 562 | 572 | d.EwmaUpdate(n, dur) |
| 573 | wg.Done() | |
| 563 | 574 | default: |
| 564 | wg.Add(1) | |
| 565 | 575 | go func() { |
| 566 | 576 | d.EwmaUpdate(n, dur) |
| 567 | 577 | wg.Done() |
| 568 | 578 | }() |
| 569 | 579 | } |
| 570 | 580 | } |
| 571 | wg.Wait() | |
| 572 | 581 | } |
| 573 | 582 | |
| 574 | 583 | func (s bState) decoratorAverageAdjust(start time.Time, wg *sync.WaitGroup) { |