-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnullbatcher.go
More file actions
38 lines (30 loc) · 837 Bytes
/
nullbatcher.go
File metadata and controls
38 lines (30 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package sebbroker
import (
"fmt"
"sync"
"github.com/micvbang/simple-event-broker/internal/sebrecords"
)
// nullBatcher calls persist() for every record it receives, always creating a
// record batch of size 1. This is useful for testing.
type nullBatcher struct {
mu sync.Mutex
persist Persist
}
func NewNullBatcher(persist Persist) *nullBatcher {
return &nullBatcher{
persist: persist,
}
}
func (b *nullBatcher) AddRecords(batch sebrecords.Batch) ([]uint64, error) {
b.mu.Lock()
defer b.mu.Unlock()
offsets, err := b.persist(batch)
if err != nil {
return nil, err
}
if len(offsets) != batch.Len() {
// This is not supposed to happen; if it does, we can't trust b.persist()
panic(fmt.Sprintf("unexpected number of offsets returned %d, expected %d", len(offsets), batch.Len()))
}
return offsets, nil
}