Skip to content

Commit b5942bb

Browse files
committed
Simplify API
1 parent e17a19f commit b5942bb

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

batchan.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,20 @@ package batchan
22

33
import "time"
44

5-
type option[T any] func(*config[T])
5+
type option func(*config)
66

7-
type config[T any] struct {
7+
type config struct {
88
timeout time.Duration
99
hasTimeout bool
10-
splitFunc func(T, T) bool
1110
}
1211

13-
func WithTimeout[T any](timeout time.Duration) option[T] {
14-
return func(cfg *config[T]) {
12+
func WithTimeout(timeout time.Duration) option {
13+
return func(cfg *config) {
1514
cfg.timeout = timeout
1615
cfg.hasTimeout = true
1716
}
1817
}
1918

20-
func WithSplitFunc[T any](splitFunc func(T, T) bool) option[T] {
21-
return func(cfg *config[T]) {
22-
cfg.splitFunc = splitFunc
23-
}
24-
}
25-
2619
func timerOrNil(t *time.Timer) <-chan time.Time {
2720
if t != nil {
2821
return t.C
@@ -32,10 +25,8 @@ func timerOrNil(t *time.Timer) <-chan time.Time {
3225

3326
func noSplitFunc[T any](t1, t2 T) bool { return false }
3427

35-
func New[T any](in <-chan T, size int, opts ...option[T]) <-chan []T {
36-
cfg := &config[T]{
37-
splitFunc: noSplitFunc[T],
38-
}
28+
func NewWithSplit[T any](in <-chan T, size int, splitFunc func(T, T) bool, opts ...option) <-chan []T {
29+
cfg := &config{}
3930

4031
for _, opt := range opts {
4132
opt(cfg)
@@ -74,7 +65,7 @@ func New[T any](in <-chan T, size int, opts ...option[T]) <-chan []T {
7465
return
7566
}
7667

77-
if len(currentBatch) > 0 && cfg.splitFunc(currentBatch[len(currentBatch)-1], t) {
68+
if len(currentBatch) > 0 && splitFunc(currentBatch[len(currentBatch)-1], t) {
7869
flush()
7970
}
8071

@@ -90,3 +81,7 @@ func New[T any](in <-chan T, size int, opts ...option[T]) <-chan []T {
9081

9182
return out
9283
}
84+
85+
func New[T any](in <-chan T, size int, opts ...option) <-chan []T {
86+
return NewWithSplit(in, size, noSplitFunc, opts...)
87+
}

batchan_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func collectBatches[T any](out <-chan []T) [][]T {
3030

3131
func TestBatchingFlushOnTimeout(t *testing.T) {
3232
in := sendIntsToChan([]int{1, 2}, 150*time.Millisecond) // delay > timeout
33-
out := batchan.New(in, 5, batchan.WithTimeout[int](100*time.Millisecond))
33+
out := batchan.New(in, 5, batchan.WithTimeout(100*time.Millisecond))
3434

3535
got := collectBatches(out)
3636

@@ -43,7 +43,7 @@ func TestBatchingFlushOnTimeout(t *testing.T) {
4343

4444
func TestBatchingFlushOnSizeOrTimeout(t *testing.T) {
4545
in := sendIntsToChan([]int{1, 2, 3, 4}, 50*time.Millisecond)
46-
out := batchan.New(in, 2, batchan.WithTimeout[int](200*time.Millisecond))
46+
out := batchan.New(in, 2, batchan.WithTimeout(200*time.Millisecond))
4747

4848
got := collectBatches(out)
4949
expected := [][]int{{1, 2}, {3, 4}}
@@ -55,7 +55,7 @@ func TestBatchingFlushOnSizeOrTimeout(t *testing.T) {
5555

5656
func TestFlushTimeoutMultiple(t *testing.T) {
5757
in := sendIntsToChan([]int{1, 2, 3}, 300*time.Millisecond)
58-
out := batchan.New(in, 10, batchan.WithTimeout[int](200*time.Millisecond)) // small timeout, large batch size
58+
out := batchan.New(in, 10, batchan.WithTimeout(200*time.Millisecond)) // small timeout, large batch size
5959

6060
got := collectBatches(out)
6161
expected := [][]int{{1}, {2}, {3}}
@@ -77,7 +77,7 @@ func TestTimeoutResetsAfterFlush(t *testing.T) {
7777
in <- 3
7878
}()
7979

80-
out := batchan.New(in, 2, batchan.WithTimeout[int](100*time.Millisecond))
80+
out := batchan.New(in, 2, batchan.WithTimeout(100*time.Millisecond))
8181

8282
got := collectBatches(out)
8383
expected := [][]int{{1}, {2}, {3}}
@@ -148,7 +148,7 @@ func TestBatchSizeLargerThanInput(t *testing.T) {
148148

149149
func TestSplitFunc(t *testing.T) {
150150
in := sendIntsToChan([]int{1, 2, 3, 5, 6}, time.Microsecond)
151-
out := batchan.New(in, 5, batchan.WithSplitFunc(func(i1, i2 int) bool { return i2-i1 > 1 }))
151+
out := batchan.NewWithSplit(in, 5, func(i1, i2 int) bool { return i2-i1 > 1 })
152152

153153
expected := [][]int{{1, 2, 3}, {5, 6}}
154154
got := collectBatches(out)

0 commit comments

Comments
 (0)