iterate subscribed decorators concurrently...
but only if there is more than one
Vladimir Bauer
4 years ago
| 488 | 488 | |
| 489 | 489 | func (s bState) decoratorEwmaUpdate(dur time.Duration) { |
| 490 | 490 | wg := new(sync.WaitGroup) |
| 491 | wg.Add(len(s.ewmaDecorators)) | |
| 492 | for _, d := range s.ewmaDecorators { | |
| 493 | d := d | |
| 494 | go func() { | |
| 491 | for i := 0; i < len(s.ewmaDecorators); i++ { | |
| 492 | switch d := s.ewmaDecorators[i]; i { | |
| 493 | case len(s.ewmaDecorators) - 1: | |
| 495 | 494 | d.EwmaUpdate(s.lastIncrement, dur) |
| 496 | wg.Done() | |
| 497 | }() | |
| 495 | default: | |
| 496 | wg.Add(1) | |
| 497 | go func() { | |
| 498 | d.EwmaUpdate(s.lastIncrement, dur) | |
| 499 | wg.Done() | |
| 500 | }() | |
| 501 | } | |
| 498 | 502 | } |
| 499 | 503 | wg.Wait() |
| 500 | 504 | } |
| 501 | 505 | |
| 502 | 506 | func (s bState) decoratorAverageAdjust(start time.Time) { |
| 503 | 507 | wg := new(sync.WaitGroup) |
| 504 | wg.Add(len(s.averageDecorators)) | |
| 505 | for _, d := range s.averageDecorators { | |
| 506 | d := d | |
| 507 | go func() { | |
| 508 | for i := 0; i < len(s.averageDecorators); i++ { | |
| 509 | switch d := s.averageDecorators[i]; i { | |
| 510 | case len(s.averageDecorators) - 1: | |
| 508 | 511 | d.AverageAdjust(start) |
| 509 | wg.Done() | |
| 510 | }() | |
| 512 | default: | |
| 513 | wg.Add(1) | |
| 514 | go func() { | |
| 515 | d.AverageAdjust(start) | |
| 516 | wg.Done() | |
| 517 | }() | |
| 518 | } | |
| 511 | 519 | } |
| 512 | 520 | wg.Wait() |
| 513 | 521 | } |
| 514 | 522 | |
| 515 | 523 | func (s bState) decoratorShutdownNotify() { |
| 516 | 524 | wg := new(sync.WaitGroup) |
| 517 | wg.Add(len(s.shutdownListeners)) | |
| 518 | for _, d := range s.shutdownListeners { | |
| 519 | d := d | |
| 520 | go func() { | |
| 525 | for i := 0; i < len(s.shutdownListeners); i++ { | |
| 526 | switch d := s.shutdownListeners[i]; i { | |
| 527 | case len(s.shutdownListeners) - 1: | |
| 521 | 528 | d.Shutdown() |
| 522 | wg.Done() | |
| 523 | }() | |
| 529 | default: | |
| 530 | wg.Add(1) | |
| 531 | go func() { | |
| 532 | d.Shutdown() | |
| 533 | wg.Done() | |
| 534 | }() | |
| 535 | } | |
| 524 | 536 | } |
| 525 | 537 | wg.Wait() |
| 526 | 538 | } |