valkey_module/
lib.rs

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