Path: redb-route/redb.Route/Processors/LoadBalancer/StickyStrategy.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Processors/LoadBalancer/StickyStrategy.cs ---- using System; using System.Collections.Generic; using redb.Route.Abstractions; namespace redb.Route.Processors.LoadBalancer; /// /// Sticky load balancer strategy. /// Routes exchanges with the same key to the same endpoint using hash-based selection. /// Falls back to round-robin when the key extractor returns null/empty. /// Thread-safe. /// public sealed class StickyStrategy : ILoadBalancerStrategy { private readonly Func _keyExtractor; private readonly RoundRobinStrategy _fallback = new(); /// /// Creates a sticky strategy with the given key extractor. /// /// /// Extracts a routing key from the exchange (e.g., tenant ID, correlation ID). /// When the result is null or empty, falls back to round-robin. /// public StickyStrategy(Func keyExtractor) { _keyExtractor = keyExtractor ?? throw new ArgumentNullException(nameof(keyExtractor)); } /// public string Select(IExchange exchange, IReadOnlyList endpoints) { if (endpoints.Count == 0) throw new InvalidOperationException("No endpoints configured for load balancer."); var key = _keyExtractor(exchange); if (string.IsNullOrEmpty(key)) return _fallback.Select(exchange, endpoints); // Stable hash → deterministic endpoint selection var hash = key.GetHashCode(StringComparison.Ordinal); var idx = ((hash % endpoints.Count) + endpoints.Count) % endpoints.Count; return endpoints[idx]; } }