-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver_events.rs
59 lines (49 loc) · 1.91 KB
/
server_events.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::sync::atomic::{AtomicI64, Ordering};
use valkey_module::alloc::ValkeyAlloc;
use valkey_module::{
server_events::FlushSubevent, valkey_module, Context, ValkeyResult, ValkeyString, ValkeyValue,
};
use valkey_module_macros::{config_changed_event_handler, cron_event_handler, flush_event_handler};
static NUM_FLUSHES: AtomicI64 = AtomicI64::new(0);
static NUM_CRONS: AtomicI64 = AtomicI64::new(0);
static NUM_MAX_MEMORY_CONFIGURATION_CHANGES: AtomicI64 = AtomicI64::new(0);
#[flush_event_handler]
fn flushed_event_handler(_ctx: &Context, flush_event: FlushSubevent) {
if let FlushSubevent::Started = flush_event {
NUM_FLUSHES.fetch_add(1, Ordering::SeqCst);
}
}
#[config_changed_event_handler]
fn config_changed_event_handler(_ctx: &Context, changed_configs: &[&str]) {
changed_configs
.iter()
.find(|v| **v == "maxmemory")
.map(|_| NUM_MAX_MEMORY_CONFIGURATION_CHANGES.fetch_add(1, Ordering::SeqCst));
}
#[cron_event_handler]
fn cron_event_handler(_ctx: &Context, _hz: u64) {
NUM_CRONS.fetch_add(1, Ordering::SeqCst);
}
fn num_flushed(_ctx: &Context, _args: Vec<ValkeyString>) -> ValkeyResult {
Ok(ValkeyValue::Integer(NUM_FLUSHES.load(Ordering::SeqCst)))
}
fn num_crons(_ctx: &Context, _args: Vec<ValkeyString>) -> ValkeyResult {
Ok(ValkeyValue::Integer(NUM_CRONS.load(Ordering::SeqCst)))
}
fn num_maxmemory_changes(_ctx: &Context, _args: Vec<ValkeyString>) -> ValkeyResult {
Ok(ValkeyValue::Integer(
NUM_MAX_MEMORY_CONFIGURATION_CHANGES.load(Ordering::SeqCst),
))
}
//////////////////////////////////////////////////////
valkey_module! {
name: "srv_events",
version: 1,
allocator: (ValkeyAlloc, ValkeyAlloc),
data_types: [],
commands: [
["num_flushed", num_flushed, "readonly", 0, 0, 0],
["num_max_memory_changes", num_maxmemory_changes, "readonly", 0, 0, 0],
["num_crons", num_crons, "readonly", 0, 0, 0],
],
}