mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 04:56:49 +00:00
feat(gateway): add reconnect timestamp to shard state
This commit is contained in:
parent
2fc5f2a9d9
commit
1378379e14
4 changed files with 18 additions and 6 deletions
|
|
@ -6,7 +6,7 @@ use std::sync::Arc;
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
use tracing::{error, info, warn};
|
use tracing::{error, info, warn};
|
||||||
use twilight_gateway::{
|
use twilight_gateway::{
|
||||||
create_iterator, ConfigBuilder, Event, EventTypeFlags, Message, Shard, ShardId,
|
create_iterator, ConfigBuilder, Event, EventTypeFlags, Message, Shard, ShardId, CloseFrame
|
||||||
};
|
};
|
||||||
use twilight_model::gateway::{
|
use twilight_model::gateway::{
|
||||||
payload::outgoing::update_presence::UpdatePresencePayload,
|
payload::outgoing::update_presence::UpdatePresencePayload,
|
||||||
|
|
@ -116,7 +116,11 @@ pub async fn runner(
|
||||||
let raw_event = match item {
|
let raw_event = match item {
|
||||||
Ok(evt) => match evt {
|
Ok(evt) => match evt {
|
||||||
Message::Close(frame) => {
|
Message::Close(frame) => {
|
||||||
|
let mut state_event = ShardStateEvent::Closed;
|
||||||
let close_code = if let Some(close) = frame {
|
let close_code = if let Some(close) = frame {
|
||||||
|
if close == CloseFrame::RESUME {
|
||||||
|
state_event = ShardStateEvent::Reconnect;
|
||||||
|
}
|
||||||
close.code.to_string()
|
close.code.to_string()
|
||||||
} else {
|
} else {
|
||||||
"unknown".to_string()
|
"unknown".to_string()
|
||||||
|
|
@ -132,7 +136,7 @@ pub async fn runner(
|
||||||
.increment(1);
|
.increment(1);
|
||||||
|
|
||||||
if let Err(error) =
|
if let Err(error) =
|
||||||
tx_state.try_send((shard.id(), ShardStateEvent::Closed, None, None))
|
tx_state.try_send((shard.id(), state_event, None, None))
|
||||||
{
|
{
|
||||||
error!("failed to update shard state for socket closure: {error}");
|
error!("failed to update shard state for socket closure: {error}");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ impl ShardStateManager {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn socket_closed(&self, shard_id: u32) -> anyhow::Result<()> {
|
pub async fn socket_closed(&self, shard_id: u32, reconnect: bool) -> anyhow::Result<()> {
|
||||||
gauge!("pluralkit_gateway_shard_up").decrement(1);
|
gauge!("pluralkit_gateway_shard_up").decrement(1);
|
||||||
|
|
||||||
let mut info = self
|
let mut info = self
|
||||||
|
|
@ -97,6 +97,7 @@ impl ShardStateManager {
|
||||||
info.shard_id = shard_id as i32;
|
info.shard_id = shard_id as i32;
|
||||||
info.cluster_id = Some(cluster_config().node_id as i32);
|
info.cluster_id = Some(cluster_config().node_id as i32);
|
||||||
info.up = false;
|
info.up = false;
|
||||||
|
info.last_reconnect = chrono::offset::Utc::now().timestamp() as i32;
|
||||||
info.disconnection_count += 1;
|
info.disconnection_count += 1;
|
||||||
|
|
||||||
self.save_shard(shard_id, info).await?;
|
self.save_shard(shard_id, info).await?;
|
||||||
|
|
|
||||||
|
|
@ -109,8 +109,13 @@ async fn main() -> anyhow::Result<()> {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
ShardStateEvent::Closed => {
|
ShardStateEvent::Closed => {
|
||||||
if let Err(error) = shard_state.socket_closed(shard_id.number()).await {
|
if let Err(error) = shard_state.socket_closed(shard_id.number(), false).await {
|
||||||
error!("failed to update shard state for heartbeat: {error}")
|
error!("failed to update shard state for closed: {error}")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
ShardStateEvent::Reconnect => {
|
||||||
|
if let Err(error) = shard_state.socket_closed(shard_id.number(), true).await {
|
||||||
|
error!("failed to update shard state for reconnect: {error}")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
ShardStateEvent::Other => {
|
ShardStateEvent::Other => {
|
||||||
|
|
@ -121,7 +126,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
error!("failed to update shard state for heartbeat: {error}")
|
error!("failed to update shard state for other evt: {error}")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,13 @@ pub struct ShardState {
|
||||||
/// unix timestamp
|
/// unix timestamp
|
||||||
pub last_heartbeat: i32,
|
pub last_heartbeat: i32,
|
||||||
pub last_connection: i32,
|
pub last_connection: i32,
|
||||||
|
pub last_reconnect: i32,
|
||||||
pub cluster_id: Option<i32>,
|
pub cluster_id: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ShardStateEvent {
|
pub enum ShardStateEvent {
|
||||||
Closed,
|
Closed,
|
||||||
Heartbeat,
|
Heartbeat,
|
||||||
|
Reconnect,
|
||||||
Other,
|
Other,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue