Skip to content

Commit

Permalink
Resume on resumable session invalidations
Browse files Browse the repository at this point in the history
Session invalidations include whether the session may be resumable.
Previously, this was not parsed by serenity. This data is now contained
in `GatewayEvent::InvalidateSession` as a boolean, which constitutes a
breaking change for users that manually handle gateway events.

Upgrade path:

Instead of pattern matching on simply the variant like so:

```rust
use serenity::model::event::GatewayEvent;

match gateway_event {
    GatewayEvent::InvalidateSession => {
        // work here
    },
    // other matching arms here
    _ => {},
}
```

Instead pattern match it as a single field tuple-struct:

```rust
use serenity::model::event::GatewayEvent;

match gateway_event {
    GatewayEvent::InvalidateSession(resumable) => {
        // work here
    },
    // other matching arms here
    _ => {},
}
```
  • Loading branch information
Zeyla Hellyer committed Oct 10, 2017
1 parent 1735e57 commit eb9e8df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/gateway/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,17 @@ impl Shard {
self.autoreconnect().and(Ok(None))
}
},
Ok(GatewayEvent::InvalidateSession) => {
Ok(GatewayEvent::InvalidateSession(resumable)) => {
info!(
"[Shard {:?}] Received session invalidation; re-identifying",
self.shard_info
"[Shard {:?}] Received session invalidation",
self.shard_info,
);

self.seq = 0;
self.session_id = None;

self.identify().and(Ok(None))
if resumable {
self.resume().and(Ok(None))
} else {
self.identify().and(Ok(None))
}
},
Ok(GatewayEvent::Reconnect) => self.reconnect().and(Ok(None)),
Err(Error::Gateway(GatewayError::Closed(data))) => {
Expand Down Expand Up @@ -981,6 +982,7 @@ impl Shard {
self.heartbeat_instants = (Some(Instant::now()), None);
self.heartbeat_interval = None;
self.last_heartbeat_acknowledged = true;
self.session_id = None;
self.stage = ConnectionStage::Disconnected;
self.seq = 0;
}
Expand Down
13 changes: 11 additions & 2 deletions src/model/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,8 @@ pub enum GatewayEvent {
Dispatch(u64, Event),
Heartbeat(u64),
Reconnect,
InvalidateSession,
/// Whether the session can be resumed.
InvalidateSession(bool),
Hello(u64),
HeartbeatAck,
}
Expand Down Expand Up @@ -1096,7 +1097,15 @@ impl GatewayEvent {
GatewayEvent::Heartbeat(s)
},
OpCode::Reconnect => GatewayEvent::Reconnect,
OpCode::InvalidSession => GatewayEvent::InvalidateSession,
OpCode::InvalidSession => {
let resumable = map.remove("d")
.ok_or_else(|| {
DeError::custom("expected gateway invalid session d")
})
.and_then(bool::deserialize)?;

GatewayEvent::InvalidateSession(resumable)
},
OpCode::Hello => {
let mut d = map.remove("d")
.ok_or_else(|| DeError::custom("expected gateway hello d"))
Expand Down

0 comments on commit eb9e8df

Please sign in to comment.