Protocol error while publishing on Redis using hiredis

286 Views Asked by At

Problem Description I am using hiredis unix socket to publish a number of messages (~2 KB per second) on a RPI 4B. I use the code given below to publish. I start getting Redis Protocol error messages after approximately 30-45 minutes of operation. Please let me know I can resolve this. Thank you!

` #include "RedisDriver.h"

static redisContext* context;

const char* REDIS_SERVER_UNIX_SOCKET = (const char*) "/var/run/redis/redis-server.sock"; const char* REDIS_PACKET_CHANNEL_NAME = (const char*) "App-Data"; const char* REDIS_STATS_CHANNEL_NAME = (const char*) "App-Stats";

int RedisDriver_Initialize() {

struct timeval timeout = { 1, 500000 }; // 1.5 seconds
context = redisConnectUnixWithTimeout(REDIS_SERVER_UNIX_SOCKET, timeout);

if (context == NULL || context->err) {
    if (context) {
        syslog(LOG_ERR, "REDIS connection error: %s", context->errstr);
        redisFree(context);
    } else {
        syslog(LOG_ERR,"Unable to allocate REDIS context.");
    }
    return FAILURE;
}

syslog(LOG_INFO, "Connected to REDIS at %s", REDIS_SERVER_UNIX_SOCKET);
return SUCCESS;

}

int RedisDriver_Publish(char* msg, const char* channel_name) {

if (!msg || !channel_name) {
    return FAILURE;
}
//syslog(LOG_DEBUG, "message to publish: %s", msg);
redisReply* reply = (redisReply*) redisCommand(context, "PUBLISH %s %s", channel_name, msg);

if (reply) {
    syslog(LOG_INFO, "Published %s. Reply: %s", channel_name, reply->str);
    freeReplyObject(reply);
    return SUCCESS;
}

switch(context->err) {
    case REDIS_ERR_IO:
        syslog(LOG_ERR, "There was an I/O error while connecting to REDIS.");
        break;
    case REDIS_ERR_EOF:
        syslog(LOG_ERR, "REDIS server closed the connection.");
        break;
    case REDIS_ERR_PROTOCOL:
        syslog(LOG_ERR, "REDIS protocol error.");
        break;
    case REDIS_ERR_OTHER:
    default:
        syslog(LOG_ERR, "Unknown REDIS Error.");
        break;
}

return context->err;

}

int RedisDriver_Finalize() {

redisFree(context);
return SUCCESS;

} `

Environment:

  • Hardware: Raspberry PI 4B with 8 GB RAM
  • OS: Raspbian GNU/Linux 10 (buster)
  • Redis server: v=5.0.14 sha=00000000:0 malloc=libc bits=32 build=a6df2d1b76e73a91
  • Compiler: gcc (Raspbian 8.3.0-6+rpi1) 8.3.0
  • hiredis version: 0.14.0-3 armhf
0

There are 0 best solutions below