Skip to content

Commit a133373

Browse files
committed
rename lo.Batch to lo.Buffer and lo.BatchWithTImeout to lo.BufferWithTimeout
1 parent 686821d commit a133373

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
@samber: I sometimes forget to update this file. Ping me on [Twitter](https://twitter.com/samuelberthe) or open an issue in case of error. We need to keep a clear changelog for easier lib upgrade.
44

5+
## 1.34.1 (2022-11-xx)
6+
7+
Adding:
8+
- lo.BufferWithTimeout (alias to lo.BatchWithTimeout)
9+
- lo.Buffer (alias to lo.Batch)
10+
11+
Deprecation:
12+
- lo.BatchWithTimeout
13+
- lo.Batch
14+
515
## 1.34.0 (2022-11-12)
616

717
Improving:

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ Supported helpers for channels:
159159
- [ChannelDispatcher](#channeldispatcher)
160160
- [SliceToChannel](#slicetochannel)
161161
- [Generator](#generator)
162-
- [Batch](#batch)
163-
- [BatchWithTimeout](#batchwithtimeout)
162+
- [Buffer](#buffer)
163+
- [BufferWithTimeout](#bufferwithtimeout)
164164
- [FanIn](#fanin)
165165
- [FanOut](#fanout)
166166

@@ -1438,16 +1438,16 @@ for v := range lo.Generator(2, generator) {
14381438
// prints 1, then 2, then 3
14391439
```
14401440

1441-
### Batch
1441+
### Buffer
14421442

14431443
Creates a slice of n elements from a channel. Returns the slice, the slice length, the read time and the channel status (opened/closed).
14441444

14451445
```go
14461446
ch := lo.SliceToChannel(2, []int{1, 2, 3, 4, 5})
14471447

1448-
items1, length1, duration1, ok1 := lo.Batch(ch, 3)
1448+
items1, length1, duration1, ok1 := lo.Buffer(ch, 3)
14491449
// []int{1, 2, 3}, 3, 0s, true
1450-
items2, length2, duration2, ok2 := lo.Batch(ch, 3)
1450+
items2, length2, duration2, ok2 := lo.Buffer(ch, 3)
14511451
// []int{4, 5}, 2, 0s, false
14521452
```
14531453

@@ -1458,7 +1458,7 @@ ch := readFromQueue()
14581458

14591459
for {
14601460
// read 1k items
1461-
items, length, _, ok := lo.Batch(ch, 1000)
1461+
items, length, _, ok := lo.Buffer(ch, 1000)
14621462

14631463
// do batching stuff
14641464

@@ -1468,7 +1468,7 @@ for {
14681468
}
14691469
```
14701470

1471-
### BatchWithTimeout
1471+
### BufferWithTimeout
14721472

14731473
Creates a slice of n elements from a channel, with timeout. Returns the slice, the slice length, the read time and the channel status (opened/closed).
14741474

@@ -1482,11 +1482,11 @@ generator := func(yield func(int)) {
14821482

14831483
ch := lo.Generator(0, generator)
14841484

1485-
items1, length1, duration1, ok1 := lo.BatchWithTimeout(ch, 3, 100*time.Millisecond)
1485+
items1, length1, duration1, ok1 := lo.BufferWithTimeout(ch, 3, 100*time.Millisecond)
14861486
// []int{1, 2}, 2, 100ms, true
1487-
items2, length2, duration2, ok2 := lo.BatchWithTimeout(ch, 3, 100*time.Millisecond)
1487+
items2, length2, duration2, ok2 := lo.BufferWithTimeout(ch, 3, 100*time.Millisecond)
14881488
// []int{3, 4, 5}, 3, 75ms, true
1489-
items3, length3, duration2, ok3 := lo.BatchWithTimeout(ch, 3, 100*time.Millisecond)
1489+
items3, length3, duration2, ok3 := lo.BufferWithTimeout(ch, 3, 100*time.Millisecond)
14901490
// []int{}, 0, 10ms, false
14911491
```
14921492

@@ -1498,7 +1498,7 @@ ch := readFromQueue()
14981498
for {
14991499
// read 1k items
15001500
// wait up to 1 second
1501-
items, length, _, ok := lo.BatchWithTimeout(ch, 1000, 1*time.Second)
1501+
items, length, _, ok := lo.BufferWithTimeout(ch, 1000, 1*time.Second)
15021502

15031503
// do batching stuff
15041504

@@ -1521,7 +1521,7 @@ consumer := func(c <-chan int) {
15211521
for {
15221522
// read 1k items
15231523
// wait up to 1 second
1524-
items, length, _, ok := lo.BatchWithTimeout(ch, 1000, 1*time.Second)
1524+
items, length, _, ok := lo.BufferWithTimeout(ch, 1000, 1*time.Second)
15251525

15261526
// do batching stuff
15271527

channel.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ func Generator[T any](bufferSize int, generator func(yield func(T))) <-chan T {
193193
return ch
194194
}
195195

196-
// Batch creates a slice of n elements from a channel. Returns the slice and the slice length.
196+
// Buffer creates a slice of n elements from a channel. Returns the slice and the slice length.
197197
// @TODO: we should probably provide an helper that reuse the same buffer.
198-
func Batch[T any](ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool) {
198+
func Buffer[T any](ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool) {
199199
buffer := make([]T, 0, size)
200200
index := 0
201201
now := time.Now()
@@ -212,9 +212,15 @@ func Batch[T any](ch <-chan T, size int) (collection []T, length int, readTime t
212212
return buffer, index, time.Since(now), true
213213
}
214214

215-
// BatchWithTimeout creates a slice of n elements from a channel, with timeout. Returns the slice and the slice length.
215+
// Buffer creates a slice of n elements from a channel. Returns the slice and the slice length.
216+
// Deprecated: Use lo.Buffer instead.
217+
func Batch[T any](ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool) {
218+
return Buffer(ch, size)
219+
}
220+
221+
// BufferWithTimeout creates a slice of n elements from a channel, with timeout. Returns the slice and the slice length.
216222
// @TODO: we should probably provide an helper that reuse the same buffer.
217-
func BatchWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int, readTime time.Duration, ok bool) {
223+
func BufferWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int, readTime time.Duration, ok bool) {
218224
expire := time.NewTimer(timeout)
219225
defer expire.Stop()
220226

@@ -239,6 +245,12 @@ func BatchWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (coll
239245
return buffer, index, time.Since(now), true
240246
}
241247

248+
// BufferWithTimeout creates a slice of n elements from a channel, with timeout. Returns the slice and the slice length.
249+
// Deprecated: Use lo.BufferWithTimeout instead.
250+
func BatchWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int, readTime time.Duration, ok bool) {
251+
return BufferWithTimeout(ch, size, timeout)
252+
}
253+
242254
// FanIn collects messages from multiple input channels into a single buffered channel.
243255
// Output messages has no priority. When all upstream channels reach EOF, downstream channel closes.
244256
func FanIn[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T {

channel_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,16 @@ func TestGenerate(t *testing.T) {
257257
is.Equal(i, 4)
258258
}
259259

260-
func TestBatch(t *testing.T) {
260+
func TestBuffer(t *testing.T) {
261261
t.Parallel()
262262
testWithTimeout(t, 10*time.Millisecond)
263263
is := assert.New(t)
264264

265265
ch := SliceToChannel(2, []int{1, 2, 3})
266266

267-
items1, length1, _, ok1 := Batch(ch, 2)
268-
items2, length2, _, ok2 := Batch(ch, 2)
269-
items3, length3, _, ok3 := Batch(ch, 2)
267+
items1, length1, _, ok1 := Buffer(ch, 2)
268+
items2, length2, _, ok2 := Buffer(ch, 2)
269+
items3, length3, _, ok3 := Buffer(ch, 2)
270270

271271
is.Equal([]int{1, 2}, items1)
272272
is.Equal(2, length1)
@@ -279,7 +279,7 @@ func TestBatch(t *testing.T) {
279279
is.False(ok3)
280280
}
281281

282-
func TestBatchWithTimeout(t *testing.T) {
282+
func TestBufferWithTimeout(t *testing.T) {
283283
t.Parallel()
284284
testWithTimeout(t, 200*time.Millisecond)
285285
is := assert.New(t)
@@ -292,27 +292,27 @@ func TestBatchWithTimeout(t *testing.T) {
292292
}
293293
ch := Generator(0, generator)
294294

295-
items1, length1, _, ok1 := BatchWithTimeout(ch, 20, 15*time.Millisecond)
295+
items1, length1, _, ok1 := BufferWithTimeout(ch, 20, 15*time.Millisecond)
296296
is.Equal([]int{0, 1}, items1)
297297
is.Equal(2, length1)
298298
is.True(ok1)
299299

300-
items2, length2, _, ok2 := BatchWithTimeout(ch, 20, 2*time.Millisecond)
300+
items2, length2, _, ok2 := BufferWithTimeout(ch, 20, 2*time.Millisecond)
301301
is.Equal([]int{}, items2)
302302
is.Equal(0, length2)
303303
is.True(ok2)
304304

305-
items3, length3, _, ok3 := BatchWithTimeout(ch, 1, 30*time.Millisecond)
305+
items3, length3, _, ok3 := BufferWithTimeout(ch, 1, 30*time.Millisecond)
306306
is.Equal([]int{2}, items3)
307307
is.Equal(1, length3)
308308
is.True(ok3)
309309

310-
items4, length4, _, ok4 := BatchWithTimeout(ch, 2, 25*time.Millisecond)
310+
items4, length4, _, ok4 := BufferWithTimeout(ch, 2, 25*time.Millisecond)
311311
is.Equal([]int{3, 4}, items4)
312312
is.Equal(2, length4)
313313
is.True(ok4)
314314

315-
items5, length5, _, ok5 := BatchWithTimeout(ch, 3, 25*time.Millisecond)
315+
items5, length5, _, ok5 := BufferWithTimeout(ch, 3, 25*time.Millisecond)
316316
is.Equal([]int{}, items5)
317317
is.Equal(0, length5)
318318
is.False(ok5)

0 commit comments

Comments
 (0)