code: func (b *bufferedBackend) collectEvents(timer <-chan time.Time, stopCh <-chan struct{}) []*auditinternal.Event {
  	var events []*auditinternal.Event
  
  L:
  	for i := 0; i < b.maxBatchSize; i++ {
  		select {
  		case ev, ok := <-b.buffer:
  			// Buffer channel was closed and no new events will follow.
  			if !ok {
  				break L
  			}
  			events = append(events, ev)
  		case <-timer:
  			// Timer has expired. Send currently accumulated batch.
  			break L
  		case <-stopCh:
  			// Backend has been stopped. Send currently accumulated batch.
  			break L
  		}
  	}
  
  	return events
  }
docs: // collectEvents attempts to collect some number of events in a batch.
  //
  // The following things can cause collectEvents to stop and return the list
  // of events:
  //
  //   * Maximum number of events for a batch.
  //   * Timer has passed.
  //   * Buffer channel is closed and empty.
  //   * stopCh is closed.