valkey_module/
lib.rs

1pub use crate::context::InfoContext;
2extern crate num_traits;
3
4pub mod alloc;
5pub mod apierror;
6pub mod digest;
7pub mod error;
8pub mod native_types;
9pub mod raw;
10pub mod rediserror;
11mod redismodule;
12pub mod redisraw;
13pub mod redisvalue;
14pub mod stream;
15
16pub mod configuration;
17mod context;
18pub mod key;
19pub mod logging;
20mod macros;
21mod utils;
22
23pub use crate::context::blocked::BlockedClient;
24pub use crate::context::thread_safe::{
25    ContextGuard, DetachedFromClient, ThreadSafeContext, ValkeyGILGuard, ValkeyLockIndicator,
26};
27pub use crate::raw::NotifyEvent;
28
29pub use crate::configuration::ConfigurationValue;
30pub use crate::configuration::EnumConfigurationValue;
31pub use crate::context::call_reply::FutureCallReply;
32pub use crate::context::call_reply::{CallReply, CallResult, ErrorReply, PromiseCallReply};
33pub use crate::context::commands;
34pub use crate::context::keys_cursor::KeysCursor;
35pub use crate::context::server_events;
36pub use crate::context::AclPermissions;
37#[cfg(all(any(
38    feature = "min-valkey-compatibility-version-8-0",
39    feature = "min-redis-compatibility-version-7-2"
40)))]
41pub use crate::context::BlockingCallOptions;
42pub use crate::context::CallOptionResp;
43pub use crate::context::CallOptions;
44pub use crate::context::CallOptionsBuilder;
45pub use crate::context::Context;
46pub use crate::context::ContextFlags;
47pub use crate::context::DetachedContext;
48pub use crate::context::DetachedContextGuard;
49pub use crate::context::{
50    InfoContextBuilderFieldBottomLevelValue, InfoContextBuilderFieldTopLevelValue,
51    InfoContextFieldBottomLevelData, InfoContextFieldTopLevelData, OneInfoSectionData,
52};
53pub use crate::raw::*;
54pub use crate::redismodule::*;
55use backtrace::Backtrace;
56use context::server_events::INFO_COMMAND_HANDLER_LIST;
57
58/// The detached Valkey module context (the context of this module). It
59/// is only set to a proper value after the module is initialised via the
60/// provided [redis_module] macro.
61/// See [DetachedContext].
62pub static MODULE_CONTEXT: DetachedContext = DetachedContext::new();
63
64#[deprecated(
65    since = "2.1.0",
66    note = "Please use the valkey_module::logging::ValkeyLogLevel directly instead."
67)]
68pub type LogLevel = logging::ValkeyLogLevel;
69
70fn add_trace_info(ctx: &InfoContext) -> ValkeyResult<()> {
71    const SECTION_NAME: &str = "trace";
72    const FIELD_NAME: &str = "backtrace";
73
74    let current_backtrace = Backtrace::new();
75    let trace = format!("{current_backtrace:?}");
76
77    ctx.builder()
78        .add_section(SECTION_NAME)
79        .field(FIELD_NAME, trace)?
80        .build_section()?
81        .build_info()?;
82
83    Ok(())
84}
85
86/// A type alias for the custom info command handler.
87/// The function may optionally return an object of one section to add.
88/// If nothing is returned, it is assumed that the function has already
89/// filled all the information required via [`InfoContext::builder`].
90pub type InfoHandlerFunctionType = fn(&InfoContext, bool) -> ValkeyResult<()>;
91
92/// Default "INFO" command handler for the module.
93///
94/// This function can be invoked, for example, by sending `INFO modules`
95/// through the RESP protocol.
96pub fn basic_info_command_handler(ctx: &InfoContext, for_crash_report: bool) {
97    if for_crash_report {
98        if let Err(e) = add_trace_info(ctx) {
99            log::error!("Couldn't send info for the module: {e}");
100            return;
101        }
102    }
103
104    INFO_COMMAND_HANDLER_LIST
105        .iter()
106        .filter_map(|callback| callback(ctx, for_crash_report).err())
107        .for_each(|e| log::error!("Couldn't build info for the module's custom handler: {e}"));
108}
109
110/// Initialize RedisModuleAPI without register as a module.
111pub fn init_api(ctx: &Context) {
112    unsafe { crate::raw::Export_RedisModule_InitAPI(ctx.ctx) };
113}
114
115pub(crate) unsafe fn deallocate_pointer<P>(p: *mut P) {
116    std::ptr::drop_in_place(p);
117    std::alloc::dealloc(p as *mut u8, std::alloc::Layout::new::<P>());
118}