Class: DiscordRDA::ReshardManager
- Inherits:
-
Object
- Object
- DiscordRDA::ReshardManager
- Defined in:
- lib/discord_rda/connection/reshard_manager.rb
Overview
Zero-downtime resharding manager. Allows adding/removing shards without stopping the bot.
Instance Attribute Summary collapse
-
#bot ⇒ Bot
readonly
Bot instance.
-
#logger ⇒ Logger
readonly
Logger instance.
-
#new_shards ⇒ Array<GatewayClient>
readonly
New shards being added.
-
#old_shards ⇒ Array<GatewayClient>
readonly
Old shards being removed.
-
#resharding ⇒ Boolean
readonly
Whether resharding is in progress.
-
#shard_manager ⇒ ShardManager
readonly
Original shard manager.
Instance Method Summary collapse
-
#calculate_guilds_per_shard(shard_count) ⇒ Integer
Calculate optimal guilds per shard.
-
#initialize(bot, shard_manager, logger) ⇒ ReshardManager
constructor
Initialize reshard manager.
-
#reshard_to(new_shard_count) ⇒ void
Start zero-downtime resharding to new shard count.
-
#status ⇒ Hash
Get reshard status.
Constructor Details
#initialize(bot, shard_manager, logger) ⇒ ReshardManager
Initialize reshard manager
30 31 32 33 34 35 36 37 38 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 30 def initialize(bot, shard_manager, logger) @bot = bot @shard_manager = shard_manager @logger = logger @resharding = false @new_shards = [] @old_shards = [] @mutex = Mutex.new end |
Instance Attribute Details
#bot ⇒ Bot (readonly)
Returns Bot instance.
9 10 11 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 9 def bot @bot end |
#logger ⇒ Logger (readonly)
Returns Logger instance.
15 16 17 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 15 def logger @logger end |
#new_shards ⇒ Array<GatewayClient> (readonly)
Returns New shards being added.
21 22 23 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 21 def new_shards @new_shards end |
#old_shards ⇒ Array<GatewayClient> (readonly)
Returns Old shards being removed.
24 25 26 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 24 def old_shards @old_shards end |
#resharding ⇒ Boolean (readonly)
Returns Whether resharding is in progress.
18 19 20 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 18 def resharding @resharding end |
#shard_manager ⇒ ShardManager (readonly)
Returns Original shard manager.
12 13 14 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 12 def shard_manager @shard_manager end |
Instance Method Details
#calculate_guilds_per_shard(shard_count) ⇒ Integer
Calculate optimal guilds per shard
89 90 91 92 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 89 def calculate_guilds_per_shard(shard_count) total_guilds = @shard_manager.total_guilds || 1000 (total_guilds / shard_count.to_f).ceil end |
#reshard_to(new_shard_count) ⇒ void
This method returns an undefined value.
Start zero-downtime resharding to new shard count
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 43 def reshard_to(new_shard_count) return if new_shard_count <= @shard_manager.shard_count.to_i return if @resharding @mutex.synchronize do @resharding = true old_count = @shard_manager.shard_count || 1 @logger.info('Starting zero-downtime resharding', old_count: old_count, new_count: new_shard_count ) # Step 1: Identify which shards need to be moved guilds_per_shard = calculate_guilds_per_shard(new_shard_count) # Step 2: Start new shards start_new_shards(old_count, new_shard_count) # Step 3: Wait for new shards to be ready wait_for_new_shards_ready # Step 4: Begin session transfer transfer_sessions # Step 5: Mark old shards for shutdown mark_old_shards_for_shutdown # Step 6: Wait for old shards to drain wait_for_old_shards_drain # Step 7: Update shard manager state finalize_reshard @logger.info('Resharding complete', new_shard_count: new_shard_count) @resharding = false end rescue => e @logger.error('Resharding failed', error: e) @resharding = false raise end |
#status ⇒ Hash
Get reshard status
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/discord_rda/connection/reshard_manager.rb', line 96 def status @mutex.synchronize do { resharding: @resharding, new_shards: @new_shards.length, old_shards: @old_shards.length, total_shards: @shard_manager.shard_count } end end |