Files
lo/docs/data/core-dispatchingstrategy.md
T
2025-10-06 17:15:49 +02:00

2.5 KiB

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
DispatchingStrategy dispatchingstrategy channel.go#L78 core channel
func DispatchingStrategyRoundRobin[T any](msg T, index uint64, channels []<-chan T) int
func DispatchingStrategyRandom[T any](msg T, index uint64, channels []<-chan T) int
func DispatchingStrategyWeightedRandom[T any](weights []int) DispatchingStrategy[T]
func DispatchingStrategyFirst[T any](msg T, index uint64, channels []<-chan T) int
func DispatchingStrategyLeast[T any](msg T, index uint64, channels []<-chan T) int
func DispatchingStrategyMost[T any](msg T, index uint64, channels []<-chan T) int
core#channel#dispatchingstrategyroundrobin
core#channel#dispatchingstrategyrandom
core#channel#dispatchingstrategyweightedrandom
core#channel#dispatchingstrategyfirst
core#channel#dispatchingstrategyleast
core#channel#dispatchingstrategymost
core#channel#channeldispatcher
270

DispatchingStrategyRoundRobin distributes messages to channels in round-robin order.

strategy := lo.DispatchingStrategyRoundRobin[int]
index := strategy(42, 0, []chan int{ch1, ch2, ch3})
// Returns 0, then 1, then 2, then 0, cycling through channels

DispatchingStrategyRandom distributes messages to a random channel.

strategy := lo.DispatchingStrategyRandom[int]
index := strategy(42, 0, []chan int{ch1, ch2, ch3})
// Returns a random channel index 0, 1, or 2

DispatchingStrategyWeightedRandom distributes messages to channels based on weights.

weights := []int{1, 3, 6} // Channel 0: 10%, Channel 1: 30%, Channel 2: 60%
strategy := lo.DispatchingStrategyWeightedRandom[int](weights)
index := strategy(42, 0, []chan int{ch1, ch2, ch3})
// Returns 2 most often, 1 sometimes, 0 rarely

DispatchingStrategyFirst distributes messages to the first non-full channel.

strategy := lo.DispatchingStrategyFirst[int]
index := strategy(42, 0, []chan int{ch1, ch2, ch3})
// Always returns 0 if ch1 is not full

DispatchingStrategyLeast distributes messages to the channel with the fewest items.

strategy := lo.DispatchingStrategyLeast[int]
index := strategy(42, 0, []chan int{ch1, ch2, ch3})
// Returns the index of the channel with the smallest buffer size

DispatchingStrategyMost distributes messages to the channel with the most items.

strategy := lo.DispatchingStrategyMost[int]
index := strategy(42, 0, []chan int{ch1, ch2, ch3})
// Returns the index of the channel with the largest buffer size