| 93 | } |
| 94 | |
| 95 | func (nb *notificationBatcher) flush(ctx context.Context) { |
| 96 | if len(nb.notifications) == 0 { |
| 97 | return |
| 98 | } |
| 99 | |
| 100 | count := min(len(nb.notifications), maxNotificationsPerInterval) |
| 101 | notifications := make([]stacksapi.StackNotification, count) |
| 102 | for i := range count { |
| 103 | notifications[i] = <-nb.notifications |
| 104 | } |
| 105 | |
| 106 | var wg sync.WaitGroup |
| 107 | for batch := range slices.Chunk(notifications, maxNotificationsPerBatch) { |
| 108 | wg.Go(func() { |
| 109 | nb.sendBatch(ctx, batch) |
| 110 | }) |
| 111 | } |
| 112 | wg.Wait() |
| 113 | } |
| 114 | |
| 115 | func (nb *notificationBatcher) sendBatch(ctx context.Context, notifications []stacksapi.StackNotification) { |
| 116 | req := stacksapi.CreateStackNotificationsRequest{ |