Skip to content

Commit

Permalink
Log the unsupported event notification flags when registering the mod…
Browse files Browse the repository at this point in the history
…ule's subscribers
  • Loading branch information
iddm committed Dec 5, 2023
1 parent f152f91 commit ab5902f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::ptr::NonNull;
use std::sync::atomic::{AtomicI64, Ordering};

static NUM_KEY_MISSES: AtomicI64 = AtomicI64::new(0);
static NUM_KEYS: AtomicI64 = AtomicI64::new(0);

fn on_event(ctx: &Context, event_type: NotifyEvent, event: &str, key: &[u8]) {
if key == b"num_sets" {
Expand Down Expand Up @@ -52,6 +53,13 @@ fn num_key_miss(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValue::Integer(NUM_KEY_MISSES.load(Ordering::SeqCst)))
}

fn on_new_key(_ctx: &Context, _event_type: NotifyEvent, _event: &str, _key: &[u8]) {
NUM_KEYS.fetch_add(1, Ordering::SeqCst);
}

fn num_keys(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValue::Integer(NUM_KEYS.load(Ordering::SeqCst)))
}
//////////////////////////////////////////////////////

redis_module! {
Expand All @@ -62,10 +70,12 @@ redis_module! {
commands: [
["events.send", event_send, "", 0, 0, 0],
["events.num_key_miss", num_key_miss, "", 0, 0, 0],
["events.num_keys", num_keys, "", 0, 0, 0],
],
event_handlers: [
[@STRING: on_event],
[@STREAM: on_stream],
[@MISSED: on_key_miss],
[@NEW: on_new_key],
],
}
10 changes: 10 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ macro_rules! redis_event_handler {
$crate::raw::Status::Ok as c_int
}

let all_available_flags = $crate::raw::get_keyspace_notification_flags_all();
if !all_available_flags.contains($event_type) {
let not_supported = $event_type.difference(all_available_flags);
log::warn!("The event notification flags set aren't supported: {not_supported:?}");

// $crate::Context::new($ctx).log_warning(&format!(
// "The event notification flags set aren't supported: {not_supported:?}"
// ));
}

if unsafe {
$crate::raw::RedisModule_SubscribeToKeyspaceEvents.unwrap()(
$ctx,
Expand Down
18 changes: 18 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,24 @@ pub fn get_keyspace_events() -> NotifyEvent {
}
}

/// Returns all the available notification flags for key-space
/// notifications.
///
/// # Safety
///
/// This function is safe to use as it doesn't perform any work with
/// the [RedisModuleCtx] pointer except for passing it to the redis server.
///
/// # Panics
///
/// Panics when the [RedisModule_GetKeyspaceNotificationFlagsAll] is
/// unavailable.
pub fn get_keyspace_notification_flags_all() -> NotifyEvent {
unsafe {
NotifyEvent::from_bits_truncate(RedisModule_GetKeyspaceNotificationFlagsAll.unwrap()())
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version {
pub major: i32,
Expand Down

0 comments on commit ab5902f

Please sign in to comment.