Struct twilight_gateway::Session

source ·
pub struct Session { /* private fields */ }
Expand description

Gateway session information for a shard’s active connection.

A session is a stateful identifier on Discord’s end for running a shard. It is used for maintaining an authenticated Websocket connection based on an identifier. While a session is only connected to one shard, one shard can have more than one session: if a shard shuts down its connection and starts a new session, then the previous session will be kept alive for a short time.

§Reusing sessions

Sessions are able to be reused across connections to Discord. If an application’s process needs to be restarted, then this session information—which can be (de)serialized via serde—can be stored, the application restarted, and then used again via ConfigBuilder::session.

If the delay between disconnecting from the gateway and reconnecting isn’t too long and Discord hasn’t invalidated the session, then the session will be reused by Discord. As a result, any events that were “missed” while restarting and reconnecting will be played back, meaning the application won’t have missed any events. If the delay has been too long, then a new session will be initialized, resulting in those events being missed.

Implementations§

source§

impl Session

source

pub fn new(sequence: u64, session_id: String) -> Self

Create new configuration for resuming a gateway session.

Can be provided to ConfigBuilder::session.

source

pub const fn id(&self) -> &str

ID of the session being resumed.

The ID of the session is different from the ID of the shard; shards are identified by an index, and when authenticated with the gateway the shard is given a unique identifier for the gateway session.

Session IDs are obtained by shards via sending an Identify command with the shard’s authentication details, and in return the session ID is provided via the Ready event.

source

pub const fn sequence(&self) -> u64

Current sequence of the connection.

Number of the events that have been received during this session. A larger number typically correlates that the shard has been connected with this session for a longer time, while a smaller number typically correlates to meaning that it’s been connected with this session for a shorter duration of time.

As a shard is connected to the gateway and receives events this sequence will be updated in real time when obtaining the session of a shard.

Trait Implementations§

source§

impl Clone for Session

source§

fn clone(&self) -> Session

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Session

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Session

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl PartialEq for Session

source§

fn eq(&self, other: &Session) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Session

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for Session

source§

impl StructuralPartialEq for Session

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,