feat(gateway): split off cache into separate package, rename package to 'gateway'

This commit is contained in:
spiral 2022-05-10 07:04:33 -04:00
parent 433603feaa
commit abb01aaf2c
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
12 changed files with 1733 additions and 33 deletions

View File

@ -4,6 +4,7 @@
# Include project code and build files # Include project code and build files
!PluralKit.*/ !PluralKit.*/
!gateway/ !gateway/
!myriad_rs/
!Myriad/ !Myriad/
!PluralKit.sln !PluralKit.sln
!nuget.config !nuget.config

3
.gitignore vendored
View File

@ -2,6 +2,9 @@
bin/ bin/
obj/ obj/
# rust build files
target/
# IDE files # IDE files
.idea/ .idea/
.run/ .run/

View File

@ -1,7 +1,7 @@
# twilight requires newer rustc than what is in alpine:latest # twilight requires newer rustc than what is in alpine:latest
FROM alpine:edge AS builder FROM alpine:edge AS builder
RUN apk add cargo RUN apk add cargo protobuf
# Precache crates.io index # Precache crates.io index
RUN cargo search >/dev/null RUN cargo search >/dev/null
@ -9,6 +9,9 @@ RUN cargo search >/dev/null
WORKDIR /build WORKDIR /build
COPY proto/ /build/proto COPY proto/ /build/proto
COPY gateway/ /build/gateway COPY gateway/ /build/gateway
COPY myriad_rs/ /build/myriad_rs
# todo: cache build of myriad_rs elsewhere
RUN (cd gateway && cargo build --release) RUN (cd gateway && cargo build --release)

229
gateway/Cargo.lock generated
View File

