-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rewrite presence system to use sets
- Loading branch information
Showing
8 changed files
with
91 additions
and
156 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "revolt-delta" | ||
version = "0.5.17" | ||
version = "0.5.18" | ||
license = "AGPL-3.0-or-later" | ||
authors = ["Paul Makles <[email protected]>"] | ||
edition = "2018" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,12 @@ | ||
use std::env; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use once_cell::sync::Lazy; | ||
|
||
pub static REGION_ID: Lazy<u16> = Lazy::new(|| env::var("REGION_ID") | ||
.unwrap_or_else(|_| "0".to_string()) | ||
.parse() | ||
.unwrap()); | ||
pub static REGION_ID: Lazy<u16> = Lazy::new(|| { | ||
env::var("REGION_ID") | ||
.unwrap_or_else(|_| "0".to_string()) | ||
.parse() | ||
.unwrap() | ||
}); | ||
|
||
pub static REGION_KEY: Lazy<String> = Lazy::new(|| format!("region{}", &*REGION_ID)); | ||
|
||
/// Compact presence information for a user | ||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct PresenceEntry { | ||
/// Region this session exists in | ||
/// | ||
/// We can have up to 65535 regions | ||
pub region_id: u16, | ||
|
||
/// Unique session ID | ||
pub session_id: u8, | ||
|
||
/// Known flags about session | ||
pub flags: u8, | ||
} | ||
|
||
impl PresenceEntry { | ||
/// Create a new presence entry from a given session ID and known flags | ||
pub fn from(session_id: u8, flags: u8) -> Self { | ||
Self { | ||
region_id: *REGION_ID, | ||
session_id, | ||
flags, | ||
} | ||
} | ||
} | ||
|
||
pub trait PresenceOp { | ||
/// Find next available session ID | ||
fn find_next_id(&self) -> u8; | ||
} | ||
|
||
impl PresenceOp for Vec<PresenceEntry> { | ||
fn find_next_id(&self) -> u8 { | ||
// O(n^2) scan algorithm | ||
// should be relatively fast at low numbers anyways | ||
for i in 0..255 { | ||
let mut found = false; | ||
for entry in self { | ||
if entry.session_id == i { | ||
found = true; | ||
break; | ||
} | ||
} | ||
|
||
if !found { | ||
return i; | ||
} | ||
} | ||
|
||
255 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,42 @@ | ||
use redis_kiss::{AsyncCommands, Conn}; | ||
|
||
use super::entry::PresenceEntry; | ||
|
||
/// Set presence entry by given ID | ||
pub async fn __set_key_presence_entry(conn: &mut Conn, id: &str, data: Vec<PresenceEntry>) { | ||
let _: Option<()> = conn.set(id, bincode::serialize(&data).unwrap()).await.ok(); | ||
/// Add to set (string) | ||
pub async fn __add_to_set_string(conn: &mut Conn, key: &str, value: &str) { | ||
let _: Option<()> = conn.sadd(key, value).await.ok(); | ||
} | ||
|
||
/// Delete presence entry by given ID | ||
pub async fn __delete_key_presence_entry(conn: &mut Conn, id: &str) { | ||
let _: Option<()> = conn.del(id).await.ok(); | ||
/// Add to set (u32) | ||
pub async fn __add_to_set_u32(conn: &mut Conn, key: &str, value: u32) { | ||
let _: Option<()> = conn.sadd(key, value).await.ok(); | ||
} | ||
|
||
/// Get presence entry by given ID | ||
pub async fn __get_key_presence_entry(conn: &mut Conn, id: &str) -> Option<Vec<PresenceEntry>> { | ||
conn.get::<_, Option<Vec<u8>>>(id) | ||
.await | ||
.unwrap() | ||
.map(|entry| bincode::deserialize(&entry[..]).unwrap()) | ||
/// Remove from set (string) | ||
pub async fn __remove_from_set_string(conn: &mut Conn, key: &str, value: &str) { | ||
let _: Option<()> = conn.srem(key, value).await.ok(); | ||
} | ||
|
||
/// Add to region session set | ||
pub async fn __add_to_set_sessions( | ||
conn: &mut Conn, | ||
region_id: &str, | ||
user_id: &str, | ||
session_id: u8, | ||
) { | ||
let _: Option<()> = conn | ||
.sadd(region_id, format!("{user_id}:{session_id}")) | ||
.await | ||
.ok(); | ||
/// Remove from set (u32) | ||
pub async fn __remove_from_set_u32(conn: &mut Conn, key: &str, value: u32) { | ||
let _: Option<()> = conn.srem(key, value).await.ok(); | ||
} | ||
|
||
/// Remove from region session set | ||
pub async fn __remove_from_set_sessions( | ||
conn: &mut Conn, | ||
region_id: &str, | ||
user_id: &str, | ||
session_id: u8, | ||
) { | ||
let _: Option<()> = conn | ||
.srem(region_id, format!("{user_id}:{session_id}")) | ||
/// Get set members as string | ||
pub async fn __get_set_members_as_string(conn: &mut Conn, key: &str) -> Vec<String> { | ||
conn.smembers::<_, Vec<String>>(key) | ||
.await | ||
.ok(); | ||
.expect("could not get set members as string") | ||
} | ||
|
||
/// Get region session set as list | ||
pub async fn __get_set_sessions(conn: &mut Conn, region_id: &str) -> Vec<String> { | ||
conn.smembers::<_, Vec<String>>(region_id).await.unwrap() | ||
/// Get set size | ||
pub async fn __get_set_size(conn: &mut Conn, id: &str) -> u32 { | ||
conn.scard::<_, u32>(id) | ||
.await | ||
.expect("could not get set size") | ||
} | ||
|
||
/// Delete region session set | ||
pub async fn __delete_set_sessions(conn: &mut Conn, region_id: &str) { | ||
conn.del::<_, ()>(region_id).await.unwrap(); | ||
/// Delete key by id | ||
pub async fn __delete_key(conn: &mut Conn, id: &str) { | ||
conn.del::<_, ()>(id) | ||
.await | ||
.expect("could not delete key by id"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters