Skip to main content

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