pub struct ShardId { /* private fields */ }
Expand description
Shard identifier to calculate if it receivies a given event.
A shard ID consist of two fields: number
and total
. These values do not
need to be unique, and are used by Discord for calculating which events to
send to which shard. Shards should in general share the same total
value
and have an unique number
value, but users may deviate from this when
resharding/migrating to a new set of shards.
§Advanced use
Incoming events are split by their originating guild and are received by the shard with the id calculated from the following formula:
number = (guild_id >> 22) % total
.
total
is in other words unrelated to the total number of shards and is
only used to specify the share of events a shard will receive. The formula
is independently calculated for all shards, which means that events may be
duplicated or lost if it’s determined that an event should be sent to
multiple or no shard.
It may be helpful to visualize the logic in code:
for shard in shards {
if shard.id().number() == (guild_id >> 22) % shard.id().total() {
unimplemented!("send event to shard");
}
}
Implementations§
Source§impl ShardId
impl ShardId
Sourcepub const ONE: ShardId
pub const ONE: ShardId
ID of a bot that has only one shard.
Should only be used by small bots in under one or two thousand guilds.
Sourcepub const fn new(number: u32, total: u32) -> Self
pub const fn new(number: u32, total: u32) -> Self
Create a new shard identifier.
The shard number is 0-indexed while the total number of shards is 1-indexed. A shard number of 7 with a total of 8 is therefore valid, whilst a shard number of 8 out of 8 total shards is invalid.
§Examples
Create a new shard with a shard number of 13 out of a total of 24 shards:
use twilight_model::gateway::ShardId;
let id = ShardId::new(13, 24);
§Panics
Panics if the shard number is greater than or equal to the total number of shards.
Sourcepub const fn new_checked(number: u32, total: u32) -> Option<Self>
pub const fn new_checked(number: u32, total: u32) -> Option<Self>
Create a new shard identifier if the shard indexes are valid.