@ -155,6 +155,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "cmake"
version = "0.1.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "combine" name = "combine"
version = "4.6.3" version = "4.6.3"
@ -293,12 +302,33 @@ version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0"
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]] [[package]]
name = "fallible-iterator" name = "fallible-iterator"
version = "0.2.0" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fastrand"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf"
dependencies = [
"instant",
]
[[package]]
name = "fixedbitset"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "279fb028e20b3c4c320317955b77c5e0c9701f05a1d309905d6fc702cdc5053e"
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.22" version = "1.0.22"
@ -417,6 +447,33 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "gateway"
version = "0.1.0"
dependencies = [
"anyhow",
"config",
"deadpool",
"deadpool-postgres",
"futures",
"lazy_static",
"libc",
"myriad",
"postgres-types",
"procfs",
"redis",
"serde",
"tokio",
"tokio-postgres",
"tokio-stream",
"tracing",
"tracing-subscriber",
"twilight-gateway",
"twilight-gateway-queue",
"twilight-http",
"twilight-model",
]
[[package]] [[package]]
name = "generic-array" name = "generic-array"
version = "0.14.4" version = "0.14.4"
@ -463,6 +520,12 @@ version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.20" version = "0.1.20"
@ -590,6 +653,15 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "itertools"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.8" version = "0.4.8"
@ -724,6 +796,23 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "multimap"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a"
[[package]]
name = "myriad"
version = "0.1.0"
dependencies = [
"anyhow",
"prost",
"prost-build",
"redis",
"twilight-gateway",
]
[[package]] [[package]]
name = "nom" name = "nom"
version = "5.1.2" version = "5.1.2"
@ -831,6 +920,16 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "petgraph"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a13a2fa9d0b63e5f22328828741e523766fff0ee9e779316902290dff3f824f"
dependencies = [
"fixedbitset",
"indexmap",
]
[[package]] [[package]]
name = "phf" name = "phf"
version = "0.10.0" version = "0.10.0"
@ -867,32 +966,6 @@ version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1a3ea4f0dd7f1f3e512cf97bf100819aa547f36a6eccac8dbaae839eb92363e" checksum = "d1a3ea4f0dd7f1f3e512cf97bf100819aa547f36a6eccac8dbaae839eb92363e"
[[package]]
name = "pluralkit"
version = "0.1.0"
dependencies = [
"anyhow",
"config",
"deadpool",
"deadpool-postgres",
"futures",
"lazy_static",
"libc",
"postgres-types",
"procfs",
"redis",
"serde",
"tokio",
"tokio-postgres",
"tokio-stream",
"tracing",
"tracing-subscriber",
"twilight-gateway",
"twilight-gateway-queue",
"twilight-http",
"twilight-model",
]
[[package]] [[package]]
name = "postgres-derive" name = "postgres-derive"
version = "0.4.1" version = "0.4.1"
@ -967,6 +1040,61 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "prost"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc03e116981ff7d8da8e5c220e374587b98d294af7ba7dd7fda761158f00086f"
dependencies = [
"bytes",
"prost-derive",
]
[[package]]
name = "prost-build"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65a1118354442de7feb8a2a76f3d80ef01426bd45542c8c1fdffca41a758f846"
dependencies = [
"bytes",
"cfg-if",
"cmake",
"heck",
"itertools",
"lazy_static",
"log",
"multimap",
"petgraph",
"prost",
"prost-types",
"regex",
"tempfile",
"which",
]
[[package]]
name = "prost-derive"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b670f45da57fb8542ebdbb6105a925fe571b67f9e7ed9f47a06a84e72b4e7cc"
dependencies = [
"anyhow",
"itertools",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "prost-types"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d0a014229361011dc8e69c8a1ec6c2e8d0f2af7c91e3ea3f5b2170298461e68"
dependencies = [
"bytes",
"prost",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.10" version = "1.0.10"
@ -1045,6 +1173,30 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "regex"
version = "1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "ring" name = "ring"
version = "0.16.20" version = "0.16.20"
@ -1324,6 +1476,20 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "tempfile"
version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4"
dependencies = [
"cfg-if",
"fastrand",
"libc",
"redox_syscall",
"remove_dir_all",
"winapi",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.30" version = "1.0.30"
@ -1831,6 +1997,17 @@ dependencies = [
"untrusted", "untrusted",
] ]
[[package]]
name = "which"
version = "4.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c4fb54e6113b6a8772ee41c3404fb0301ac79604489467e0a9ce1f3e97c24ae"
dependencies = [
"either",
"lazy_static",
"libc",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View File

@ -1,5 +1,5 @@
[package] [package]
name = "pluralkit" name = "gateway"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
@ -31,3 +31,5 @@ postgres-types = { version = "0.2", features = ["derive"] }
tokio-postgres = { version = "0.7", features = ["with-serde_json-1", "with-uuid-0_8"] } tokio-postgres = { version = "0.7", features = ["with-serde_json-1", "with-uuid-0_8"] }
redis = { version = "0.21.5", features = ["aio", "tokio-comp"] } redis = { version = "0.21.5", features = ["aio", "tokio-comp"] }
myriad = { path = "../myriad_rs" }

View File

@ -7,8 +7,6 @@ use tracing::info;
use twilight_gateway::Event; use twilight_gateway::Event;
use twilight_http::Client as HttpClient; use twilight_http::Client as HttpClient;
use crate::cache;
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref ALLOWED_EVENTS: Vec<&'static str> = [ static ref ALLOWED_EVENTS: Vec<&'static str> = [
"INTERACTION_CREATE", "INTERACTION_CREATE",
@ -27,7 +25,7 @@ pub async fn handle_event<'a>(
_db: Pool, _db: Pool,
rconn: redis::Client rconn: redis::Client
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
cache::handle_event(event.clone(), rconn.clone()).await?; myriad::cache::handle_event(event.clone(), rconn.clone()).await?;
match event { match event {
Event::GatewayInvalidateSession(resumable) => { Event::GatewayInvalidateSession(resumable) => {

View File

@ -9,7 +9,6 @@ use twilight_gateway::{
}; };
use twilight_http::Client as HttpClient; use twilight_http::Client as HttpClient;
mod cache;
mod config; mod config;
mod evt; mod evt;
mod db; mod db;

1489
myriad_rs/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

16
myriad_rs/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "myriad"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1"
prost = "0.10.3"
twilight-gateway = "0.10.0"
redis = { version = "0.21.5", features = ["aio", "tokio-comp"] }
[build-dependencies]
prost-build = "0.10.3"

6
myriad_rs/build.rs Normal file
View File

@ -0,0 +1,6 @@
use std::io::Result;
fn main() -> Result<()> {
prost_build::compile_protos(&["../proto/discord_cache.proto"], &["../proto/"])?;
Ok(())
}

View File

@ -1,11 +1,13 @@
use twilight_gateway::Event; use twilight_gateway::Event;
include!(concat!(env!("OUT_DIR"), "/myriad.cache.rs"));
pub async fn handle_event<'a>( pub async fn handle_event<'a>(
event: Event, event: Event,
rconn: redis::Client rconn: redis::Client
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
match event { match event {
// todo: SaveDMChannelStub (?) // todo: save private channels (see SaveDMChannelStub / PrivateChannelService)
// todo: save user profiles for some reason (from message create, etc) // todo: save user profiles for some reason (from message create, etc)
// todo(dotnet): remove relying on cache.OwnUserId // todo(dotnet): remove relying on cache.OwnUserId
@ -66,6 +68,9 @@ pub async fn handle_event<'a>(
Event::ThreadListSync(tls) => { Event::ThreadListSync(tls) => {
// save channels // save channels
} }
Event::MessageCreate(message) => {
// save last message of channel
}
_ => {} _ => {}
} }

1
myriad_rs/src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod cache;