Skip to content

Commit 6514595

Browse files
committed
Initial version (without timeout)
0 parents  commit 6514595

7 files changed

Lines changed: 153 additions & 0 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: CI
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
- name: Setup Go
12+
uses: actions/setup-go@v5
13+
with:
14+
go-version: '1.24'
15+
- name: Install dependencies
16+
run: go get .
17+
- name: Build
18+
run: go build -v ./...
19+
- name: Test with the Go CLI
20+
run: go test
21+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Vsevolod Strukchinsky
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# BatChan
2+
3+
[![CI](https://github.com/floatdrop/batchan/actions/workflows/ci.yaml/badge.svg)](https://github.com/floatdrop/batchan/actions/workflows/ci.yaml)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/floatdrop/batchan)](https://goreportcard.com/report/github.com/floatdrop/batchan)
5+
[![Go Reference](https://pkg.go.dev/badge/github.com/floatdrop/batchan.svg)](https://pkg.go.dev/github.com/floatdrop/batchan)
6+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7+
8+
## Installation
9+
10+
```bash
11+
go get github.com/floatdrop/batchan
12+
```
13+
14+
## Usage
15+
16+
```go
17+
package main
18+
19+
import (
20+
"fmt"
21+
"time"
22+
23+
"github.com/floatdrop/batchan"
24+
)
25+
26+
func main() {
27+
input := make(chan string, 5)
28+
batches := batchan.New(input, 3)
29+
30+
go func() {
31+
inputs := []string{"A", "B", "C", "D", "E"}
32+
for _, v := range inputs {
33+
input <- v
34+
}
35+
close(input)
36+
}()
37+
38+
for v := range batches {
39+
fmt.Println("Got:", v)
40+
}
41+
42+
// Output:
43+
// Got: [A B C]
44+
// Got: [D E]
45+
}
46+
```
47+
48+
## License
49+
50+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

batchan.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package batchan
2+
3+
func New[T any](in <-chan T, size int) <-chan []T {
4+
out := make(chan []T)
5+
6+
go func() {
7+
defer close(out)
8+
9+
var currentBatch []T
10+
11+
flush := func() {
12+
if len(currentBatch) > 0 {
13+
out <- currentBatch
14+
currentBatch = nil
15+
}
16+
}
17+
18+
for t := range in {
19+
if len(currentBatch) >= size {
20+
flush()
21+
}
22+
currentBatch = append(currentBatch, t)
23+
}
24+
25+
flush()
26+
}()
27+
28+
return out
29+
}

batchan_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package batchan

example_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package batchan_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/floatdrop/batchan"
7+
)
8+
9+
func ExampleNew() {
10+
input := make(chan string, 5)
11+
batches := batchan.New(input, 3)
12+
13+
go func() {
14+
inputs := []string{"A", "B", "C", "D", "E"}
15+
for _, v := range inputs {
16+
input <- v
17+
}
18+
close(input)
19+
}()
20+
21+
for v := range batches {
22+
fmt.Println("Got:", v)
23+
}
24+
25+
// Output:
26+
// Got: [A B C]
27+
// Got: [D E]
28+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/floatdrop/batchan
2+
3+
go 1.23

0 commit comments

Comments
 (0)