Exemple #1
0
static void rc_validate_connection(rconn_t *c, int optional) {
    if (!c->rc && (c->flags & RCF_RECONNECT)) {
	struct timeval tv;
	tv.tv_sec = (int) c->timeout;
	tv.tv_usec = (c->timeout - (double)tv.tv_sec) * 1000000.0;
	if (c->port < 1)
	    c->rc = redisConnectUnixWithTimeout(c->host, tv);
	else
	    c->rc = redisConnectWithTimeout(c->host, c->port, tv);
	if (!c->rc) {
	    if (optional) return;
	    Rf_error("disconnected connection and re-connect to redis failed (NULL context)");
	}
	if (c->rc->err){
	    SEXP es = Rf_mkChar(c->rc->errstr);
	    redisFree(c->rc);
	    c->rc = 0;
	    if (optional) return;
	    Rf_error("disconnected connection and re-connect to redis failed: %s", CHAR(es));
	}
	redisSetTimeout(c->rc, tv);
	/* re-connect succeeded */
    }
    if (!c->rc && !optional)
	Rf_error("disconnected redis connection");
}
Exemple #2
0
int main()
{
    redisContext *c;
    redisReply *reply;

    long i = 0, count = 1;
    long long start, current, consume;

    start = getCurrentTime();
    struct timeval timeout = { 1, 500000 };
    c = redisConnectUnixWithTimeout("/tmp/redis.sock", timeout);

    for(i = 0; i < count; i++)
    {
        int num = 1;
        reply = (redisReply*) redisCommand(c, "SET a %d", num);
        if(reply)
        {
            printf("%lld\n", reply->integer);
            freeReplyObject(reply);
        }
        reply = (redisReply*) redisCommand(c, "GET a");
        if(reply)
        {
            printReplyObject(reply);
            freeReplyObject(reply);
        }
    }

    consume = getCurrentTime() - start;

    printf("average handle time (us) : [%lld]\n", consume/count);

    return 0;
}
Exemple #3
0
static redisContext* redis_connect(void)
{
	struct timeval timeout;
	timeout.tv_sec = REDIS_TIMEOUT;
	timeout.tv_usec = 0;
	return (redisContext*) redisConnectUnixWithTimeout(REDIS_UNIX_PATH,
			timeout);
}
Exemple #4
0
redisContext* redis_connect_from_conf(redisconf_t* c)
{
	assert(c);
	struct timeval timeout;
	timeout.tv_sec = REDIS_TIMEOUT;
	timeout.tv_usec = 0;
	if (c->type == T_LOCAL) {
		return (redisContext*) redisConnectUnixWithTimeout(c->path,
			timeout);
	} else {
		return (redisContext*) redisConnectWithTimeout(c->server,
			c->port, timeout);
	}
}
Exemple #5
0
SEXP cr_connect(SEXP sHost, SEXP sPort, SEXP sTimeout, SEXP sReconnect, SEXP sRetry) {
    const char *host = "localhost";
    double tout = Rf_asReal(sTimeout);
    int port = Rf_asInteger(sPort), reconnect = (Rf_asInteger(sReconnect) > 0),
	retry = (Rf_asInteger(sRetry) > 0);
    redisContext *ctx;
    rconn_t *c;
    SEXP res;
    struct timeval tv;

    if (TYPEOF(sHost) == STRSXP && LENGTH(sHost) > 0)
	host = CHAR(STRING_ELT(sHost, 0));

    tv.tv_sec = (int) tout;
    tv.tv_usec = (tout - (double)tv.tv_sec) * 1000000.0;
    if (port < 1)
	ctx = redisConnectUnixWithTimeout(host, tv);
    else
	ctx = redisConnectWithTimeout(host, port, tv);
    if (!ctx) Rf_error("connect to redis failed (NULL context)");
    if (ctx->err){
	SEXP es = Rf_mkChar(ctx->errstr);
	redisFree(ctx);
	Rf_error("connect to redis failed: %s", CHAR(es));
    }
    c = malloc(sizeof(rconn_t));
    if (!c) {
	redisFree(ctx);
	Rf_error("unable to allocate connection context");
    }
    c->rc = ctx;
    c->flags = (reconnect ? RCF_RECONNECT : 0) | (retry ? RCF_RETRY : 0);
    c->host  = strdup(host);
    c->port  = port;
    c->timeout = tout;
    redisSetTimeout(ctx, tv);
    res = PROTECT(R_MakeExternalPtr(c, R_NilValue, R_NilValue));
    Rf_setAttrib(res, R_ClassSymbol, Rf_mkString("redisConnection"));
    R_RegisterCFinalizer(res, rconn_fin);
    UNPROTECT(1);
    return res;
}
Exemple #6
0
static int lua_connect_unix (lua_State *L) {
  const char *path = luaL_checkstring(L, 1);
  int timeout = luaL_optint(L, 3, -1);
  int nonblock = lua_toboolean(L, 4);
  if (timeout == -1) {
    if (nonblock == 1) {
      redisContext *con = redisConnectUnixNonBlock(path);
      return pushConnection(L, con);
    } else {
      redisContext *con = redisConnectUnix(path);
      return pushConnection(L, con);
    }
  } else {
    struct timeval to;
    to.tv_sec = timeout;
    to.tv_usec = 0;
    redisContext *con = redisConnectUnixWithTimeout(path, to);
    return pushConnection(L, con);
  }
};
bool DRedisConnection::connect()
{
  if(redis_context)
    return true;

  if(!cfg.unix_socket) {
    DBG("connecting to REDIS at %s:%u\n", cfg.host.c_str(), cfg.port);
    redis_context = redisConnectWithTimeout((char*)cfg.host.c_str(),
					    cfg.port, cfg.tv_timeout);
  }
  else {
    DBG("connecting to REDIS at %s\n", cfg.host.c_str());
    redis_context = redisConnectUnixWithTimeout(cfg.host.c_str(),
						cfg.tv_timeout);
  }

  if (redis_context->err) {
    ERROR("REDIS Connection error: %s\n", redis_context->errstr);
    disconnect();
    return false;
  }

  return true;
}
Exemple #8
0
int main(int argc, char **argv) {
  redisContext *c;
  redisReply *reply;
  const char *socket = NULL;
  const char *ip = "127.0.0.1";
  int port = 3388;
  const char *topic = "loggen";
  int num = -1;
  int period = -1;
  int i = 0;
  void *kid = NULL;
  char logbuf[10240];
  int opt;
  int quiet = 0;
  int psub = 0;
  const char* const short_options = "s:i:p:t:f:n:qbh";
  const struct option long_options[] = {
    { "socket", required_argument, NULL, 's' },
    { "ip", required_argument, NULL, 'i' },
    { "port", required_argument, NULL, 'p' },
    { "topic", required_argument, NULL, 't' },
    { "psub", no_argument, NULL, 'b' },
    { "num", required_argument, NULL, 'n' },
    { "period", required_argument, NULL, 'r' },
    { "quiet", no_argument, NULL, 'q' },
    { "help", no_argument, NULL, 'h' },
    { NULL, 0, NULL, 0 },
  };

  while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) > 0) {
    switch (opt) {
      case 's':
        socket = optarg;
        break;
      case 'i':
        ip = optarg;
        break;
      case 'p':
        port = atoi(optarg);
        break;
      case 't':
        topic = optarg;
        break;
      case 'b':
        psub = 1;
        break;
      case 'n':
        num = atoi(optarg);
        break;
      case 'r':
        period = atoi(optarg);
        break;
      case 'q':
        quiet = 1;
        break;
      case 'h':
      default:
        Usage(argv[0]);
    }
  }

  if (socket == NULL && ip == NULL) {
    Usage(argv[0]);
  }

  struct timeval timeout = { 1, 500000 }; // 1.5 seconds

  if (socket) {
    c = redisConnectUnixWithTimeout(socket, timeout);
  } else {
    c = redisConnectWithTimeout((char*)ip, port, timeout);
  }
  if (c->err) {
    printf("Connection error: %s\n", c->errstr);
    exit(1);
  }

  if (psub) reply = redisCommand(c,"PSUBSCRIBE %s", topic);
  else reply = redisCommand(c,"SUBSCRIBE %s", topic);
  if (reply) {
    printf("SUBSCRIBE %s\n", reply->str);
    freeReplyObject(reply);
  }

  while (1) {
    if (!redisGetReply(c, (void*)&reply)) {
      printf("%s %s %s",  reply->element[0]->str, reply->element[1]->str, reply->element[2]->str);
      if (psub) printf("%s", reply->element[3]->str);
      printf("\n");
      freeReplyObject(reply);
    } else {
      printf("get msg failed\n");
    }

    if (period != -1) {
      usleep(period * 1000);
    }
  }

  redisFree(c);
}
Exemple #9
0
int redisc_init(void)
{
	char *addr, *unix_sock_path = NULL;
	unsigned int port, db;
	redisc_server_t *rsrv=NULL;
	param_t *pit = NULL;
	struct timeval tv;

	tv.tv_sec = 1;
	tv.tv_usec = 0;

	if(_redisc_srv_list==NULL)
	{
		LM_ERR("no redis servers defined\n");
		return -1;
	}

	for(rsrv=_redisc_srv_list; rsrv; rsrv=rsrv->next)
	{
		addr = "127.0.0.1";
		port = 6379;
		db = 0;
		for (pit = rsrv->attrs; pit; pit=pit->next)
		{
			if(pit->name.len==4 && strncmp(pit->name.s, "unix", 4)==0) {
				unix_sock_path = pit->body.s;
				unix_sock_path[pit->body.len] = '\0';
			} else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
				addr = pit->body.s;
				addr[pit->body.len] = '\0';
			} else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) {
				if(str2int(&pit->body, &port) < 0)
					port = 6379;
			} else if(pit->name.len==2 && strncmp(pit->name.s, "db", 2)==0) {
				if(str2int(&pit->body, &db) < 0)
					db = 0;
			}
		}

		if(unix_sock_path != NULL) {
			LM_DBG("Connecting to unix socket: %s\n", unix_sock_path);
			rsrv->ctxRedis = redisConnectUnixWithTimeout(unix_sock_path, tv);
		} else {
			rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv);
		}

		if(!rsrv->ctxRedis)
			goto err;
		if (rsrv->ctxRedis->err)
			goto err2;
		if (redisCommandNR(rsrv->ctxRedis, "PING"))
			goto err2;
		if (redisCommandNR(rsrv->ctxRedis, "SELECT %i", db))
			goto err2;

	}

	return 0;

