changed where rwlocks lock

This commit is contained in:
jude 2020-09-07 12:04:10 +01:00
parent 8761dcb55f
commit 63b94a0113
3 changed files with 79 additions and 73 deletions

121
Cargo.lock generated
View File

@ -65,14 +65,14 @@ dependencies = [
[[package]] [[package]]
name = "async-tls" name = "async-tls"
version = "0.6.0" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce6977f57fa68da77ffe5542950d47e9c23d65f5bc7cb0a9f8700996913eec7" checksum = "d7e7fbc0843fc5ad3d5ca889c5b2bea9130984d34cd0e62db57ab70c2529a8e3"
dependencies = [ dependencies = [
"futures", "futures",
"rustls 0.16.0", "rustls 0.18.1",
"webpki", "webpki",
"webpki-roots 0.17.0", "webpki-roots 0.20.0",
] ]
[[package]] [[package]]
@ -88,9 +88,9 @@ dependencies = [
[[package]] [[package]]
name = "async-tungstenite" name = "async-tungstenite"
version = "0.4.2" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4187bb446c8ecb8849f17cef7553db8bdb09e482e806257130189958fb42dca7" checksum = "a5c45a0dd44b7e6533ac4e7acc38ead1a3b39885f5bbb738140d30ea528abc7c"
dependencies = [ dependencies = [
"async-tls", "async-tls",
"futures-io", "futures-io",
@ -126,15 +126,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]]
name = "base64"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
dependencies = [
"byteorder",
]
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.11.0" version = "0.11.0"
@ -176,6 +167,15 @@ dependencies = [
"generic-array 0.12.3", "generic-array 0.12.3",
] ]
[[package]]
name = "block-buffer"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
dependencies = [
"generic-array 0.14.3",
]
[[package]] [[package]]
name = "block-padding" name = "block-padding"
version = "0.1.5" version = "0.1.5"
@ -235,9 +235,9 @@ dependencies = [
[[package]] [[package]]
name = "command_attr" name = "command_attr"
version = "0.3.0-rc.0" version = "0.3.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "093ee06b40fcba41b22ddf85de7ed33728fe2e246fcdbc1b1d3ab26f87fabf3b" checksum = "c538daab2daaf13de61cea91648a62bb11d267ef629f707d5fe3dd080043ab4d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -260,6 +260,12 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac" checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac"
[[package]]
name = "cpuid-bool"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
[[package]] [[package]]
name = "crc32fast" name = "crc32fast"
version = "1.2.0" version = "1.2.0"
@ -299,6 +305,15 @@ dependencies = [
"generic-array 0.12.3", "generic-array 0.12.3",
] ]
[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
dependencies = [
"generic-array 0.14.3",
]
[[package]] [[package]]
name = "dotenv" name = "dotenv"
version = "0.15.0" version = "0.15.0"
@ -885,6 +900,12 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]]
name = "opaque-debug"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]] [[package]]
name = "openssl" name = "openssl"
version = "0.10.29" version = "0.10.29"
@ -1135,19 +1156,6 @@ dependencies = [
"winapi 0.3.8", "winapi 0.3.8",
] ]
[[package]]
name = "rustls"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b25a18b1bf7387f0145e7f8324e700805aade3842dd3db2e74e4cdeb4677c09e"
dependencies = [
"base64 0.10.1",
"log",
"ring",
"sct",
"webpki",
]
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.17.0" version = "0.17.0"
@ -1278,11 +1286,10 @@ dependencies = [
[[package]] [[package]]
name = "serenity" name = "serenity"
version = "0.9.0-rc.0" version = "0.9.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f2b6557e836289a35b45ecc9da78b464de79cec3aa9b7cfd07f654cbc478c00" checksum = "21e935a7f3f4752257183ee1f3553b10ea5b514a55de0e536dca7f3742b97d18"
dependencies = [ dependencies = [
"async-tls",
"async-trait", "async-trait",
"async-tungstenite", "async-tungstenite",
"audiopus", "audiopus",
@ -1297,7 +1304,6 @@ dependencies = [
"log", "log",
"rand", "rand",
"reqwest", "reqwest",
"rustls 0.18.1",
"serde", "serde",
"serde_json", "serde_json",
"static_assertions", "static_assertions",
@ -1305,8 +1311,6 @@ dependencies = [
"typemap_rev", "typemap_rev",
"url", "url",
"uwl", "uwl",
"webpki",
"webpki-roots 0.19.0",
"xsalsa20poly1305", "xsalsa20poly1305",
] ]
@ -1316,10 +1320,23 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df" checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df"
dependencies = [ dependencies = [
"block-buffer", "block-buffer 0.7.3",
"digest", "digest 0.8.1",
"fake-simd", "fake-simd",
"opaque-debug", "opaque-debug 0.2.3",
]
[[package]]
name = "sha-1"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "170a36ea86c864a3f16dd2687712dd6646f7019f301e57537c7f4dc9f5916770"
dependencies = [
"block-buffer 0.9.0",
"cfg-if",
"cpuid-bool",
"digest 0.9.0",
"opaque-debug 0.3.0",
] ]
[[package]] [[package]]
@ -1328,10 +1345,10 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0" checksum = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0"
dependencies = [ dependencies = [
"block-buffer", "block-buffer 0.7.3",
"digest", "digest 0.8.1",
"fake-simd", "fake-simd",
"opaque-debug", "opaque-debug 0.2.3",
] ]
[[package]] [[package]]
@ -1421,7 +1438,7 @@ dependencies = [
"byteorder", "byteorder",
"crossbeam-queue", "crossbeam-queue",
"crossbeam-utils", "crossbeam-utils",
"digest", "digest 0.8.1",
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-util", "futures-util",
@ -1433,7 +1450,7 @@ dependencies = [
"num-bigint", "num-bigint",
"percent-encoding", "percent-encoding",
"rand", "rand",
"sha-1", "sha-1 0.8.2",
"sha2", "sha2",
"sqlformat", "sqlformat",
"tokio", "tokio",
@ -1629,11 +1646,11 @@ checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382"
[[package]] [[package]]
name = "tungstenite" name = "tungstenite"
version = "0.10.1" version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfea31758bf674f990918962e8e5f07071a3161bd7c4138ed23e416e1ac4264e" checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23"
dependencies = [ dependencies = [
"base64 0.11.0", "base64 0.12.3",
"byteorder", "byteorder",
"bytes", "bytes",
"http", "http",
@ -1641,7 +1658,7 @@ dependencies = [
"input_buffer", "input_buffer",
"log", "log",
"rand", "rand",
"sha-1", "sha-1 0.9.1",
"url", "url",
"utf-8", "utf-8",
] ]
@ -1854,18 +1871,18 @@ dependencies = [
[[package]] [[package]]
name = "webpki-roots" name = "webpki-roots"
version = "0.17.0" version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a262ae37dd9d60f60dd473d1158f9fbebf110ba7b6a5051c8160460f6043718b" checksum = "f8eff4b7516a57307f9349c64bf34caa34b940b66fed4b2fb3136cb7386e5739"
dependencies = [ dependencies = [
"webpki", "webpki",
] ]
[[package]] [[package]]
name = "webpki-roots" name = "webpki-roots"
version = "0.19.0" version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8eff4b7516a57307f9349c64bf34caa34b940b66fed4b2fb3136cb7386e5739" checksum = "0f20dea7535251981a9670857150d571846545088359b28e4951d350bdaf179f"
dependencies = [ dependencies = [
"webpki", "webpki",
] ]

View File

@ -5,7 +5,7 @@ authors = ["jude-lafitteIII <judewrs@gmail.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
serenity = {version = "0.9.0-rc.0", features = ["voice", "collector"]} serenity = {version = "0.9.0-rc.1", features = ["voice", "collector"]}
sqlx = {version = "0.3.5", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal"]} sqlx = {version = "0.3.5", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal"]}
dotenv = "0.15" dotenv = "0.15"
tokio = {version = "0.2.19", features = ["fs", "sync", "process", "io-util"]} tokio = {version = "0.2.19", features = ["fs", "sync", "process", "io-util"]}

View File

@ -55,10 +55,7 @@ use sqlx::{
use dotenv::dotenv; use dotenv::dotenv;
use tokio::{ use tokio::{
sync::{ sync::RwLock,
RwLock,
RwLockWriteGuard,
},
time, time,
}; };
@ -327,11 +324,9 @@ SELECT name, id, plays, public, server_id, uploader_id
let voice_manager_lock = ctx.data.read().await let voice_manager_lock = ctx.data.read().await
.get::<VoiceManager>().cloned().expect("Could not get VoiceManager from data"); .get::<VoiceManager>().cloned().expect("Could not get VoiceManager from data");
let voice_guilds_lock = ctx.data.read().await let voice_guilds = ctx.data.read().await
.get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data"); .get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data");
let voice_guilds = voice_guilds_lock.write().await;
let mut voice_manager = voice_manager_lock.lock().await; let mut voice_manager = voice_manager_lock.lock().await;
if let Some(handler) = voice_manager.join(guild_id, user_channel) { if let Some(handler) = voice_manager.join(guild_id, user_channel) {
@ -346,7 +341,7 @@ SELECT name, id, plays, public, server_id, uploader_id
} }
} }
async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: RwLockWriteGuard<'_, HashMap<GuildId, u64>>, pool: MySqlPool) async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, voice_guilds: Arc<RwLock<HashMap<GuildId, u64>>>, pool: MySqlPool)
-> Result<(), Box<dyn std::error::Error + Send + Sync>> { -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let audio = handler.play_only(sound.store_sound_source(pool.clone()).await?); let audio = handler.play_only(sound.store_sound_source(pool.clone()).await?);
@ -360,10 +355,11 @@ async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHand
sound.plays += 1; sound.plays += 1;
sound.commit(pool).await?; sound.commit(pool).await?;
let start = SystemTime::now(); {
let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); let since_epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
voice_guilds.insert(GuildId(guild.id), since_epoch.as_secs()); voice_guilds.write().await.insert(GuildId(guild.id), since_epoch.as_secs());
}
Ok(()) Ok(())
} }
@ -484,14 +480,9 @@ async fn disconnect_from_inactive(voice_manager_mutex: Arc<SerenityMutex<ClientV
for (guild, last_active) in voice_guilds_acquired.iter() { for (guild, last_active) in voice_guilds_acquired.iter() {
if (now - last_active) > wait_time { if (now - last_active) > wait_time {
let mut voice_manager = voice_manager_mutex.lock().await; let mut voice_manager = voice_manager_mutex.lock().await;
let manager_opt = voice_manager.get_mut(guild); voice_manager.leave(guild);
if let Some(manager) = manager_opt {
manager.leave();
}
} }
} }
} }
@ -542,11 +533,9 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
Some(handler) => { Some(handler) => {
let guild_data = GuildData::get_from_id(guild, pool.clone()).await.unwrap(); let guild_data = GuildData::get_from_id(guild, pool.clone()).await.unwrap();
let voice_guilds_lock = ctx.data.read().await let voice_guilds = ctx.data.read().await
.get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data"); .get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data");
let voice_guilds = voice_guilds_lock.write().await;
play_audio(sound, guild_data, handler, voice_guilds, pool).await?; play_audio(sound, guild_data, handler, voice_guilds, pool).await?;
msg.channel_id.say(&ctx, format!("Playing sound {} with ID {}", sound.name, sound.id)).await?; msg.channel_id.say(&ctx, format!("Playing sound {} with ID {}", sound.name, sound.id)).await?;