Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
improve internal code of models
Browse files Browse the repository at this point in the history
this should hopefully fix the silent deadlocking
  • Loading branch information
katsumi143 committed Jun 17, 2024
1 parent 9b2fbd4 commit 14f61c1
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 84 deletions.
370 changes: 339 additions & 31 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions crates/mellow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ rust-version.workspace = true

[dependencies]
hex = "0.4.3"
url = "2.5.0"
url = "2.5.1"
hmac = "0.12.1"
sha2 = "0.10.8"
uuid = { version = "1.8.0", features = ["serde"] }
moka = { version = "0.12.7", features = ["future"] }
sqlx = { version = "0.7.4", features = ["uuid", "json", "chrono", "macros", "postgres", "tls-native-tls", "rust_decimal", "runtime-tokio"] }
tokio = { version = "1.37.0", features = ["full"] }
tokio = { version = "1.38.0", features = ["full"] }
serde = { version = "1.0.203", features = ["derive"] }
chrono = { version = "0.4.38", features = ["serde"] }
tracing = "0.1.40"
futures = "0.3.30"
dashmap = { version = "5.5.3", features = ["inline"] }
reqwest = { version = "0.12.4", features = ["json"] }
reqwest = { version = "0.12.5", features = ["json"] }
mimalloc = "0.1.42"
actix-web = "4.6.0"
actix-web = "4.7.0"
once_cell = "1.19.0"
thiserror = "1.0.61"
simd-json = "0.13.10"
Expand All @@ -39,7 +39,7 @@ serde_repr = "0.1.19"
serde_json = "1.0.117"
tracing-log = "0.2.0"
signal-hook = "0.3.17"
derive_more = "0.99.17"
derive_more = "0.99.18"
futures-util = "0.3.30"
tracing-error = "0.2.0"
mellow_macros = { path = "../mellow_macros" }
Expand Down
3 changes: 3 additions & 0 deletions crates/mellow/src/discord/gateway/event/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub fn guild_create(guild_create: GuildCreate) -> Result<()> {
let guild_id = guild_create.id;
tracing::info!("model.discord.guild.create (guild_id={guild_id})");

for role in &guild_create.roles {
DISCORD_MODELS.roles.insert((guild_id, role.id), role.clone().into());
}
DISCORD_MODELS.guilds.insert(guild_id, guild_create.0.into());
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions crates/mellow/src/model/discord/guild/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use twilight_model::{
id::{
marker::{ UserMarker, GuildMarker, ChannelMarker },
marker::{ UserMarker, ChannelMarker },
Id
},
util::ImageHash,
Expand Down Expand Up @@ -39,7 +39,7 @@ pub struct CachedGuild {
//pub explicit_content_filter: ExplicitContentFilter,
pub features: Vec<GuildFeature>,
pub icon: Option<ImageHash>,
pub id: Id<GuildMarker>,
//pub id: Id<GuildMarker>,
//pub joined_at: Option<Timestamp>,
//pub large: bool,
pub max_members: Option<u64>,
Expand Down Expand Up @@ -116,7 +116,7 @@ impl From<Guild> for CachedGuild {
//explicit_content_filter,
features,
icon,
id,
//id,
//joined_at,
//large,
max_members,
Expand Down Expand Up @@ -168,7 +168,7 @@ impl From<Guild> for CachedGuild {
//explicit_content_filter,
features,
icon,
id,
//id,
//joined_at,
//large,
max_members,
Expand Down
41 changes: 19 additions & 22 deletions crates/mellow/src/model/discord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ impl DiscordModels {
self.members.insert((guild_id, member.user.id), member.clone().into());
}
self.guild_members.insert(guild_id, new_item.members.iter().map(|x| x.user.id).collect());
self.guilds.insert(guild_id, new_item.into());
self.guilds.get(&guild_id).unwrap()
self.guilds
.entry(guild_id)
.insert(new_item.into())
.downgrade()
})
}

Expand All @@ -59,25 +61,16 @@ impl DiscordModels {
let new_item = CLIENT.guild_onboarding(guild_id).await?.model().await?;
tracing::info!("guild_onboarding.write (guild_id={guild_id})");

self.guild_onboardings.insert(guild_id, new_item.into());
self.guild_onboardings.get(&guild_id).unwrap()
self.guild_onboardings
.entry(guild_id)
.insert(new_item.into())
.downgrade()
})
}

pub async fn role(&self, guild_id: Id<GuildMarker>, role_id: Id<RoleMarker>) -> Result<Ref<'_, (Id<GuildMarker>, Id<RoleMarker>), CachedRole>> {
let key = (guild_id, role_id);
Ok(if let Some(item) = self.roles.get(&key) {
tracing::info!("roles.read (guild_id={guild_id}) (role_id={role_id})");
item
} else {
let roles = CLIENT.roles(guild_id).await?.model().await?;
tracing::info!("roles.write (guild_id={guild_id}) (role_id={role_id})");

for item in roles {
self.roles.insert((guild_id, item.id), item.into());
}
self.roles.get(&key).unwrap()
})
#[allow(clippy::type_complexity)]
pub fn role(&self, guild_id: Id<GuildMarker>, role_id: Id<RoleMarker>) -> Option<Ref<'_, (Id<GuildMarker>, Id<RoleMarker>), CachedRole>> {
self.roles.get(&(guild_id, role_id))
}