err2:
	if (unix_sock_path != NULL) {
		LM_ERR("error communicating with redis server [%.*s] (unix:%s db:%d): %s\n",
			   rsrv->sname->len, rsrv->sname->s, unix_sock_path, db, rsrv->ctxRedis->errstr);
	} else {
		LM_ERR("error communicating with redis server [%.*s] (%s:%d/%d): %s\n",
			   rsrv->sname->len, rsrv->sname->s, addr, port, db, rsrv->ctxRedis->errstr);
	}
	return -1;
err:
	if (unix_sock_path != NULL) {
		LM_ERR("failed to connect to redis server [%.*s] (unix:%s db:%d)\n",
			   rsrv->sname->len, rsrv->sname->s, unix_sock_path, db);
	} else {
		LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n",
			   rsrv->sname->len, rsrv->sname->s, addr, port, db);
	}
	return -1;
}
Exemple #10
0
int redisc_init(void)
{
	char addr[256], pass[256], unix_sock_path[256];

	unsigned int port, db, sock = 0, haspass = 0;
	redisc_server_t *rsrv=NULL;
	param_t *pit = NULL;
	struct timeval tv_conn;
	struct timeval tv_cmd;

	tv_conn.tv_sec = (int) redis_connect_timeout_param / 1000;
	tv_conn.tv_usec = (int) (redis_connect_timeout_param % 1000) * 1000;

	tv_cmd.tv_sec = (int) redis_cmd_timeout_param / 1000;
	tv_cmd.tv_usec = (int) (redis_cmd_timeout_param % 1000) * 1000;

	if(_redisc_srv_list==NULL)
	{
		LM_ERR("no redis servers defined\n");
		return -1;
	}

	for(rsrv=_redisc_srv_list; rsrv; rsrv=rsrv->next)
	{
		port = 6379;
		db = 0;
		haspass = 0;
		sock = 0;

		memset(addr, 0, sizeof(addr));
		memset(pass, 0, sizeof(pass));
		memset(unix_sock_path, 0, sizeof(unix_sock_path));

		for (pit = rsrv->attrs; pit; pit=pit->next)
		{
			if(pit->name.len==4 && strncmp(pit->name.s, "unix", 4)==0) {
				snprintf(unix_sock_path, sizeof(unix_sock_path)-1, "%.*s", pit->body.len, pit->body.s);
				sock = 1;
			} else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
				snprintf(addr, sizeof(addr)-1, "%.*s", pit->body.len, pit->body.s);
			} else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) {
				if(str2int(&pit->body, &port) < 0)
					port = 6379;
			} else if(pit->name.len==2 && strncmp(pit->name.s, "db", 2)==0) {
				if(str2int(&pit->body, &db) < 0)
					db = 0;
			} else if(pit->name.len==4 && strncmp(pit->name.s, "pass", 4)==0) {
				snprintf(pass, sizeof(pass)-1, "%.*s", pit->body.len, pit->body.s);
				haspass = 1;
			}
		}

		if(sock != 0) {
			LM_DBG("Connecting to unix socket: %s\n", unix_sock_path);
			rsrv->ctxRedis = redisConnectUnixWithTimeout(unix_sock_path,
					tv_conn);
		} else {
			LM_DBG("Connecting to %s:%d\n", addr, port);
			rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv_conn);
		}

		LM_DBG("rsrv->ctxRedis = %p\n", rsrv->ctxRedis);

		if(!rsrv->ctxRedis) {
			LM_ERR("Failed to create REDIS-Context.\n");
			goto err;
		}
		if (rsrv->ctxRedis->err) {
			LM_ERR("Failed to create REDIS returned an error: %s\n", rsrv->ctxRedis->errstr);
			goto err2;
		}
		if ((haspass != 0) && redisc_check_auth(rsrv, pass)) {
			LM_ERR("Authentication failed.\n");
			goto err2;
		}
		if (redisSetTimeout(rsrv->ctxRedis, tv_cmd)) {
			LM_ERR("Failed to set timeout.\n");
			goto err2;
		}
		if (redisCommandNR(rsrv->ctxRedis, "PING")) {
			LM_ERR("Failed to send PING (REDIS returned %s).\n", rsrv->ctxRedis->errstr);
			goto err2;
		}
		if ((redis_cluster_param == 0) && redisCommandNR(rsrv->ctxRedis, "SELECT %i", db)) {
			LM_ERR("Failed to send \"SELECT %i\" (REDIS returned \"%s\", and not in cluster mode).\n", db, rsrv->ctxRedis->errstr);
			goto err2;
		}
	}

	return 0;

