-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
801 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::sync::Arc; | ||
|
||
use redis::{from_owned_redis_value, FromRedisValue, ToRedisArgs}; | ||
|
||
use crate::log::record_exception; | ||
|
||
/// A listener to receive messages from a redis channel via pubsub. | ||
pub struct RedisChannelListener<T> { | ||
pub(crate) on_drop_tx: Arc<tokio::sync::mpsc::UnboundedSender<(String, u64)>>, | ||
pub(crate) key: u64, | ||
pub(crate) channel: String, | ||
pub(crate) rx: tokio::sync::mpsc::UnboundedReceiver<redis::Value>, | ||
pub(crate) _t: std::marker::PhantomData<T>, | ||
} | ||
|
||
impl<T: ToRedisArgs + FromRedisValue> RedisChannelListener<T> { | ||
/// Get a new message from the channel. | ||
/// The outer None indicates the channel has been closed erroneously, or the internal data could not be coerced to the target type. | ||
/// In either case, something's gone wrong, an exception will probably have been recorded too. | ||
pub async fn recv(&mut self) -> Option<T> { | ||
if let Some(v) = self.rx.recv().await { | ||
match from_owned_redis_value(v) { | ||
Ok(v) => Some(v), | ||
Err(e) => { | ||
record_exception( | ||
format!( | ||
"Failed to convert redis value to target type '{}'", | ||
std::any::type_name::<T>() | ||
), | ||
format!("{:?}", e), | ||
); | ||
None | ||
} | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
/// Tell the global pubsub manager this listener is being dropped. | ||
impl<T> Drop for RedisChannelListener<T> { | ||
fn drop(&mut self) { | ||
let _ = self.on_drop_tx.send((self.channel.clone(), self.key)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod channel_listener; | ||
pub(crate) mod pubsub_global; | ||
|
||
pub use channel_listener::*; |
Oops, something went wrong.