diff --git a/Cargo.lock b/Cargo.lock index 4a98d677..c63b7a08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,6 +40,17 @@ version = "1.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704" +[[package]] +name = "async-channel" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2114d64672151c0c5eaa5e131ec84a74f06e1e559830dabba01ca30605d66319" +dependencies = [ + "concurrent-queue", + "event-listener", + "futures-core", +] + [[package]] name = "async-io" version = "1.7.0" @@ -59,15 +70,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "async-lock" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e97a171d191782fba31bb902b14ad94e24a68145032b7eedf871ab0bc0d077b6" -dependencies = [ - "event-listener", -] - [[package]] name = "async-trait" version = "0.1.56" @@ -88,6 +90,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "atomic" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b88d82667eca772c4aa12f0f1348b3ae643424c8876448f3f7bd5787032e234c" +dependencies = [ + "autocfg", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -142,12 +153,6 @@ version = "3.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3" -[[package]] -name = "bytecount" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c676a478f63e9fa2dd5368a42f28bba0d6c560b775f38583c8bbaa7fcd67c9c" - [[package]] name = "byteorder" version = "1.4.3" @@ -166,49 +171,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c" -[[package]] -name = "camino" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "869119e97797867fd90f5e22af7d0bd274bd4635ebb9eb68c04f3f513ae6c412" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo-platform" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbdb825da8a5df079a43676dbe042702f1707b1109f713a01420fbb4cc71fa27" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo_metadata" -version = "0.14.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4acbb09d9ee8e23699b9634375c72795d095bf268439da88562cf9b501f181fa" -dependencies = [ - "camino", - "cargo-platform", - "semver", - "serde", - "serde_json", -] - [[package]] name = "cc" version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - [[package]] name = "cfg-if" version = "1.0.0" @@ -283,7 +251,7 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -292,23 +260,8 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c02a4d71819009c192cf4872265391563fd6a84c81ff2c0f2a7026ca4c1d85c" dependencies = [ - "cfg-if 1.0.0", - "crossbeam-utils 0.8.10", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" -dependencies = [ - "autocfg", - "cfg-if 0.1.10", - "crossbeam-utils 0.7.2", - "lazy_static", - "maybe-uninit", - "memoffset", - "scopeguard", + "cfg-if", + "crossbeam-utils", ] [[package]] @@ -317,19 +270,8 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f25d8400f4a7a5778f0e4e52384a48cbd9b5c495d110786187fc750075277a2" dependencies = [ - "cfg-if 1.0.0", - "crossbeam-utils 0.8.10", -] - -[[package]] -name = "crossbeam-utils" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" -dependencies = [ - "autocfg", - "cfg-if 0.1.10", - "lazy_static", + "cfg-if", + "crossbeam-utils", ] [[package]] @@ -338,7 +280,7 @@ version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d82ee10ce34d7bc12c2122495e7593a9c41347ecdd64185af4ecf72cb1a7f83" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "once_cell", ] @@ -358,7 +300,7 @@ version = "5.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3495912c9c1ccf2e18976439f4443f3fee0fd61f424ff99fde6a66b15ecb448f" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "hashbrown", "lock_api", "parking_lot_core 0.9.3", @@ -416,15 +358,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdc0036e5881a30eec779c55ac7b4bf54eedc3c72ddc8d02c2a580c43b1c9ba" -[[package]] -name = "error-chain" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" -dependencies = [ - "version_check", -] - [[package]] name = "event-listener" version = "2.5.2" @@ -598,17 +531,11 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "wasi 0.11.0+wasi-snapshot-preview1", ] -[[package]] -name = "glob" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" - [[package]] name = "h2" version = "0.3.13" @@ -787,7 +714,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -862,16 +789,7 @@ version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ - "cfg-if 1.0.0", -] - -[[package]] -name = "mach" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" -dependencies = [ - "libc", + "cfg-if", ] [[package]] @@ -880,12 +798,6 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" -[[package]] -name = "maybe-uninit" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" - [[package]] name = "md-5" version = "0.10.1" @@ -901,15 +813,6 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" -[[package]] -name = "memoffset" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "043175f069eda7b85febe4a74abbaeff828d9f8b448515d3151a14a3542811aa" -dependencies = [ - "autocfg", -] - [[package]] name = "minimal-lexical" version = "0.2.1" @@ -937,31 +840,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "moka" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b3caf8ff6db712c7f7fe018e7fb6d2ddbaf428b74f94a48fe075ef09a6b8a1" -dependencies = [ - "async-io", - "async-lock", - "crossbeam-channel", - "crossbeam-epoch", - "crossbeam-utils 0.8.10", - "futures-util", - "num_cpus", - "once_cell", - "parking_lot 0.12.1", - "quanta", - "scheduled-thread-pool", - "skeptic", - "smallvec", - "tagptr", - "thiserror", - "triomphe", - "uuid", -] - [[package]] name = "nom" version = "7.1.1" @@ -1064,7 +942,7 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "instant", "libc", "redox_syscall", @@ -1078,7 +956,7 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "redox_syscall", "smallvec", @@ -1121,7 +999,7 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "685404d509889fade3e86fe3a5803bca2ec09b0c0778d5ada6ec8bf7a8de5259" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "log", "wepoll-ffi", @@ -1143,33 +1021,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "pulldown-cmark" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34f197a544b0c9ab3ae46c359a7ec9cbbb5c7bf97054266fecb7ead794a181d6" -dependencies = [ - "bitflags", - "memchr", - "unicase", -] - -[[package]] -name = "quanta" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bafd74c340a0a7e79415981ede3460df16b530fd071541901a57416eea950b17" -dependencies = [ - "crossbeam-utils 0.8.10", - "libc", - "mach", - "once_cell", - "raw-cpuid", - "wasi 0.10.0+wasi-snapshot-preview1", - "web-sys", - "winapi", -] - [[package]] name = "quote" version = "1.0.20" @@ -1209,15 +1060,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "raw-cpuid" -version = "10.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "738bc47119e3eeccc7e94c4a506901aea5e7b4944ecd0829cbebf4af04ceda12" -dependencies = [ - "bitflags", -] - [[package]] name = "redox_syscall" version = "0.2.13" @@ -1238,15 +1080,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] - [[package]] name = "ring" version = "0.16.20" @@ -1301,15 +1134,6 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" -[[package]] -name = "same-file" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" -dependencies = [ - "winapi-util", -] - [[package]] name = "schannel" version = "0.1.20" @@ -1320,15 +1144,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "scheduled-thread-pool" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "977a7519bff143a44f842fd07e80ad1329295bd71686457f18e496736f4bf9bf" -dependencies = [ - "parking_lot 0.12.1", -] - [[package]] name = "scopeguard" version = "1.1.0" @@ -1368,15 +1183,6 @@ dependencies = [ "libc", ] -[[package]] -name = "semver" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2333e6df6d6598f2b1974829f853c2b4c5f4a6e503c10af918081aa6f8564e1" -dependencies = [ - "serde", -] - [[package]] name = "serde" version = "1.0.138" @@ -1435,7 +1241,7 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest", ] @@ -1446,7 +1252,7 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55deaec60f81eefe3cce0dc50bda92d6d8e88f2a27df7c5033b42afeb1ed2676" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest", ] @@ -1460,21 +1266,6 @@ dependencies = [ "libc", ] -[[package]] -name = "skeptic" -version = "0.13.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16d23b015676c90a0f01c197bfdc786c20342c73a0afdda9025adb0bc42940a8" -dependencies = [ - "bytecount", - "cargo_metadata", - "error-chain", - "glob", - "pulldown-cmark", - "tempfile", - "walkdir", -] - [[package]] name = "slab" version = "0.4.6" @@ -1621,10 +1412,10 @@ dependencies = [ "dotenv", "emojis", "futures", - "moka", "serde", "serde_json", "sqlx", + "stretto", "tokio", "twilight-error", "twilight-gateway", @@ -1636,6 +1427,30 @@ dependencies = [ "twilight-util", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "stretto" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f7537062c5906795813a413b4acc0508406b7d12f48bd1f7ed81bd9500569ba" +dependencies = [ + "async-channel", + "async-io", + "atomic", + "crossbeam-channel", + "futures", + "parking_lot 0.12.1", + "rand", + "thiserror", + "twox-hash", + "wg", +] + [[package]] name = "stringprep" version = "0.1.2" @@ -1663,26 +1478,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "tagptr" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" - -[[package]] -name = "tempfile" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" -dependencies = [ - "cfg-if 1.0.0", - "fastrand", - "libc", - "redox_syscall", - "remove_dir_all", - "winapi", -] - [[package]] name = "thiserror" version = "1.0.31" @@ -1834,7 +1629,7 @@ version = "0.1.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a400e31aa60b9d44a52a8ee0343b5b18566b03a8321e0d321f695cf56e940160" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -1860,12 +1655,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "triomphe" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fe1b3800b35f9b936c28dc59dbda91b195371269396784d931fe2a5a2be3d2f" - [[package]] name = "try-lock" version = "0.2.3" @@ -2046,19 +1835,21 @@ dependencies = [ ] [[package]] -name = "typenum" -version = "1.15.0" +name = "twox-hash" +version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" +checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" +dependencies = [ + "cfg-if", + "rand", + "static_assertions", +] [[package]] -name = "unicase" -version = "2.6.0" +name = "typenum" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" -dependencies = [ - "version_check", -] +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" [[package]] name = "unicode-bidi" @@ -2117,15 +1908,6 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" -[[package]] -name = "uuid" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd6469f4314d5f1ffec476e05f17cc9a78bc7a27a6a857842170bdf8d6f98d2f" -dependencies = [ - "getrandom", -] - [[package]] name = "vcpkg" version = "0.2.15" @@ -2144,17 +1926,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" -[[package]] -name = "walkdir" -version = "2.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" -dependencies = [ - "same-file", - "winapi", - "winapi-util", -] - [[package]] name = "want" version = "0.3.0" @@ -2183,7 +1954,7 @@ version = "0.2.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c53b543413a17a202f4be280a7e5c62a1c69345f5de525ee64f8cfdbc954994" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "wasm-bindgen-macro", ] @@ -2269,6 +2040,15 @@ dependencies = [ "cc", ] +[[package]] +name = "wg" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c4f805fab9f31f0295729d0bc4d563bc466223a45326718e842a5d86a8d1c65" +dependencies = [ + "parking_lot 0.12.1", +] + [[package]] name = "whoami" version = "1.2.1" @@ -2295,15 +2075,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 544f5843..c4631c9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,4 +26,4 @@ serde_json = "1.0" serde = "1.0.137" emojis = "0.4.0" dashmap = "5.3.4" -moka = { version = "0.9.0", features = ["future"] } +stretto = { version = "0.5.1", features = ["async"] } diff --git a/Dockerfile b/Dockerfile index 08d02f4b..e1986926 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.62.0 as builder +FROM rust:1.62.0-slim-buster as builder WORKDIR /usr/src/starboard # force cargo to update the crates.io index. diff --git a/src/cache/cache.rs b/src/cache/cache.rs index 80008a3a..532544be 100644 --- a/src/cache/cache.rs +++ b/src/cache/cache.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use dashmap::{DashMap, DashSet}; use twilight_gateway::Event; use twilight_model::id::{ @@ -11,30 +9,35 @@ use crate::constants; use super::{models::message::CachedMessage, update::UpdateCache}; -#[derive(Clone)] pub struct Cache { // discord side - pub guild_emojis: Arc, DashSet>>>, - pub messages: moka::future::Cache, Arc>, + pub guild_emojis: DashMap, DashSet>>, + pub messages: stretto::AsyncCache, CachedMessage>, // database side - pub autostar_channel_ids: Arc>>, + pub autostar_channel_ids: DashSet>, // autocomplete - pub guild_autostar_channel_names: moka::future::Cache, Arc>>, + pub guild_autostar_channel_names: stretto::AsyncCache, Vec>, } impl Cache { pub fn new(autostar_channel_ids: DashSet>) -> Self { Self { - guild_emojis: Arc::new(DashMap::new()), - messages: moka::future::Cache::builder() - .max_capacity(constants::MAX_MESSAGES) - .build(), - autostar_channel_ids: Arc::new(autostar_channel_ids), - guild_autostar_channel_names: moka::future::Cache::builder() - .max_capacity(constants::MAX_AUTOSTAR_NAMES) - .build(), + guild_emojis: DashMap::new(), + messages: stretto::AsyncCache::new( + (constants::MAX_MESSAGES * 10).try_into().unwrap(), + constants::MAX_MESSAGES.into(), + tokio::spawn, + ) + .unwrap(), + autostar_channel_ids, + guild_autostar_channel_names: stretto::AsyncCache::new( + (constants::MAX_AUTOSTAR_NAMES * 10).try_into().unwrap(), + constants::MAX_AUTOSTAR_NAMES.into(), + tokio::spawn, + ) + .unwrap(), } } diff --git a/src/cache/events/message.rs b/src/cache/events/message.rs index 311a1976..38a02ea3 100644 --- a/src/cache/events/message.rs +++ b/src/cache/events/message.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use async_trait::async_trait; use twilight_model::gateway::payload::incoming::{ MessageCreate, MessageDelete, MessageDeleteBulk, MessageUpdate, @@ -21,14 +19,14 @@ impl UpdateCache for MessageCreate { embeds: self.embeds.clone(), }; - cache.messages.insert(message.id, Arc::new(message)).await; + cache.messages.insert(message.id, message, 1).await; } } #[async_trait] impl UpdateCache for MessageDelete { async fn update_cache(&self, cache: &Cache) { - cache.messages.invalidate(&self.id).await; + cache.messages.remove(&self.id).await; } } @@ -36,7 +34,7 @@ impl UpdateCache for MessageDelete { impl UpdateCache for MessageDeleteBulk { async fn update_cache(&self, cache: &Cache) { for id in &self.ids { - cache.messages.invalidate(id).await; + cache.messages.remove(id).await; } } } @@ -53,11 +51,11 @@ impl UpdateCache for MessageUpdate { let attachments = match &self.attachments { Some(attachments) => attachments.clone(), - None => cached.attachments.clone(), + None => cached.value().attachments.clone(), }; let embeds = match &self.embeds { Some(embeds) => embeds.clone(), - None => cached.embeds.clone(), + None => cached.value().embeds.clone(), }; let message = CachedMessage { @@ -66,6 +64,6 @@ impl UpdateCache for MessageUpdate { embeds, }; - cache.messages.insert(self.id, Arc::new(message)).await; + cache.messages.insert(self.id, message, 1).await; } } diff --git a/src/client/bot.rs b/src/client/bot.rs index 9333dfe9..3a39fe6b 100644 --- a/src/client/bot.rs +++ b/src/client/bot.rs @@ -1,4 +1,4 @@ -use std::{fmt::Debug, sync::Arc}; +use std::fmt::Debug; use sqlx::PgPool; use tokio::sync::RwLock; @@ -18,17 +18,16 @@ use crate::{cache::cache::Cache, client::config::Config}; use super::cooldowns::Cooldowns; -#[derive(Clone)] pub struct StarboardBot { - pub cluster: Arc, - pub http: Arc, + pub cluster: Cluster, + pub http: HttpClient, pub cache: Cache, - pub application: Arc>>, - pub pool: Arc, - pub errors: Arc, - pub standby: Arc, - pub config: Arc, - pub cooldowns: Arc, + pub application: RwLock>, + pub pool: PgPool, + pub errors: ErrorHandler, + pub standby: Standby, + pub config: Config, + pub cooldowns: Cooldowns, } impl Debug for StarboardBot { @@ -89,15 +88,15 @@ impl StarboardBot { Ok(( events, Self { - cluster: Arc::new(cluster), - http: Arc::new(http), + cluster: cluster, + http: http, cache: cache, - application: Arc::new(RwLock::new(None)), - pool: Arc::new(pool), - errors: Arc::new(errors), - standby: Arc::new(Standby::new()), - config: Arc::new(config), - cooldowns: Arc::new(Cooldowns::new()), + application: RwLock::new(None), + pool: pool, + errors: errors, + standby: Standby::new(), + config: config, + cooldowns: Cooldowns::new(), }, )) } diff --git a/src/client/runner.rs b/src/client/runner.rs index 2eabd5f0..13598253 100644 --- a/src/client/runner.rs +++ b/src/client/runner.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use futures::stream::StreamExt; use tokio::{ signal::unix::{signal, SignalKind}, @@ -7,7 +9,7 @@ use twilight_gateway::cluster::Events; use crate::{client::bot::StarboardBot, events::handle_event}; -async fn shutdown_handler(bot: StarboardBot) { +async fn shutdown_handler(bot: Arc) { let (tx, mut rx) = mpsc::unbounded_channel(); for kind in [SignalKind::terminate(), SignalKind::interrupt()].into_iter() { @@ -26,6 +28,8 @@ async fn shutdown_handler(bot: StarboardBot) { } pub async fn run(mut events: Events, bot: StarboardBot) { + let bot = Arc::new(bot); + if bot.config.development { println!("Running bot in development mode."); } diff --git a/src/constants.rs b/src/constants.rs index 7a26b9ec..6525173a 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -3,8 +3,8 @@ use std::time::Duration; pub const BOT_COLOR: u32 = 0xFFE19C; // Cache size -pub const MAX_MESSAGES: u64 = 10_000; -pub const MAX_AUTOSTAR_NAMES: u64 = 100; +pub const MAX_MESSAGES: u32 = 10_000; +pub const MAX_AUTOSTAR_NAMES: u32 = 100; // Cooldowns pub const AUTOSTAR_COOLDOWN: (u32, Duration) = (5, Duration::from_secs(20)); diff --git a/src/core/autostar.rs b/src/core/autostar.rs index 8da8d500..091c9383 100644 --- a/src/core/autostar.rs +++ b/src/core/autostar.rs @@ -112,8 +112,9 @@ async fn get_status(bot: &StarboardBot, asc: &AutoStarChannel, event: &MessageCr let updated_msg = bot.cache.messages.get(&event.id); let mut still_invalid = true; + // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA if let Some(msg) = updated_msg { - if has_image(&msg.embeds, &msg.attachments) { + if has_image(&msg.value().embeds, &msg.value().attachments) { still_invalid = false; } } else { diff --git a/src/events/mod.rs b/src/events/mod.rs index cb8ff513..cb9703e3 100644 --- a/src/events/mod.rs +++ b/src/events/mod.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use twilight_gateway::Event; use twilight_model::gateway::payload::outgoing::RequestGuildMembers; @@ -7,11 +9,11 @@ use crate::{ interactions::{commands::register::post_commands, handle::handle_interaction}, }; -pub async fn handle_event(shard_id: u64, event: Event, bot: StarboardBot) { +pub async fn handle_event(shard_id: u64, event: Event, bot: Arc) { tokio::spawn(internal_handle_event(shard_id, event, bot)); } -async fn internal_handle_event(shard_id: u64, event: Event, bot: StarboardBot) { +async fn internal_handle_event(shard_id: u64, event: Event, bot: Arc) { bot.cache.update(&event).await; bot.standby.process(&event); @@ -26,7 +28,7 @@ async fn internal_handle_event(shard_id: u64, event: Event, bot: StarboardBot) { } } -async fn match_events(shard_id: u64, event: Event, bot: StarboardBot) -> anyhow::Result<()> { +async fn match_events(shard_id: u64, event: Event, bot: Arc) -> anyhow::Result<()> { match event { Event::InteractionCreate(int) => handle_interaction(shard_id, int.0, bot).await?, Event::ShardConnected(event) => println!("Shard {} connected.", event.shard_id), diff --git a/src/interactions/autocomplete/autostar_name.rs b/src/interactions/autocomplete/autostar_name.rs index f64a1334..e109fb07 100644 --- a/src/interactions/autocomplete/autostar_name.rs +++ b/src/interactions/autocomplete/autostar_name.rs @@ -13,7 +13,7 @@ pub async fn autostar_name_autocomplete( .guild_autostar_channel_names .get(&interaction.guild_id.unwrap()) { - Some(names) => (*names).clone(), + Some(names) => (*names.value()).clone(), None => { AutoStarChannel::list_by_guild(&bot.pool, unwrap_id!(interaction.guild_id.unwrap())) .await? diff --git a/src/interactions/autocomplete/handle.rs b/src/interactions/autocomplete/handle.rs index 8458e89c..a1a09aa2 100644 --- a/src/interactions/autocomplete/handle.rs +++ b/src/interactions/autocomplete/handle.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use twilight_model::{ application::interaction::{ application_command_autocomplete::{ @@ -61,7 +63,7 @@ pub fn qualified_name(interaction: &Box) -> Stri } pub async fn handle_autocomplete( - bot: StarboardBot, + bot: Arc, interaction: Box, ) -> anyhow::Result<()> { let options = match qualified_name(&interaction).as_str() { diff --git a/src/interactions/commands/chat/autostar/create.rs b/src/interactions/commands/chat/autostar/create.rs index cdeef530..5cdeb42a 100644 --- a/src/interactions/commands/chat/autostar/create.rs +++ b/src/interactions/commands/chat/autostar/create.rs @@ -53,7 +53,7 @@ impl CreateAutoStarChannel { ctx.bot .cache .guild_autostar_channel_names - .invalidate(&guild_id) + .remove(&guild_id) .await; ctx.respond_str( diff --git a/src/interactions/commands/chat/autostar/delete.rs b/src/interactions/commands/chat/autostar/delete.rs index 98561784..ffde30d2 100644 --- a/src/interactions/commands/chat/autostar/delete.rs +++ b/src/interactions/commands/chat/autostar/delete.rs @@ -23,7 +23,7 @@ impl DeleteAutoStarChannel { ctx.bot .cache .guild_autostar_channel_names - .invalidate(&guild_id) + .remove(&guild_id) .await; ctx.respond_str(&format!("Deleted autostar channel '{}'.", self.name), false) .await?; diff --git a/src/interactions/commands/chat/autostar/rename.rs b/src/interactions/commands/chat/autostar/rename.rs index 61c993bd..55b1d190 100644 --- a/src/interactions/commands/chat/autostar/rename.rs +++ b/src/interactions/commands/chat/autostar/rename.rs @@ -56,7 +56,7 @@ impl RenameAutoStarChannel { ctx.bot .cache .guild_autostar_channel_names - .invalidate(&guild_id) + .remove(&guild_id) .await; ctx.respond_str( diff --git a/src/interactions/commands/context.rs b/src/interactions/commands/context.rs index 703aaf7e..416ba9fb 100644 --- a/src/interactions/commands/context.rs +++ b/src/interactions/commands/context.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use twilight_http::Response; use twilight_model::{ application::interaction::ApplicationCommand, @@ -11,7 +13,7 @@ use crate::client::bot::StarboardBot; #[derive(Debug)] pub struct CommandCtx { pub shard_id: u64, - pub bot: StarboardBot, + pub bot: Arc, pub interaction: Box, responded: bool, } @@ -19,7 +21,11 @@ pub struct CommandCtx { type TwResult = Result, twilight_http::Error>; impl CommandCtx { - pub fn new(shard_id: u64, bot: StarboardBot, interaction: Box) -> Self { + pub fn new( + shard_id: u64, + bot: Arc, + interaction: Box, + ) -> Self { Self { shard_id, bot, diff --git a/src/interactions/commands/handle.rs b/src/interactions/commands/handle.rs index d0e020c5..6aeec03a 100644 --- a/src/interactions/commands/handle.rs +++ b/src/interactions/commands/handle.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use twilight_interactions::command::CommandModel; use twilight_model::application::interaction::ApplicationCommand; @@ -21,7 +23,7 @@ macro_rules! match_commands { pub async fn handle_command( shard_id: u64, - bot: StarboardBot, + bot: Arc, interaction: Box, ) -> anyhow::Result<()> { let ctx = CommandCtx::new(shard_id, bot, interaction); diff --git a/src/interactions/commands/register.rs b/src/interactions/commands/register.rs index b3c09282..cd0ef88c 100644 --- a/src/interactions/commands/register.rs +++ b/src/interactions/commands/register.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use twilight_interactions::command::CreateCommand; use crate::{client::bot::StarboardBot, interactions::commands::chat}; @@ -12,7 +14,7 @@ macro_rules! commands_to_create { }; } -pub async fn post_commands(bot: StarboardBot) { +pub async fn post_commands(bot: Arc) { let inter_client = bot.interaction_client().await; let commands = commands_to_create!(chat::ping::Ping, chat::autostar::AutoStar); diff --git a/src/interactions/components/dismiss.rs b/src/interactions/components/dismiss.rs index fb76345c..2695a518 100644 --- a/src/interactions/components/dismiss.rs +++ b/src/interactions/components/dismiss.rs @@ -1,9 +1,11 @@ +use std::sync::Arc; + use twilight_model::application::interaction::MessageComponentInteraction; use crate::client::bot::StarboardBot; pub async fn handle_dismiss( - bot: StarboardBot, + bot: Arc, interaction: Box, ) -> anyhow::Result<()> { assert!(interaction.is_dm()); diff --git a/src/interactions/components/handle.rs b/src/interactions/components/handle.rs index 32e053bf..b562d754 100644 --- a/src/interactions/components/handle.rs +++ b/src/interactions/components/handle.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use twilight_model::application::interaction::MessageComponentInteraction; use crate::client::bot::StarboardBot; @@ -5,7 +7,7 @@ use crate::client::bot::StarboardBot; use super::dismiss::handle_dismiss; pub async fn handle_component( - bot: StarboardBot, + bot: Arc, interaction: Box, ) -> anyhow::Result<()> { match interaction.data.custom_id.as_str() { diff --git a/src/interactions/handle.rs b/src/interactions/handle.rs index 88b72d4f..37dfb01c 100644 --- a/src/interactions/handle.rs +++ b/src/interactions/handle.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use twilight_model::application::interaction::Interaction; use crate::client::bot::StarboardBot; @@ -10,7 +12,7 @@ use super::{ pub async fn handle_interaction( shard_id: u64, interaction: Interaction, - bot: StarboardBot, + bot: Arc, ) -> anyhow::Result<()> { match interaction { Interaction::ApplicationCommand(interaction) => {