Skip to main content

valkey_module/
lib.rs

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