pub async fn member(&self, guild_id: Id<GuildMarker>, user_id: Id<UserMarker>) -> Result<Ref<'_, (Id<GuildMarker>, Id<UserMarker>), CachedMember>> {
Expand All @@ -89,8 +82,10 @@ impl DiscordModels {
let new_item = CLIENT.guild_member(guild_id, user_id).await?.model().await?;
tracing::info!("members.write (guild_id={guild_id}) (user_id={user_id})");

self.members.insert(key, new_item.into());
self.members.get(&key).unwrap()
self.members
.entry(key)
.insert(new_item.into())
.downgrade()
})
}

Expand All @@ -102,8 +97,10 @@ impl DiscordModels {
let new_item = CLIENT.user(user_id).await?.model().await?;
tracing::info!("users.write (user_id={user_id})");

self.users.insert(user_id, new_item.into());
self.users.get(&user_id).unwrap()
self.users
.entry(user_id)
.insert(new_item.into())
.downgrade()
})
}
}
19 changes: 13 additions & 6 deletions crates/mellow/src/model/hakumi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ impl HakumiModels {

new_item.refresh_oauth().await?;

self.users.insert(user_id, new_item);
self.users.get(&user_id).unwrap()
self.users
.entry(user_id)
.insert(new_item)
.downgrade()
})
}

Expand All @@ -72,9 +74,12 @@ impl HakumiModels {
tracing::info!("users.write (user_id={user_id})");
tracing::info!("users_by_discord.write (guild_id={guild_id}) (discord_id={user_id}) (user_id={user_id})");

self.users.insert(user_id, new_item);
self.users_by_discord.insert(key, user_id);
self.users.get(&user_id)
Some(self.users
.entry(user_id)
.insert(new_item)
.downgrade()
)
})
}

Expand All @@ -92,8 +97,10 @@ impl HakumiModels {
.value;
tracing::debug!("vs_documents.write (document_id={document_id})",);

self.vs_documents.insert(document_id, new_item);
self.vs_documents.get(&document_id).unwrap()
self.vs_documents
.entry(document_id)
.insert(new_item)
.downgrade()
})
}
}
Expand Down
19 changes: 13 additions & 6 deletions crates/mellow/src/model/mellow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ impl MellowModels {
let server = Server::get(guild_id).await?;
tracing::debug!("servers.write (guild_id={guild_id})");

self.servers.insert(guild_id, server);
self.servers.get(&guild_id).unwrap()
self.servers
.entry(guild_id)
.insert(server)
.downgrade()
})
}

Expand All @@ -62,8 +64,11 @@ impl MellowModels {
self.event_documents.insert((guild_id, document_kind), id);

if let Some(document) = document && let Some(id) = id {
HAKUMI_MODELS.vs_documents.insert(id, document);
HAKUMI_MODELS.vs_documents.get(&id)
Some(HAKUMI_MODELS.vs_documents
.entry(id)
.insert(document)
.downgrade()
)
} else { None }
})
}
Expand All @@ -85,8 +90,10 @@ impl MellowModels {
.unwrap_or_default();
tracing::debug!("member_settings.write (guild_id={guild_id}) (user_id={user_id})");

self.member_settings.insert(key, new_item.clone());
self.member_settings.get(&key).unwrap()
self.member_settings
.entry(key)
.insert(new_item)
.downgrade()
})
}
}
11 changes: 5 additions & 6 deletions crates/mellow/src/syncing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,9 @@ pub async fn get_connection_metadata(users: &Vec<&User>, server: &Server) -> Res
})
}

#[tracing::instrument(level = "trace")]
async fn get_role_name(guild_id: Id<GuildMarker>, role_id: Id<RoleMarker>) -> Result<String> {
DISCORD_MODELS.role(guild_id, role_id).await.map(|x| x.name.clone())
//return Ok(items.iter().find(|x| &x.id == id).map_or("unknown role".into(), |x| x.name.clone()));
fn get_role_name(guild_id: Id<GuildMarker>, role_id: Id<RoleMarker>) -> String {
DISCORD_MODELS.role(guild_id, role_id)
.map_or_else(|| "Unknown Role".into(), |x| x.name.clone())
}

// async_recursion required due to a cycle error caused by visual scripting
Expand Down Expand Up @@ -215,7 +214,7 @@ pub async fn sync_member(user: Option<&User>, member: &CachedMember, server: &Se
role_changes.push(RoleChange {
kind: RoleChangeKind::Added,
target_id: *role_id,
display_name: get_role_name(server.id, *role_id).await?
display_name: get_role_name(server.id, *role_id)
});
}
}
Expand All @@ -227,7 +226,7 @@ pub async fn sync_member(user: Option<&User>, member: &CachedMember, server: &Se
role_changes.push(RoleChange {
kind: RoleChangeKind::Removed,
target_id: *role_id,
display_name: get_role_name(server.id, *role_id).await?
display_name: get_role_name(server.id, *role_id)
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/mellow_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust-version.workspace = true
proc-macro = true

[dependencies]
syn = "2.0.58"
quote = "1.0.35"
darling = "0.20.8"
proc-macro2 = "1.0.79"
syn = "2.0.66"
quote = "1.0.36"
darling = "0.20.9"
proc-macro2 = "1.0.85"

0 comments on commit 14f61c1

Please sign in to comment.