err2:
	if (sock != 0) {
		LM_ERR("error communicating with redis server [%.*s]"
				" (unix:%s db:%d): %s\n",
				rsrv->sname->len, rsrv->sname->s, unix_sock_path, db,
				rsrv->ctxRedis->errstr);
	} else {
		LM_ERR("error communicating with redis server [%.*s] (%s:%d/%d): %s\n",
				rsrv->sname->len, rsrv->sname->s, addr, port, db,
				rsrv->ctxRedis->errstr);
	}
	if (init_without_redis==1)
	{
		LM_WARN("failed to initialize redis connections, but initializing"
				" module anyway.\n");
		return 0;
	}

	return -1;
err:
	if (sock != 0) {
		LM_ERR("failed to connect to redis server [%.*s] (unix:%s db:%d)\n",
				rsrv->sname->len, rsrv->sname->s, unix_sock_path, db);
	} else {
		LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n",
				rsrv->sname->len, rsrv->sname->s, addr, port, db);
	}
	if (init_without_redis==1)
	{
		LM_WARN("failed to initialize redis connections, but initializing"
				" module anyway.\n");
		return 0;
	}

	return -1;
}
Exemple #11
0
int redisc_reconnect_server(redisc_server_t *rsrv)
{
	char addr[256], pass[256], unix_sock_path[256];
	unsigned int port, db, sock = 0, haspass = 0;
	param_t *pit = NULL;
	struct timeval tv_conn;
	struct timeval tv_cmd;

	tv_conn.tv_sec = (int) redis_connect_timeout_param / 1000;
	tv_conn.tv_usec = (int) (redis_connect_timeout_param % 1000) * 1000;

	tv_cmd.tv_sec = (int) redis_cmd_timeout_param / 1000;
	tv_cmd.tv_usec = (int) (redis_cmd_timeout_param % 1000) * 1000;

	memset(addr, 0, sizeof(addr));
	port = 6379;
	db = 0;
	memset(pass, 0, sizeof(pass));
	memset(unix_sock_path, 0, sizeof(unix_sock_path));
	for (pit = rsrv->attrs; pit; pit=pit->next)
	{
		if(pit->name.len==4 && strncmp(pit->name.s, "unix", 4)==0) {
			snprintf(unix_sock_path, sizeof(unix_sock_path)-1, "%.*s", pit->body.len, pit->body.s);
			sock = 1;
		} else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
			snprintf(addr, sizeof(addr)-1, "%.*s", pit->body.len, pit->body.s);
		} else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) {
			if(str2int(&pit->body, &port) < 0)
				port = 6379;
		} else if(pit->name.len==2 && strncmp(pit->name.s, "db", 2)==0) {
			if(str2int(&pit->body, &db) < 0)
				db = 0;
		} else if(pit->name.len==4 && strncmp(pit->name.s, "pass", 4)==0) {
			snprintf(pass, sizeof(pass)-1, "%.*s", pit->body.len, pit->body.s);
			haspass = 1;
		}
	}

	LM_DBG("rsrv->ctxRedis = %p\n", rsrv->ctxRedis);
	if(rsrv->ctxRedis!=NULL) {
		redisFree(rsrv->ctxRedis);
		rsrv->ctxRedis = NULL;
	}

	if(sock != 0) {
		rsrv->ctxRedis = redisConnectUnixWithTimeout(unix_sock_path, tv_conn);
	} else {
		rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv_conn);
	}
	LM_DBG("rsrv->ctxRedis = %p\n", rsrv->ctxRedis);
	if(!rsrv->ctxRedis)
		goto err;
	if (rsrv->ctxRedis->err)
		goto err2;
	if ((haspass) && redisc_check_auth(rsrv, pass))
		goto err2;
	if (redisSetTimeout(rsrv->ctxRedis, tv_cmd))
		goto err2;
	if (redisCommandNR(rsrv->ctxRedis, "PING"))
		goto err2;
	if ((redis_cluster_param == 0) && redisCommandNR(rsrv->ctxRedis, "SELECT %i", db))
		goto err2;
	if (redis_flush_on_reconnect_param)
		if (redisCommandNR(rsrv->ctxRedis, "FLUSHALL"))
			goto err2;
	return 0;

err2:
	if (sock != 0) {
		LM_ERR("error communicating with redis server [%.*s]"
				" (unix:%s db:%d): %s\n",
				rsrv->sname->len, rsrv->sname->s, unix_sock_path, db,
				rsrv->ctxRedis->errstr);
	} else {
		LM_ERR("error communicating with redis server [%.*s] (%s:%d/%d): %s\n",
				rsrv->sname->len, rsrv->sname->s, addr, port, db,
				rsrv->ctxRedis->errstr);
	}
err:
	if (sock != 0) {
		LM_ERR("failed to connect to redis server [%.*s] (unix:%s db:%d)\n",
				rsrv->sname->len, rsrv->sname->s, unix_sock_path, db);
	} else {
		LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n",
				rsrv->sname->len, rsrv->sname->s, addr, port, db);
	}
	return -1;
}