Skip to content

Commit

Permalink
[fix] un-refactor the code. [perf] replyWithStatus now makes usage of…
Browse files Browse the repository at this point in the history
… addReplyProto
  • Loading branch information
filipecosta90 committed Sep 23, 2019
1 parent 4a30a26 commit 733280d
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,19 @@ int RM_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll) {
return REDISMODULE_OK;
}

/* Reply with an error or simple string (status message). Used to implement
* ReplyWithSimpleString() and ReplyWithError().
* The function always returns REDISMODULE_OK. */
int replyWithStatus(RedisModuleCtx *ctx, const char *msg, char *prefix) {
client *c = moduleGetReplyClient(ctx);
if (c == NULL) return REDISMODULE_OK;
const size_t len = strlen(msg);
addReplyProto(c,"-",1);
addReplyProto(c,msg,len);
addReplyProto(c,"\r\n",2);
return REDISMODULE_OK;
}

/* Reply with the error 'err'.
*
* Note that 'err' must contain all the error, including
Expand All @@ -1135,13 +1148,7 @@ int RM_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll) {
* The function always returns REDISMODULE_OK.
*/
int RM_ReplyWithError(RedisModuleCtx *ctx, const char *err) {
client *c = moduleGetReplyClient(ctx);
if (c == NULL) return REDISMODULE_OK;
const size_t len = strlen(err);
addReplyProto(c,"-",1);
addReplyProto(c,err,len);
addReplyProto(c,"\r\n",2);
return REDISMODULE_OK;
return replyWithStatus(ctx,err,"-");
}

/* Reply with a simple string (+... \r\n in RESP protocol). This replies
Expand All @@ -1150,13 +1157,7 @@ int RM_ReplyWithError(RedisModuleCtx *ctx, const char *err) {
*
* The function always returns REDISMODULE_OK. */
int RM_ReplyWithSimpleString(RedisModuleCtx *ctx, const char *msg) {
client *c = moduleGetReplyClient(ctx);
if (c == NULL) return REDISMODULE_OK;
const size_t len = strlen(msg);
addReplyProto(c,"+",1);
addReplyProto(c,msg,len);
addReplyProto(c,"\r\n",2);
return REDISMODULE_OK;
return replyWithStatus(ctx,msg,"+");
}

/* Reply with an array type of 'len' elements. However 'len' other calls
Expand Down

0 comments on commit 733280d

Please sign in to comment.