Documentation
¶
Overview ¶
Package demux splits the values received from one channel by key.
ByKey starts a consumer for each key it sees and gives it the values with that key, in order. Consumers of different keys run concurrently; values of one key are never processed concurrently.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ByKey ¶
func ByKey[T any, K comparable](ctx context.Context, in <-chan T, key func(T) K, consume func(K, <-chan T), opts ...Option) error
ByKey routes values received from in to consumers it starts for each key.
The first value with a new key starts a goroutine running consume(key, ch). Channel ch delivers that value and every later value with the same key, in the order they were received. A key's consumers never run at the same time: when a key closed by WithMaxKeys comes back, its new consumer starts only after the previous one has returned.
A consumer may return before ch is closed, for example after an error. The values of its key that are left, or still to come, are then discarded until the key is closed, so the other keys keep flowing.
ByKey runs in the caller's goroutine until in is closed or ctx is done. It then closes every channel, waits for every consumer to return, and returns ctx.Err() if ctx ended it, or nil otherwise. Values already buffered in a channel are still delivered.
Values are sent one at a time, so a consumer that falls behind holds up the others. WithBuffer gives each consumer room to absorb short delays.
Example ¶
package main
import (
"context"
"fmt"
"maps"
"slices"
"sync"
"github.com/floatdrop/chan/demux"
)
func main() {
type order struct{ customer, item string }
in := make(chan order, 5)
for _, o := range []order{
{"bob", "tea"}, {"alice", "cake"}, {"bob", "milk"}, {"carol", "tea"}, {"alice", "jam"},
} {
in <- o
}
close(in)
var (
mu sync.Mutex
items = make(map[string][]string)
)
customer := func(o order) string { return o.customer }
// Each customer's orders are handled in their own goroutine, in order.
handle := func(name string, orders <-chan order) {
var got []string
for o := range orders {
got = append(got, o.item)
}
mu.Lock()
items[name] = got
mu.Unlock()
}
if err := demux.ByKey(context.Background(), in, customer, handle, demux.WithBuffer(8)); err != nil {
panic(err)
}
// ByKey has waited for every handler to return.
for _, name := range slices.Sorted(maps.Keys(items)) {
fmt.Println(name, items[name])
}
}
Output: alice [cake jam] bob [tea milk] carol [tea]
Types ¶
type Option ¶
type Option func(*config)
Option configures ByKey.
func WithBuffer ¶
WithBuffer sets the capacity of each key's channel. The default is 0: the channels are unbuffered. WithBuffer panics if n is negative.
func WithMaxKeys ¶
WithMaxKeys limits ByKey to n open keys. When a value with a new key arrives and n keys are open, the least recently used key is closed: its channel is closed, so its consumer can finish. If that key comes back, a new consumer is started for it.
The default, 0, means no limit. WithMaxKeys panics if n is negative.