redis_module/
lib.rs

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