Generic channel utilities for Go 1.27+, with no dependencies.
go get github.com/floatdrop/chan@latestGroups values into slices by size, timeout or a split function.
// Insert rows in batches of up to 1000, and never keep a row waiting for more than 50ms.
for rows := range batch.New(ctx, in, 1000, batch.WithTimeout(50*time.Millisecond)) {
db.InsertMany(rows)
}A batch is sent when it is full, when its timeout expires (counted from its first value), or when the input is closed. It is never empty, and the receiver owns the slice. To batch by time alone, pass math.MaxInt as the size. NewFunc can also end a batch early, for example when the key changes:
byUser := func(prev, next Event) bool { return prev.UserID != next.UserID }
for events := range batch.NewFunc(ctx, in, 100, byUser) {
// events are consecutive and all belong to one user.
}A bounded channel that drops the oldest value instead of blocking the sender.
r := ring.New[Metric](128)
r.Send(m) // Never blocks: when the ring is full, the oldest value is dropped.
for m := range r.C { // A slow receiver sees the newest values.
report(m)
}
r.Dropped() // How many values were discarded.ring.From(ctx, in, size) puts a ring in front of an existing channel. Its goroutine owns the ring, so From returns a *ring.Receiver that offers only C and Dropped().
Fans values out by key: ByKey starts a consumer for each key it sees and gives it that key's values in order. It returns once every consumer has returned:
err := demux.ByKey(ctx, in, func(o Order) string { return o.CustomerID },
func(customer string, orders <-chan Order) {
for o := range orders {
process(customer, o) // One customer's orders are processed one at a time.
}
},
demux.WithMaxKeys(10_000), // Close the least recently used key beyond this limit.
demux.WithBuffer(16),
)go test -run='^$' -bench=. -benchmem ./...One op is one value. The input channels are buffered, and the receivers do no work. The numbers are mostly channel hand-offs between goroutines.
goos: darwin
goarch: arm64
cpu: Apple M3 Max
pkg: github.com/floatdrop/chan/batch
BenchmarkNew/size=1-14 2625022 443.9 ns/op 8 B/op 0 allocs/op
BenchmarkNew/size=16-14 14132894 84.89 ns/op 8 B/op 0 allocs/op
BenchmarkNew/size=256-14 29350527 41.02 ns/op 8 B/op 0 allocs/op
BenchmarkNewTimeout/size=1-14 2813750 422.6 ns/op 7 B/op 0 allocs/op
BenchmarkNewTimeout/size=16-14 12316239 100.6 ns/op 8 B/op 0 allocs/op
BenchmarkNewTimeout/size=256-14 27033140 43.46 ns/op 8 B/op 0 allocs/op
BenchmarkNewFunc-14 15001366 79.33 ns/op 8 B/op 0 allocs/op
pkg: github.com/floatdrop/chan/demux
BenchmarkByKey/keys=1-14 8403237 131.1 ns/op 0 B/op 0 allocs/op
BenchmarkByKey/keys=16-14 5907326 200.6 ns/op 0 B/op 0 allocs/op
BenchmarkByKey/keys=1024-14 3959383 301.9 ns/op 0 B/op 0 allocs/op
BenchmarkByKeyChurn-14 1289388 922.8 ns/op 872 B/op 5 allocs/op
pkg: github.com/floatdrop/chan/ring
BenchmarkSend/size=1-14 37045423 32.60 ns/op 0 B/op 0 allocs/op
BenchmarkSend/size=64-14 32368641 36.65 ns/op 0 B/op 0 allocs/op
BenchmarkSendFull-14 52008841 22.81 ns/op 0 B/op 0 allocs/op
BenchmarkSendParallel-14 24826450 49.27 ns/op 0 B/op 0 allocs/op
BenchmarkFrom-14 10233439 116.7 ns/op 0 B/op 0 allocs/op
BenchmarkChan-14 39370562 30.04 ns/op 0 B/op 0 allocs/op
Compared with the archived packages on the same machine (median of 6 runs; the archived demux.Dynamic is compared with ByKey):
| Benchmark | Before | After | Speedup |
|---|---|---|---|
batch/New/size=1 |
306 ns | 415 ns | 0.74× |
batch/New/size=16 |
91 ns | 85 ns | 1.08× |
batch/New/size=256 |
58 ns | 41 ns | 1.42× |
batch/NewTimeout/size=1 |
342 ns | 434 ns | 0.79× |
batch/NewTimeout/size=16 |
157 ns | 102 ns | 1.54× |
batch/NewTimeout/size=256 |
95 ns | 43 ns | 2.20× |
batch/NewFunc |
112 ns | 80 ns | 1.40× (128 → 8 B/op) |
demux/ByKey/keys=1 |
95 ns | 130 ns | 0.73× |
demux/ByKey/keys=16 |
166 ns | 200 ns | 0.83× |
demux/ByKey/keys=1024 |
261 ns | 298 ns | 0.87× |
demux/ByKeyChurn |
699 ns | 921 ns | 0.76× (776 → 872 B/op) |
ring/From |
103 ns | 116 ns | 0.89× |
- The old packages could not be canceled. Waiting on a cancelable context costs
demux.ByKeyandring.From11–27%. Withcontext.Background(), which can never be canceled, they use plain channel operations and run at 0.88–1.04× of the old speed (churn aside). batchwithsize=1is slower because its output channel is now unbuffered by default.WithBuffer(1)brings it to about 340 ns.ByKeyChurnreopens a key for nearly every value. The extra time there is what keeps a reopened key's consumers from running at the same time.ring.NewwithSendcosts about as much as a plain buffered channel send (BenchmarkChan), because it involves no goroutine.
MIT, see LICENSE.