2024-04-23 08:55:44 -07:00
|
|
|
#include "valkeymodule.h"
|
2022-04-17 14:43:22 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define UNUSED(V) ((void) V)
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int cmd_publish_classic_multi(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
|
Fix broken protocol when PUBLISH emits local push inside MULTI (#12326)
When a connection that's subscribe to a channel emits PUBLISH inside MULTI-EXEC,
the push notification messes up the EXEC response.
e.g. MULTI, PING, PUSH foo bar, PING, EXEC
the EXEC's response will contain: PONG, {message foo bar}, 1. and the second PONG
will be delivered outside the EXEC's response.
Additionally, this PR changes the order of responses in case of a plain PUBLISH (when
the current client also subscribed to it), by delivering the push after the command's
response instead of before it.
This also affects modules calling RM_PublishMessage in a similar way, so that we don't
run the risk of getting that push mixed together with the module command's response.
2023-06-20 18:41:41 +01:00
|
|
|
{
|
|
|
|
if (argc < 3)
|
2024-04-23 08:55:44 -07:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
|
|
|
ValkeyModule_ReplyWithArray(ctx, argc-2);
|
Fix broken protocol when PUBLISH emits local push inside MULTI (#12326)
When a connection that's subscribe to a channel emits PUBLISH inside MULTI-EXEC,
the push notification messes up the EXEC response.
e.g. MULTI, PING, PUSH foo bar, PING, EXEC
the EXEC's response will contain: PONG, {message foo bar}, 1. and the second PONG
will be delivered outside the EXEC's response.
Additionally, this PR changes the order of responses in case of a plain PUBLISH (when
the current client also subscribed to it), by delivering the push after the command's
response instead of before it.
This also affects modules calling RM_PublishMessage in a similar way, so that we don't
run the risk of getting that push mixed together with the module command's response.
2023-06-20 18:41:41 +01:00
|
|
|
for (int i = 2; i < argc; i++) {
|
2024-04-23 08:55:44 -07:00
|
|
|
int receivers = ValkeyModule_PublishMessage(ctx, argv[1], argv[i]);
|
|
|
|
ValkeyModule_ReplyWithLongLong(ctx, receivers);
|
Fix broken protocol when PUBLISH emits local push inside MULTI (#12326)
When a connection that's subscribe to a channel emits PUBLISH inside MULTI-EXEC,
the push notification messes up the EXEC response.
e.g. MULTI, PING, PUSH foo bar, PING, EXEC
the EXEC's response will contain: PONG, {message foo bar}, 1. and the second PONG
will be delivered outside the EXEC's response.
Additionally, this PR changes the order of responses in case of a plain PUBLISH (when
the current client also subscribed to it), by delivering the push after the command's
response instead of before it.
This also affects modules calling RM_PublishMessage in a similar way, so that we don't
run the risk of getting that push mixed together with the module command's response.
2023-06-20 18:41:41 +01:00
|
|
|
}
|
2024-04-23 08:55:44 -07:00
|
|
|
return VALKEYMODULE_OK;
|
Fix broken protocol when PUBLISH emits local push inside MULTI (#12326)
When a connection that's subscribe to a channel emits PUBLISH inside MULTI-EXEC,
the push notification messes up the EXEC response.
e.g. MULTI, PING, PUSH foo bar, PING, EXEC
the EXEC's response will contain: PONG, {message foo bar}, 1. and the second PONG
will be delivered outside the EXEC's response.
Additionally, this PR changes the order of responses in case of a plain PUBLISH (when
the current client also subscribed to it), by delivering the push after the command's
response instead of before it.
This also affects modules calling RM_PublishMessage in a similar way, so that we don't
run the risk of getting that push mixed together with the module command's response.
2023-06-20 18:41:41 +01:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int cmd_publish_classic(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
|
2022-04-17 14:43:22 +02:00
|
|
|
{
|
|
|
|
if (argc != 3)
|
2024-04-23 08:55:44 -07:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
2022-04-17 14:43:22 +02:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int receivers = ValkeyModule_PublishMessage(ctx, argv[1], argv[2]);
|
|
|
|
ValkeyModule_ReplyWithLongLong(ctx, receivers);
|
|
|
|
return VALKEYMODULE_OK;
|
2022-04-17 14:43:22 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int cmd_publish_shard(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
|
2022-04-17 14:43:22 +02:00
|
|
|
{
|
|
|
|
if (argc != 3)
|
2024-04-23 08:55:44 -07:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
2022-04-17 14:43:22 +02:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int receivers = ValkeyModule_PublishMessageShard(ctx, argv[1], argv[2]);
|
|
|
|
ValkeyModule_ReplyWithLongLong(ctx, receivers);
|
|
|
|
return VALKEYMODULE_OK;
|
2022-04-17 14:43:22 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
2022-04-17 14:43:22 +02:00
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(argc);
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_Init(ctx,"publish",1,VALKEYMODULE_APIVER_1)== VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2022-04-17 14:43:22 +02:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"publish.classic",cmd_publish_classic,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2022-04-17 14:43:22 +02:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"publish.classic_multi",cmd_publish_classic_multi,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
Fix broken protocol when PUBLISH emits local push inside MULTI (#12326)
When a connection that's subscribe to a channel emits PUBLISH inside MULTI-EXEC,
the push notification messes up the EXEC response.
e.g. MULTI, PING, PUSH foo bar, PING, EXEC
the EXEC's response will contain: PONG, {message foo bar}, 1. and the second PONG
will be delivered outside the EXEC's response.
Additionally, this PR changes the order of responses in case of a plain PUBLISH (when
the current client also subscribed to it), by delivering the push after the command's
response instead of before it.
This also affects modules calling RM_PublishMessage in a similar way, so that we don't
run the risk of getting that push mixed together with the module command's response.
2023-06-20 18:41:41 +01:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"publish.shard",cmd_publish_shard,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2022-04-17 14:43:22 +02:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
return VALKEYMODULE_OK;
|
2022-04-17 14:43:22 +02:00
|
|
|
}
|