Skip to content

Commit cb6ed98

Browse files
authored
Merge pull request #627 from nextcloud/redis-pubsub-ping
send keepalive pings over redis pubsub connection
2 parents c2a0e96 + 0461f40 commit cb6ed98

4 files changed

Lines changed: 55 additions & 20 deletions

File tree

Cargo.lock

Lines changed: 30 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2021"
88
rust-version = "1.81.0"
99

1010
[dependencies]
11-
redis = { version = "0.30.0", default-features = false, features = ["tokio-comp", "aio", "cluster", "cluster-async", "keep-alive", "tls-rustls", "tokio-rustls-comp", "tls-rustls-webpki-roots", "tls-rustls-insecure"] }
11+
redis = { version = "0.31.0", default-features = false, features = ["tokio-comp", "aio", "cluster", "cluster-async", "keep-alive", "tls-rustls", "tokio-rustls-comp", "tls-rustls-webpki-roots", "tls-rustls-insecure"] }
1212
serde = { version = "1.0.217", features = ["derive"] }
1313
serde_json = "1.0.139"
1414
thiserror = "2.0.11"
@@ -24,7 +24,7 @@ miette = { version = "7.4.0", features = ["fancy"] }
2424
smallvec = { version = "1.13.2", features = ["serde"] }
2525
reqwest = { version = "0.12.12", default-features = false, features = ["rustls-tls", "rustls-tls-native-roots", "json"] }
2626
warp-real-ip = "0.2.0"
27-
parse-display = "0.9.1"
27+
parse-display = "0.10.0"
2828
rand = { version = "0.8.5", features = ["small_rng"] }
2929
ahash = "0.8.11"
3030
flexi_logger = { version = "0.29.8", features = ["colors"] }

src/event.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use crate::metrics::METRICS;
77
use crate::{Redis, Result, UserId};
88
use parse_display::Display;
9+
use redis::aio::PubSubSink;
910
use redis::Msg;
1011
use serde::Deserialize;
1112
use serde_json::Value;
@@ -153,7 +154,10 @@ impl TryFrom<Msg> for Event {
153154

154155
pub async fn subscribe(
155156
client: &Redis,
156-
) -> Result<impl Stream<Item = Result<Event, MessageDecodeError>>> {
157+
) -> Result<(
158+
PubSubSink,
159+
impl Stream<Item = Result<Event, MessageDecodeError>>,
160+
)> {
157161
let mut pubsub = client.pubsub().await?;
158162
let channels = [
159163
"notify_storage_update",
@@ -172,8 +176,12 @@ pub async fn subscribe(
172176
pubsub.subscribe(*channel).await?;
173177
}
174178

175-
Ok(pubsub.into_on_message().map(|event| {
176-
METRICS.add_event();
177-
Event::try_from(event)
178-
}))
179+
let (sink, stream) = pubsub.split();
180+
Ok((
181+
sink,
182+
stream.map(|event| {
183+
METRICS.add_event();
184+
Event::try_from(event)
185+
}),
186+
))
179187
}

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ pub async fn listen_loop(app: Arc<App>, cancel: oneshot::Receiver<()>) {
418418
}
419419

420420
pub async fn listen(app: Arc<App>) -> Result<()> {
421-
let mut event_stream = event::subscribe(&app.redis).await?;
421+
let (mut pubsub_sink, mut event_stream) = event::subscribe(&app.redis).await?;
422422

423423
let handle = move |event: Event| {
424424
// todo: any way to do this without cloning the arc every event (scoped?)
@@ -428,6 +428,13 @@ pub async fn listen(app: Arc<App>) -> Result<()> {
428428
}
429429
};
430430

431+
let ping_handle = tokio::spawn(async move {
432+
loop {
433+
sleep(Duration::from_secs(15)).await;
434+
let _ = pubsub_sink.ping::<()>().await;
435+
}
436+
});
437+
431438
while let Some(event) = event_stream.next().await {
432439
match event {
433440
Ok(event) => {
@@ -440,5 +447,7 @@ pub async fn listen(app: Arc<App>) -> Result<()> {
440447
Err(e) => log::warn!("{e:#}"),
441448
}
442449
}
450+
451+
ping_handle.abort();
443452
Ok(())
444453
}

0 commit comments

Comments
 (0)