|
| 1 | +package batcher |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// Batcher provides an API for accumulating items into a batch for processing. |
| 10 | +type Batcher interface { |
| 11 | + // Put adds items to the batcher. |
| 12 | + Put(interface{}) error |
| 13 | + |
| 14 | + // Get retrieves a batch from the batcher. This call will block until |
| 15 | + // one of the conditions for a "complete" batch is reached. |
| 16 | + Get() ([]interface{}, error) |
| 17 | + |
| 18 | + // Dispose will dispose of the batcher. Any calls to Put or Get |
| 19 | + // will return errors. |
| 20 | + Dispose() |
| 21 | +} |
| 22 | + |
| 23 | +// ErrDisposed is the error returned for a disposed Batcher |
| 24 | +var ErrDisposed = errors.New("batcher: disposed") |
| 25 | + |
| 26 | +// CalculateBytes evaluates the number of bytes in an item added to a Batcher. |
| 27 | +type CalculateBytes func(interface{}) uint |
| 28 | + |
| 29 | +type basicBatcher struct { |
| 30 | + maxTime time.Duration |
| 31 | + maxItems uint |
| 32 | + maxBytes uint |
| 33 | + queueLen uint |
| 34 | + calculateBytes CalculateBytes |
| 35 | + disposed bool |
| 36 | + items []interface{} |
| 37 | + lock sync.RWMutex |
| 38 | + batchChan chan []interface{} |
| 39 | + availableBytes uint |
| 40 | +} |
| 41 | + |
| 42 | +// New creates a new Batcher using the provided arguments. |
| 43 | +// Batch readiness can be determined in three ways: |
| 44 | +// - Maximum number of bytes per batch |
| 45 | +// - Maximum number of items per batch |
| 46 | +// - Maximum amount of time waiting for a batch |
| 47 | +// Values of zero for one of these fields indicate they should not be |
| 48 | +// taken into account when evaluating the readiness of a batch. |
| 49 | +func New(maxTime time.Duration, maxItems, maxBytes, queueLen uint, calculate CalculateBytes) (Batcher, error) { |
| 50 | + if maxBytes > 0 && calculate == nil { |
| 51 | + return nil, errors.New("batcher: must provide CalculateBytes function") |
| 52 | + } |
| 53 | + |
| 54 | + return &basicBatcher{ |
| 55 | + maxTime: maxTime, |
| 56 | + maxItems: maxItems, |
| 57 | + maxBytes: maxBytes, |
| 58 | + queueLen: queueLen, |
| 59 | + calculateBytes: calculate, |
| 60 | + items: make([]interface{}, 0, maxItems), |
| 61 | + batchChan: make(chan []interface{}, queueLen), |
| 62 | + }, nil |
| 63 | +} |
| 64 | + |
| 65 | +// Put adds items to the batcher. |
| 66 | +func (b *basicBatcher) Put(item interface{}) error { |
| 67 | + b.lock.Lock() |
| 68 | + if b.disposed { |
| 69 | + b.lock.Unlock() |
| 70 | + return ErrDisposed |
| 71 | + } |
| 72 | + |
| 73 | + b.items = append(b.items, item) |
| 74 | + b.availableBytes += b.calculateBytes(item) |
| 75 | + if b.ready() { |
| 76 | + b.batchChan <- b.items |
| 77 | + b.items = make([]interface{}, 0, b.maxItems) |
| 78 | + b.availableBytes = 0 |
| 79 | + } |
| 80 | + |
| 81 | + b.lock.Unlock() |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// Get retrieves a batch from the batcher. This call will block until |
| 86 | +// one of the conditions for a "complete" batch is reached. |
| 87 | +func (b *basicBatcher) Get() ([]interface{}, error) { |
| 88 | + b.lock.RLock() |
| 89 | + if b.disposed { |
| 90 | + b.lock.RUnlock() |
| 91 | + return nil, ErrDisposed |
| 92 | + } |
| 93 | + b.lock.RUnlock() |
| 94 | + |
| 95 | + var timeout <-chan time.Time |
| 96 | + if b.maxTime > 0 { |
| 97 | + timeout = time.After(b.maxTime) |
| 98 | + } |
| 99 | + |
| 100 | + select { |
| 101 | + case items, ok := <-b.batchChan: |
| 102 | + if !ok { |
| 103 | + return nil, ErrDisposed |
| 104 | + } |
| 105 | + return items, nil |
| 106 | + case <-timeout: |
| 107 | + b.lock.Lock() |
| 108 | + if b.disposed { |
| 109 | + b.lock.Unlock() |
| 110 | + return nil, ErrDisposed |
| 111 | + } |
| 112 | + items := b.items |
| 113 | + b.items = make([]interface{}, 0, b.maxItems) |
| 114 | + b.availableBytes = 0 |
| 115 | + b.lock.Unlock() |
| 116 | + return items, nil |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// Dispose will dispose of the batcher. Any calls to Put or Get |
| 121 | +// will return errors. |
| 122 | +func (b *basicBatcher) Dispose() { |
| 123 | + b.lock.Lock() |
| 124 | + b.disposed = true |
| 125 | + b.items = nil |
| 126 | + close(b.batchChan) |
| 127 | + b.lock.Unlock() |
| 128 | +} |
| 129 | + |
| 130 | +func (b *basicBatcher) ready() bool { |
| 131 | + if b.maxItems != 0 && uint(len(b.items)) >= b.maxItems { |
| 132 | + return true |
| 133 | + } |
| 134 | + if b.maxBytes != 0 && b.availableBytes >= b.maxBytes { |
| 135 | + return true |
| 136 | + } |
| 137 | + return false |
| 138 | +} |
0 commit comments