valkey_module/
lib.rs

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