Sharding
A gateway connection is a shard, and Discord sends each shard the events of a share of the bot’s servers. A bot in fewer than 2 500 servers may use one shard; above that Discord requires more, about one per 1 000 servers. djs-rs shards by itself: the question is only where the shards run.
This chapter’s program is
sharded.rs.
It needs the sharding feature of djs-rs:
djs-rs = { version = "0.1.0-alpha.2", features = ["sharding"] }
In one process
A client asks Discord how many shards the bot should have and runs them all, in its one
process: Shards::Auto, the default. Nothing in a bot changes with the shard count. The
events of every shard reach the same handlers, ctx.shard_id() tells which shard an event
came from, and the client keeps Discord’s limit on how fast shards may connect. One
process holds a lot of shards: the repository’s benchmarks run a thousand in one.
Shards::Count(n) fixes the number, and Shards::Range { range, total } runs only some of
them, for a bot split over machines by hand.
(discord.js: the shards: "auto" client option, which is not the default there.)
In several processes
A bot that outgrows one process (its memory, a restart of everything at once) runs its
shards in several. A ClusterManager starts them: one program that, run, either is
the manager or is one of the processes it started, a cluster:
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let token = std::env::var("DISCORD_TOKEN")?;
// The manager starts this same program again for each cluster; `process` tells them
// apart from the first one.
if let Some(assignment) = sharding::process()? {
cluster(token, assignment).await?;
return Ok(());
}
// The first process: the manager.
let manager = ClusterManager::builder(token)
.shards(Shards::Count(2))
.shards_per_process(1)
.build();
let stopping = manager.clone();
tokio::spawn(async move {
if tokio::signal::ctrl_c().await.is_ok() {
stopping.shutdown();
}
});
// Returns once every process has exited.
manager.spawn().await?;
Ok(())
}
sharding::process() returns the process’s ClusterAssignment (its shards) when the
manager started it, and None in the first process, which becomes the manager. The
manager asks Discord for the shard count once, starts the processes one after another
(shards_per_process shards each), passes their connections through one queue so they
never connect too fast, and restarts a process that fails.
(discord.js: ShardingManager, which runs a separate script.)
A cluster
Each cluster is an ordinary client, given its assignment:
/// The bot itself, in each process the manager starts.
async fn cluster(token: String, assignment: ClusterAssignment) -> Result<(), djs_rs::Error> {
let client = Client::builder(token)
.intents(Intents::GUILDS | Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT)
.cluster(assignment)
.build();
let cluster = client.cluster().expect("a client built with `cluster`");
// This process's answer to `ShardIds`.
cluster.handle::<ShardIds, _, _>(|client, ()| async move {
let cluster = client.cluster().expect("a cluster");
Ok(cluster.shards().collect())
});
client.on(Events::MessageCreate, move |ctx, msg| {
let cluster = cluster.clone();
async move {
if msg.content.trim() != "!servers" {
return Ok(());
}
// Every cluster answers, this one included; one that is down fails alone.
let stats = cluster.stats().await?;
let guilds: u64 = stats
.iter()
.filter_map(|reply| reply.result.as_ref().ok()?.guilds)
.sum();
let shards = cluster.broadcast::<ShardIds>(()).await?;
let answering = shards.iter().filter(|reply| reply.result.is_ok()).count();
let answer = format!(
"{guilds} servers over {} shards in {answering} processes; this is shard {}.",
cluster.total_shards(),
ctx.shard_id(),
);
msg.reply(&ctx, answer).await?;
Ok(())
}
});
client.login().await
}
Its cache holds only its own shards’ servers, so a question about all of them goes to
every cluster. cluster.stats() asks each one for its servers, users, shards and memory.
For anything else, a bot declares a command:
/// A command each cluster answers: the IDs of its shards. The name must be the same in
/// every process, which it is: they all run this program.
struct ShardIds;
impl ClusterCommand for ShardIds {
const NAME: &'static str = "shard-ids";
type Input = ();
type Output = Vec<u32>;
}
A command has a name and the types of its input and output, which travel between the
processes as JSON. handle answers it in each cluster, and broadcast sends it to every
cluster (send to one) and returns each one’s answer, or why it gave none: a cluster that
is restarting fails alone instead of spoiling the others.
(discord.js: broadcastEval and fetchClientValues. Rust cannot run code sent to it at run
time, so the code a command runs is compiled into the bot.)
Limits worth knowing
- Discord limits how many sessions a bot may start per day (1 000 for most bots); every shard’s connection is one. The manager checks that enough are left before it starts.
- Each process has its own REST client, and Discord’s rate limits are per bot: processes that send a lot at once see 429 answers, which the client waits out.
- The processes talk to the manager over
127.0.0.1, with a secret token only the processes it started know.
What’s next
The next chapter joins voice channels.