EDA.Gateway.ShardManager (EDA v0.3.0)

Copy Markdown View Source

Orchestrates the lifecycle of all gateway shards.

Inspired by JDA's DefaultShardManager but built on OTP primitives:

  • Fetches /gateway/bot for recommended shard count and max_concurrency
  • Launches shards via DynamicSupervisor with staggered timing
  • Monitors shards and auto-reconnects with exponential backoff + jitter
  • Stores total_shards in :persistent_term for O(1) guild→shard routing

Configuration

config :eda, shards: :auto           # use Discord's recommended count (default)
config :eda, shards: 4               # fixed 4 shards (0..3)
config :eda, shards: {0..1, 4}       # this node handles shards 0,1 out of 4 total

Summary

Functions

Returns a specification to start this module under a supervisor.

Forces a shard to reconnect.

Returns the number of launched shards.

Computes which shard owns a guild. Pure function — no GenServer call.

Returns the status of a shard.

Returns a map with shard statuses and session limit info.

Returns the total number of shards.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

reconnect(shard_id)

@spec reconnect(non_neg_integer()) :: :ok

Forces a shard to reconnect.

shard_count()

@spec shard_count() :: non_neg_integer()

Returns the number of launched shards.

shard_for_guild(guild_id)

@spec shard_for_guild(integer() | String.t()) :: non_neg_integer()

Computes which shard owns a guild. Pure function — no GenServer call.

Uses :persistent_term for O(1) access to total_shards.

shard_status(shard_id)

@spec shard_status(non_neg_integer()) :: :launching | :connected | :disconnected | nil

Returns the status of a shard.

start_link(opts)

status()

@spec status() :: map()

Returns a map with shard statuses and session limit info.

total_shards()

@spec total_shards() :: non_neg_integer()

Returns the total number of shards.