Skip to main content

IdentifyQueue

Trait IdentifyQueue 

Source
pub trait IdentifyQueue:
    Debug
    + Send
    + Sync {
    // Required method
    fn wait_turn(
        &self,
        shard: ShardId,
    ) -> Pin<Box<dyn Future<Output = ()> + Send>>;
}
Expand description

Decides when a shard may send its IDENTIFY payload.

Discord lets a bot start max_concurrency sessions every 5 seconds: shard n belongs to the rate-limit bucket n % max_concurrency, and each bucket accepts one IDENTIFY per 5 seconds. Identifying too fast gets the connection closed. Every Shard waits for its turn here before identifying — on the first connection and whenever its session has to be started again (resuming does not identify).

LocalIdentifyQueue coordinates the shards of one process and is the default. Implement this trait to coordinate several processes, for example through a shared store.

discord.js counterpart: IIdentifyThrottler from @discordjs/ws.

§Examples

A queue that lets every shard identify at once — only correct if something else (here, a proxy in front of the gateway) does the throttling:

use core::{future::Future, pin::Pin};

use djs_gateway::IdentifyQueue;
use djs_model::gateway::ShardId;

#[derive(Debug)]
struct ProxyThrottles;

impl IdentifyQueue for ProxyThrottles {
    fn wait_turn(&self, _shard: ShardId) -> Pin<Box<dyn Future<Output = ()> + Send>> {
        Box::pin(async {})
    }
}

Required Methods§

Source

fn wait_turn(&self, shard: ShardId) -> Pin<Box<dyn Future<Output = ()> + Send>>

Returns a future that completes when shard may identify.

The shard sends IDENTIFY right after the future completes, so an implementation should count the identify as made at that moment. The future is dropped without completing if the shard’s connection closes while it waits; the shard then leaves the queue and asks again on its next connection.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§