Skip to main content

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