From 2ead7246338c89aa7f208f304ee2b73f0cb623c5 Mon Sep 17 00:00:00 2001 From: Karthik Subbarao Date: Fri, 17 Jan 2025 18:13:33 -0800 Subject: [PATCH] Add support for MustObeyClient Module API Signed-off-by: KarthikSubbarao --- src/module.c | 9 +++++++++ src/valkeymodule.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/module.c b/src/module.c index 75dcd81cd6..9f4cbc8c48 100644 --- a/src/module.c +++ b/src/module.c @@ -3719,6 +3719,14 @@ ValkeyModuleString *VM_GetClientUserNameById(ValkeyModuleCtx *ctx, uint64_t id) return str; } +/* Returns 1 if commands are arriving from the primary client or AOF client + * and should never be rejected. + * Returns 0 otherwise (and also if ctx or if the client is NULL). */ +int VM_MustObeyClient(ValkeyModuleCtx *ctx) { + if (!ctx || !ctx->client) return 0; + return mustObeyClient(ctx->client); +} + /* This is a helper for VM_GetClientInfoById() and other functions: given * a client, it populates the client info structure with the appropriate * fields depending on the version provided. If the version is not valid @@ -13839,6 +13847,7 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(ChannelAtPosWithFlags); REGISTER_API(GetClientId); REGISTER_API(GetClientUserNameById); + REGISTER_API(MustObeyClient); REGISTER_API(GetContextFlags); REGISTER_API(AvoidReplicaTraffic); REGISTER_API(PoolAlloc); diff --git a/src/valkeymodule.h b/src/valkeymodule.h index c501b373fd..93f12a3027 100644 --- a/src/valkeymodule.h +++ b/src/valkeymodule.h @@ -1321,6 +1321,7 @@ VALKEYMODULE_API int (*ValkeyModule_GetClientInfoById)(void *ci, uint64_t id) VA VALKEYMODULE_API ValkeyModuleString *(*ValkeyModule_GetClientNameById)(ValkeyModuleCtx *ctx, uint64_t id)VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_SetClientNameById)(uint64_t id, ValkeyModuleString *name) VALKEYMODULE_ATTR; +VALKEYMODULE_API int *(*ValkeyModule_MustObeyClient)(ValkeyModuleCtx *ctx)VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_PublishMessage)(ValkeyModuleCtx *ctx, ValkeyModuleString *channel, ValkeyModuleString *message) VALKEYMODULE_ATTR;