|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025, by Samuel Williams. |
| 5 | + |
| 6 | +require_relative "subscribe" |
| 7 | + |
| 8 | +module Async |
| 9 | + module Redis |
| 10 | + module Context |
| 11 | + # Context for managing sharded subscriptions across multiple Redis cluster nodes. |
| 12 | + # This class handles the complexity of subscribing to channels that may be distributed |
| 13 | + # across different shards in a Redis cluster. |
| 14 | + class ShardSubscribe |
| 15 | + # Initialize a new shard subscription context. |
| 16 | + # @parameter cluster_client [ClusterClient] The cluster client to use. |
| 17 | + def initialize(cluster_client) |
| 18 | + @cluster_client = cluster_client |
| 19 | + @subscriptions = {} |
| 20 | + @channels = [] |
| 21 | + end |
| 22 | + |
| 23 | + # Close all shard subscriptions. |
| 24 | + def close |
| 25 | + @subscriptions.each_value(&:close) |
| 26 | + @subscriptions.clear |
| 27 | + end |
| 28 | + |
| 29 | + # Listen for the next message from any subscribed shard. |
| 30 | + # This uses a simple round-robin approach to check each shard. |
| 31 | + # @returns [Array] The next message response, or nil if all connections closed. |
| 32 | + def listen |
| 33 | + return nil if @subscriptions.empty? |
| 34 | + |
| 35 | + # Simple round-robin checking of subscriptions |
| 36 | + @subscriptions.each_value do |subscription| |
| 37 | + # Non-blocking check for messages |
| 38 | + begin |
| 39 | + if response = subscription.listen |
| 40 | + return response |
| 41 | + end |
| 42 | + rescue => error |
| 43 | + # Handle connection errors gracefully |
| 44 | + Console.warn(self, "Error reading from shard subscription: #{error}") |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + # If no immediate messages, do a blocking wait on the first subscription |
| 49 | + if first_subscription = @subscriptions.values.first |
| 50 | + first_subscription.listen |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + # Iterate over all messages from all subscribed shards. |
| 55 | + # @yields {|response| ...} Block called for each message. |
| 56 | + # @parameter response [Array] The message response. |
| 57 | + def each |
| 58 | + return to_enum unless block_given? |
| 59 | + |
| 60 | + while response = self.listen |
| 61 | + yield response |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + # Subscribe to additional sharded channels. |
| 66 | + # @parameter channels [Array(String)] The channels to subscribe to. |
| 67 | + def subscribe(channels) |
| 68 | + slots = @cluster_client.slots_for(channels) |
| 69 | + |
| 70 | + slots.each do |slot, channels_for_slot| |
| 71 | + if subscription = @subscriptions[slot] |
| 72 | + # Add to existing subscription for this shard |
| 73 | + subscription.ssubscribe(channels_for_slot) |
| 74 | + else |
| 75 | + # Create new subscription for this shard |
| 76 | + client = @cluster_client.client_for(slot) |
| 77 | + @subscriptions[slot] = client.ssubscribe(*channels_for_slot) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + @channels.concat(channels) |
| 82 | + end |
| 83 | + |
| 84 | + # Unsubscribe from sharded channels. |
| 85 | + # @parameter channels [Array(String)] The channels to unsubscribe from. |
| 86 | + def unsubscribe(channels) |
| 87 | + slots = @cluster_client.slots_for(channels) |
| 88 | + |
| 89 | + slots.each do |slot, channels_for_slot| |
| 90 | + if subscription = @subscriptions[slot] |
| 91 | + subscription.sunsubscribe(channels_for_slot) |
| 92 | + |
| 93 | + # Remove channels from our tracking |
| 94 | + @channels -= channels_for_slot |
| 95 | + |
| 96 | + # Check if this shard still has channels |
| 97 | + remaining_channels_for_slot = @channels.select { |ch| @cluster_client.slot_for(ch) == slot } |
| 98 | + |
| 99 | + # If no channels left for this shard, close and remove it |
| 100 | + if remaining_channels_for_slot.empty? |
| 101 | + subscription.close |
| 102 | + @subscriptions.delete(slot) |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + # Get the list of currently subscribed channels. |
| 109 | + # @returns [Array(String)] The list of subscribed channels. |
| 110 | + def channels |
| 111 | + @channels.dup |
| 112 | + end |
| 113 | + |
| 114 | + # Get the number of active shard subscriptions. |
| 115 | + # @returns [Integer] The number of shard connections. |
| 116 | + def shard_count |
| 117 | + @subscriptions.size |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments