MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / getRedisConfig

Function getRedisConfig

src/redis-benchmark.cpp:334–386  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

332}
333
334static redisConfig *getRedisConfig(const char *ip, int port,
335 const char *hostsocket)
336{
337 redisConfig *cfg = (redisConfig*)zcalloc(sizeof(*cfg));
338 if (!cfg) return NULL;
339 redisContext *c = NULL;
340 redisReply *reply = NULL, *sub_reply = NULL;
341 c = getRedisContext(ip, port, hostsocket);
342 if (c == NULL) {
343 freeRedisConfig(cfg);
344 return NULL;
345 }
346 redisAppendCommand(c, "CONFIG GET %s", "save");
347 redisAppendCommand(c, "CONFIG GET %s", "appendonly");
348
349 void *r;
350 for (int i=0; i < 2; i++) {
351 int res = redisGetReply(c, &r);
352 if (reply) freeReplyObject(reply);
353 reply = res == REDIS_OK ? ((redisReply *) r) : NULL;
354 if (res != REDIS_OK || !r) goto fail;
355 if (reply->type == REDIS_REPLY_ERROR) {
356 fprintf(stderr, "ERROR: %s\n", reply->str);
357 goto fail;
358 }
359 if (reply->type != REDIS_REPLY_ARRAY || reply->elements < 2) goto fail;
360 sub_reply = reply->element[1];
361 const char *value = sub_reply->str;
362 if (!value) value = "";
363 switch (i) {
364 case 0: cfg->save = sdsnew(value); break;
365 case 1: cfg->appendonly = sdsnew(value); break;
366 }
367 }
368 freeReplyObject(reply);
369 redisFree(c);
370 return cfg;
371fail:
372 fprintf(stderr, "ERROR: failed to fetch CONFIG from ");
373 if (hostsocket == NULL) fprintf(stderr, "%s:%d\n", ip, port);
374 else fprintf(stderr, "%s\n", hostsocket);
375 int abort_test = 0;
376 if (reply && reply->type == REDIS_REPLY_ERROR &&
377 (!strncmp(reply->str,"NOAUTH",5) ||
378 !strncmp(reply->str,"WRONGPASS",9) ||
379 !strncmp(reply->str,"NOPERM",5)))
380 abort_test = 1;
381 freeReplyObject(reply);
382 redisFree(c);
383 freeRedisConfig(cfg);
384 if (abort_test) exit(1);
385 return NULL;
386}
387static void freeRedisConfig(redisConfig *cfg) {
388 if (cfg->save) sdsfree(cfg->save);
389 if (cfg->appendonly) sdsfree(cfg->appendonly);

Callers 1

mainFunction · 0.85

Calls 8

zcallocFunction · 0.85
freeRedisConfigFunction · 0.85
redisAppendCommandFunction · 0.85
redisGetReplyFunction · 0.85
freeReplyObjectFunction · 0.85
sdsnewFunction · 0.85
redisFreeFunction · 0.85
getRedisContextFunction · 0.70

Tested by

no test coverage detected