Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace redis-rs with fred #535

Merged
merged 16 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
714 changes: 358 additions & 356 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ members = [
"lib/masto-id-convert",
"lib/mrf-manifest",
"lib/mrf-tool",
"lib/multiplex-pool",
"lib/post-process",
"lib/speedy-uuid",
"lib/tick-tock-mock",
Expand All @@ -75,6 +74,14 @@ members = [
]
resolver = "2"

[workspace.dependencies]
fred = { version = "9.0.3", features = [
"enable-rustls-ring",
"i-scripts",
"partial-tracing",
"sha-1",
] }

[workspace.lints.clippy]
all = "warn"
pedantic = "warn"
Expand Down Expand Up @@ -126,13 +133,10 @@ diesel-async = { git = "https://github.com/weiznich/diesel_async.git", rev = "d0
scraper = { git = "https://github.com/causal-agent/scraper.git", rev = "d67111f5cc0b7da6e6ff10e4549d87cf09ba3e5b" }
tokio-postgres-rustls = { git = "https://github.com/jbg/tokio-postgres-rustls.git", rev = "b16c1bc0f5d4f91324174fd1bd839d743a70f86a" }

# Support XAUTOCLAIM
redis = { git = "https://github.com/redis-rs/redis-rs.git", rev = "0c544b548b52180acda8e8394ab21b2761497e50" }

# Patch to make OpenTelemetry with with hyper 1
opentelemetry = { git = "https://github.com/open-telemetry/opentelemetry-rust.git", rev = "b44cb130e4a102b0d676289e91c003f4b1008d08" }
opentelemetry-http = { git = "https://github.com/open-telemetry/opentelemetry-rust.git", rev = "b44cb130e4a102b0d676289e91c003f4b1008d08" }
opentelemetry-otlp = { git = "https://github.com/open-telemetry/opentelemetry-rust.git", rev = "b44cb130e4a102b0d676289e91c003f4b1008d08" }
opentelemetry_sdk = { git = "https://github.com/open-telemetry/opentelemetry-rust.git", rev = "b44cb130e4a102b0d676289e91c003f4b1008d08" }
opentelemetry = { git = "https://github.com/open-telemetry/opentelemetry-rust.git", rev = "40fb924215c84d6e3818351b2c0e3d2ba01a7db9" }
opentelemetry-http = { git = "https://github.com/open-telemetry/opentelemetry-rust.git", rev = "40fb924215c84d6e3818351b2c0e3d2ba01a7db9" }
opentelemetry-otlp = { git = "https://github.com/open-telemetry/opentelemetry-rust.git", rev = "40fb924215c84d6e3818351b2c0e3d2ba01a7db9" }
opentelemetry_sdk = { git = "https://github.com/open-telemetry/opentelemetry-rust.git", rev = "40fb924215c84d6e3818351b2c0e3d2ba01a7db9" }
tonic = { git = "https://github.com/hyperium/tonic.git", rev = "9b306af386528f11dbd022bc372d367adc4e96b5" }
tracing-opentelemetry = { git = "https://github.com/aumetra/tracing-opentelemetry.git", branch = "v0.1.x-http1" }
2 changes: 1 addition & 1 deletion crates/kitsune-activitypub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ kitsune-util = { path = "../kitsune-util" }
kitsune-wasm-mrf = { path = "../kitsune-wasm-mrf" }
mime = "0.3.17"
mime_guess = { version = "2.0.4", default-features = false }
serde = "1.0.201"
serde = "1.0.202"
sha2 = "0.10.8"
simd-json = "0.13.10"
speedy-uuid = { path = "../../lib/speedy-uuid" }
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-activitypub/src/fetcher/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use kitsune_error::Result;

// Maximum call depth of fetching new posts. Prevents unbounded recursion.
// Setting this to >=40 would cause the `fetch_infinitely_long_reply_chain` test to run into stack overflow
pub const MAX_FETCH_DEPTH: u32 = 15;
pub const MAX_FETCH_DEPTH: u32 = 10;

impl Fetcher {
#[instrument(skip(self))]
Expand Down
8 changes: 2 additions & 6 deletions crates/kitsune-cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ license.workspace = true

[dependencies]
enum_dispatch = "0.3.13"
fred = { workspace = true }
kitsune-error = { path = "../kitsune-error" }
moka = { version = "0.12.7", features = ["sync"] }
multiplex-pool = { path = "../../lib/multiplex-pool" }
redis = { version = "0.25.3", default-features = false, features = [
"connection-manager",
"tokio-comp",
] }
serde = "1.0.201"
serde = "1.0.202"
simd-json = "0.13.10"
tracing = "0.1.40"
triomphe = "0.1.11"
Expand Down
27 changes: 14 additions & 13 deletions crates/kitsune-cache/src/redis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::CacheBackend;
use fred::{clients::RedisPool, interfaces::KeysInterface, types::Expiration};
use kitsune_error::Result;
use redis::{aio::ConnectionManager, AsyncCommands};
use serde::{de::DeserializeOwned, Serialize};
use std::{fmt::Display, marker::PhantomData, time::Duration};
use typed_builder::TypedBuilder;
Expand All @@ -14,7 +14,7 @@ where
namespace: String,
#[builder(setter(into))]
prefix: String,
redis_conn: multiplex_pool::Pool<ConnectionManager>,
redis_conn: RedisPool,
ttl: Duration,

// Type phantom data
Expand All @@ -28,11 +28,7 @@ impl<K, V> Redis<K, V>
where
K: ?Sized,
{
pub fn new<P>(
redis_conn: multiplex_pool::Pool<ConnectionManager>,
prefix: P,
ttl: Duration,
) -> Self
pub fn new<P>(redis_conn: RedisPool, prefix: P, ttl: Duration) -> Self
where
P: Into<String>,
{
Expand All @@ -55,22 +51,20 @@ where
{
#[instrument(skip_all, fields(%key))]
async fn delete(&self, key: &K) -> Result<()> {
let mut conn = self.redis_conn.get();
let key = self.compute_key(key);

debug!(%key, "Deleting cache entry");
conn.del(key).await?;
self.redis_conn.del(key).await?;

Ok(())
}

#[instrument(skip_all, fields(%key))]
async fn get(&self, key: &K) -> Result<Option<V>> {
let mut conn = self.redis_conn.get();
let key = self.compute_key(key);

debug!(%key, "Fetching cache entry");
if let Some(serialised) = conn.get::<_, Option<String>>(&key).await? {
if let Some(serialised) = self.redis_conn.get::<Option<String>, _>(&key).await? {
let mut serialised_bytes = serialised.into_bytes();
let deserialised = simd_json::from_slice(&mut serialised_bytes)?;
Ok(Some(deserialised))
Expand All @@ -81,12 +75,19 @@ where

#[instrument(skip_all, fields(%key))]
async fn set(&self, key: &K, value: &V) -> Result<()> {
let mut conn = self.redis_conn.get();
let key = self.compute_key(key);
let serialised = simd_json::to_string(value)?;

debug!(%key, ttl = ?self.ttl, "Setting cache entry");
conn.set_ex(key, serialised, self.ttl.as_secs()).await?;
self.redis_conn
.set(
key,
serialised,
Some(Expiration::EX(self.ttl.as_secs() as i64)),
None,
false,
)
.await?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-captcha/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum_dispatch = "0.3.13"
http = "1.1.0"
kitsune-error = { path = "../kitsune-error" }
kitsune-http-client = { path = "../kitsune-http-client" }
serde = { version = "1.0.201", features = ["derive"] }
serde = { version = "1.0.202", features = ["derive"] }
serde_urlencoded = "0.7.1"
simd-json = "0.13.10"
strum = { version = "0.26.2", features = ["derive"] }
Expand Down
6 changes: 3 additions & 3 deletions crates/kitsune-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ license.workspace = true
eyre = "0.6.12"
human-size = { version = "0.4.3", features = ["serde"] }
isolang = { version = "2.4.0", features = ["serde"] }
serde = { version = "1.0.201", features = ["derive"] }
smol_str = { version = "0.2.1", features = ["serde"] }
serde = { version = "1.0.202", features = ["derive"] }
smol_str = { version = "0.2.2", features = ["serde"] }
tokio = { version = "1.37.0", features = ["fs"] }
toml = { version = "0.8.12", default-features = false, features = ["parse"] }
toml = { version = "0.8.13", default-features = false, features = ["parse"] }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion crates/kitsune-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const_format = "0.2.32"
kitsune-db = { path = "../kitsune-db" }
kitsune-error = { path = "../kitsune-error" }
paste = "1.0.15"
serde = { version = "1.0.201", features = ["derive"] }
serde = { version = "1.0.202", features = ["derive"] }
triomphe = { version = "0.1.11", features = ["unsize"] }
typed-builder = "0.18.2"
unsize = "1.1.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/kitsune-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ kitsune-language = { path = "../kitsune-language" }
kitsune-type = { path = "../kitsune-type" }
num-derive = "0.4.2"
num-traits = "0.2.19"
rustls = { version = "0.23.5", default-features = false, features = [
rustls = { version = "0.23.7", default-features = false, features = [
"logging",
"ring",
"std",
"tls12",
] }
rustls-native-certs = "0.7.0"
serde = { version = "1.0.201", features = ["derive"] }
serde = { version = "1.0.202", features = ["derive"] }
simd-json = "0.13.10"
speedy-uuid = { path = "../../lib/speedy-uuid", features = ["diesel"] }
tokio = { version = "1.37.0", features = ["rt"] }
Expand Down
4 changes: 2 additions & 2 deletions crates/kitsune-derive/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ license.workspace = true
proc-macro = true

[dependencies]
proc-macro2 = "1.0.82"
proc-macro2 = "1.0.83"
quote = "1.0.36"
syn = { version = "2.0.63", features = ["full"] }
syn = { version = "2.0.65", features = ["full"] }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion crates/kitsune-embed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ kitsune-error = { path = "../kitsune-error" }
kitsune-http-client = { path = "../kitsune-http-client" }
once_cell = "1.19.0"
scraper = { version = "0.19.0", default-features = false }
smol_str = "0.2.1"
smol_str = "0.2.2"

[lints]
workspace = true
2 changes: 1 addition & 1 deletion crates/kitsune-http-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ hyper-rustls = { version = "0.27.1", default-features = false, features = [
] }
kitsune-type = { path = "../kitsune-type" }
pin-project = "1.1.5"
serde = "1.0.201"
serde = "1.0.202"
simdutf8 = { version = "0.1.4", features = ["aarch64_neon"] }
simd-json = "0.13.10"
tower = { version = "0.4.13", features = ["util"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-jobs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ kitsune-core = { path = "../kitsune-core" }
kitsune-db = { path = "../kitsune-db" }
kitsune-email = { path = "../kitsune-email" }
kitsune-error = { path = "../kitsune-error" }
serde = { version = "1.0.201", features = ["derive"] }
serde = { version = "1.0.202", features = ["derive"] }
speedy-uuid = { path = "../../lib/speedy-uuid" }
tracing = "0.1.40"
typed-builder = "0.18.2"
Expand Down
4 changes: 2 additions & 2 deletions crates/kitsune-mastodon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ kitsune-type = { path = "../kitsune-type" }
kitsune-url = { path = "../kitsune-url" }
kitsune-util = { path = "../kitsune-util" }
mime = "0.3.17"
serde = "1.0.201"
serde = "1.0.202"
simd-json = "0.13.10"
smol_str = "0.2.1"
smol_str = "0.2.2"
speedy-uuid = { path = "../../lib/speedy-uuid" }
tracing = "0.1.40"
typed-builder = "0.18.2"
Expand Down
8 changes: 4 additions & 4 deletions crates/kitsune-observability/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ http-body-util = "0.1.1"
hyper = { version = "1.3.1", default-features = false }
kitsune-config = { path = "../kitsune-config" }
kitsune-http-client = { path = "../kitsune-http-client" }
opentelemetry = { version = "0.22.0", default-features = false, features = [
opentelemetry = { version = "0.23.0", default-features = false, features = [
"trace",
] }
opentelemetry-http = "0.11.1"
opentelemetry-otlp = { version = "0.15.0", default-features = false, features = [
opentelemetry-http = "0.12.0"
opentelemetry-otlp = { version = "0.16.0", default-features = false, features = [
"grpc-tonic",
"http-json",
"http-proto",
"tls",
"tls-roots",
"trace",
] }
opentelemetry_sdk = { version = "0.22.1", default-features = false, features = [
opentelemetry_sdk = { version = "0.23.0", default-features = false, features = [
"rt-tokio",
] }
tracing = "0.1.40"
Expand Down
8 changes: 2 additions & 6 deletions crates/kitsune-oidc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@ license.workspace = true

[dependencies]
enum_dispatch = "0.3.13"
fred = { workspace = true }
http = "1.1.0"
http-body-util = "0.1.1"
kitsune-config = { path = "../kitsune-config" }
kitsune-derive = { path = "../kitsune-derive" }
kitsune-error = { path = "../kitsune-error" }
kitsune-http-client = { path = "../kitsune-http-client" }
moka = { version = "0.12.7", features = ["sync"] }
multiplex-pool = { path = "../../lib/multiplex-pool" }
oauth2 = { version = "5.0.0-alpha.4", default-features = false }
once_cell = "1.19.0"
openidconnect = { version = "4.0.0-alpha.1", default-features = false, features = [
# Accept these two, per specification invalid, cases to increase compatibility
"accept-rfc3339-timestamps",
"accept-string-booleans",
] }
redis = { version = "0.25.3", default-features = false, features = [
"connection-manager",
"tokio-comp",
] }
serde = { version = "1.0.201", features = ["derive"] }
serde = { version = "1.0.202", features = ["derive"] }
simd-json = "0.13.10"
speedy-uuid = { path = "../../lib/speedy-uuid", features = ["serde"] }
url = "2.5.0"
Expand Down
12 changes: 4 additions & 8 deletions crates/kitsune-oidc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::state::{
store::{InMemory as InMemoryStore, Redis as RedisStore},
LoginState, OAuth2LoginState, Store,
};
use fred::{clients::RedisPool, types::RedisConfig};
use kitsune_config::oidc::{Configuration, StoreConfiguration};
use kitsune_derive::kitsune_service;
use kitsune_error::{bail, kitsune_error, Result};
use multiplex_pool::RoundRobinStrategy;
use openidconnect::{
core::{CoreAuthenticationFlow, CoreClient, CoreProviderMetadata},
AccessTokenHash, AuthorizationCode, ClientId, ClientSecret, CsrfToken, IssuerUrl, Nonce,
Expand Down Expand Up @@ -92,13 +92,9 @@ impl OidcService {
let login_state_store = match config.store {
StoreConfiguration::InMemory => InMemoryStore::new(LOGIN_STATE_STORE_SIZE).into(),
StoreConfiguration::Redis(ref redis_config) => {
let client = redis::Client::open(redis_config.url.as_str())?;
let pool = multiplex_pool::Pool::from_producer(
|| client.get_connection_manager(),
10,
RoundRobinStrategy::default(),
)
.await?;
let config = RedisConfig::from_url(redis_config.url.as_str())?;
// TODO: Make pool size configurable
let pool = RedisPool::new(config, None, None, None, 10)?;

RedisStore::new(pool).into()
}
Expand Down
14 changes: 7 additions & 7 deletions crates/kitsune-oidc/src/state/store/redis.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use super::Store;
use crate::state::LoginState;
use fred::{clients::RedisPool, interfaces::KeysInterface};
use kitsune_error::Result;
use redis::{aio::ConnectionManager, AsyncCommands};

const REDIS_PREFIX: &str = "OIDC-LOGIN-STATE";

#[derive(Clone)]
pub struct Redis {
pool: multiplex_pool::Pool<ConnectionManager>,
pool: RedisPool,
}

impl Redis {
pub fn new(pool: multiplex_pool::Pool<ConnectionManager>) -> Self {
pub fn new(pool: RedisPool) -> Self {
Self { pool }
}

Expand All @@ -23,17 +23,17 @@ impl Redis {

impl Store for Redis {
async fn get_and_remove(&self, key: &str) -> Result<LoginState> {
let mut conn = self.pool.get();
let raw_value: String = conn.get_del(Self::format_key(key)).await?;
let raw_value: String = self.pool.getdel(Self::format_key(key)).await?;

let mut raw_value = raw_value.into_bytes();
Ok(simd_json::from_slice(&mut raw_value)?)
}

async fn set(&self, key: &str, value: LoginState) -> Result<()> {
let raw_value = simd_json::to_string(&value)?;
let mut conn = self.pool.get();
conn.set(Self::format_key(key), raw_value).await?;
self.pool
.set(Self::format_key(key), raw_value, None, None, false)
.await?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-s3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ kitsune-error = { path = "../kitsune-error" }
kitsune-http-client = { path = "../kitsune-http-client" }
quick-xml = { version = "0.31.0", features = ["serialize"] }
rusty-s3 = "0.5.0"
serde = { version = "1.0.201", features = ["derive"] }
serde = { version = "1.0.202", features = ["derive"] }
typed-builder = "0.18.2"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-scss-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license.workspace = true
[dependencies]
eyre = "0.6.12"
glob = "0.3.1"
grass_compiler = "0.13.2"
grass_compiler = "0.13.3"
tracing = { version = "0.1.40", default-features = false }

[lints]
Expand Down
Loading
Loading