예제 #1
0
static void handleReply(redisAsyncContext *context, void *_reply, void *privdata) {
    REDIS_NOTUSED(privdata);
    redisReply *reply = (redisReply*)_reply;
    client c = (client)context->data;
    long long latency = (microseconds() - c->start) / 1000;

    if (reply == NULL && context->err) {
        fprintf(stderr,"Error: %s\n", context->errstr);
        exit(1);
    } else {
        assert(reply != NULL);
        if (reply->type == REDIS_REPLY_ERROR) {
            fprintf(stderr,"Error: %s\n", reply->str);
            exit(1);
        }
    }

    if (latency > MAX_LATENCY) latency = MAX_LATENCY;
    config.latency[latency]++;

    if (config.check) checkDataIntegrity(c,reply);
    freeReplyObject(reply);

    if (config.done || config.ctrlc) {
        redisAsyncDisconnect(c->context);
        return;
    }

    if (config.keepalive) {
        issueRequest(c);
    } else {
        /* createMissingClients will be called in the disconnection callback */
        redisAsyncDisconnect(c->context);
    }
}
예제 #2
0
    int RedisAsyncConn::DoConnect() {
        m_state = CONNECTING;
        int retry = 0;
        while (true) {
            m_async_context = redisAsyncConnect(m_server_ip.c_str(), m_server_port);
            if (m_async_context->err) {
                LOG(ERROR) << "RedisAsyncConn::DoConnect() error=" << m_async_context->errstr
                    << ", retry=" << retry;
                redisAsyncDisconnect(m_async_context);
                if (++retry > m_max_retry_times) {
                    return REDIS_ASYNC_CONN_ERR;
                }
            } else {
                break;
            }
        }
        g_async_conn_map.insert(make_pair(m_async_context, this));

        RedisNetlibAttach(m_async_context, NetEventLoop::GetInstance());
        redisAsyncSetConnectCallback(m_async_context, ConnectCallback);
        redisAsyncSetDisconnectCallback(m_async_context, DisconnectCallback);

        LOG(DEBUG) << "RedisAsyncConn::DoConnect() server_ip=" << m_server_ip << ", server_port="
            << m_server_port << ", handle=" << m_async_context->c.fd;
        return REDIS_ASYNC_CONN_OK;
    }
예제 #3
0
static PHP_METHOD(swoole_redis, close)
{
    swRedisClient *redis = swoole_get_object(getThis());
    if (redis->state != SWOOLE_REDIS_STATE_CLOSED)
    {
        redisAsyncDisconnect(redis->context);
    }
}
예제 #4
0
void getCallback(redisAsyncContext *c, void *r, void *privdata) {
    redisReply *reply = r;
    if (reply == NULL) return;
    printf("argv[%s]: %s\n", (char*)privdata, reply->str);

    /* Disconnect after receiving the reply to GET */
    redisAsyncDisconnect(c);
}
예제 #5
0
파일: main.c 프로젝트: Phantasus/kassomat
void terminate(void) {
	evtimer_del(&sspPollEvent);
	if(db) {
		redisAsyncCommand(db, NULL, NULL, "SET component:ssp 0");
		redisAsyncDisconnect(db);
		db = NULL;
	}
}
예제 #6
0
void
redis_nginx_ping_callback(redisAsyncContext *ac, void *rep, void *privdata)
{
    void *data = ac->data;
    void (*callback) (void *) = privdata;
    redisAsyncDisconnect(ac);
    if (callback != NULL) {
        callback(data);
    }
}
예제 #7
0
static PHP_METHOD(swoole_redis, __destruct)
{
    swRedisClient *redis = swoole_get_object(getThis());
    if (redis && redis->context && redis->state != SWOOLE_REDIS_STATE_CLOSED)
    {
        redisAsyncDisconnect(redis->context);
    }
    swoole_set_object(getThis(), NULL);
    efree(redis);
}
예제 #8
0
void
redis_nginx_force_close_context(redisAsyncContext **context)
{
    if ((context != NULL) && (*context != NULL)) {
        redisAsyncContext *ac = *context;
        if (!ac->err) {
            redisAsyncDisconnect(ac);
        }
        *context = NULL;
    }
}
예제 #9
0
 void RedisAsyncConn::Close() {
     if (!m_async_context) {
         return;
     }
     m_state = CLOSED;
     m_is_authorized = false;
     LOG(DEBUG) << "RedisAsyncConn::Close() server_ip=" << m_server_ip << ", server_port="
         << m_server_port << ", handle=" << m_async_context->c.fd;
     redisAsyncDisconnect(m_async_context);
     m_async_context = NULL;
 }
예제 #10
0
void AsyncRedisMgr::dis_connect_callback(const redisAsyncContext *c, int status)
{
	LOG4CPLUS_DEBUG(logger,"dis connect redis.");

	if (status != REDIS_OK)
	{
		LOG4CPLUS_ERROR(logger,"connect redis fail: "<<c->errstr);
		return;
	}

	redisAsyncDisconnect(const_cast<redisAsyncContext*>(c));
}
예제 #11
0
// async
void RedisClientPool::DestroyAsync(redisAsyncContext* item)
{
	if (item)
	{
		stringstream stream;
		stream << "free redis async context";
		LOG(INFO, stream.str());

		redisAsyncDisconnect(item);
		//redisFree(item);
	}
}
예제 #12
0
파일: redis.c 프로젝트: 4z3/luvit-redis
static int lua_client_disconnect(lua_State *L)
{
  lua_redis_client_t *lua_redis_client = (lua_redis_client_t *)
                                    luaL_checkudata(L, 1, LUA_REDIS_CLIENT_MT);

  if (lua_redis_client->redis_async_context == NULL)
  {
    return luaL_error(L, "RedisAsyncContext is null");
  }

  redisAsyncDisconnect(lua_redis_client->redis_async_context);

  return 0;
}
예제 #13
0
void getCallback(redisAsyncContext *c, void *r, void *privdata)
{
   redisReply *reply = r;
      if (reply == NULL) return;

   gw_req_async_t *async = (gw_req_async_t *)privdata;

   xbuf_xcat(async->reply, "%x\r\n%s\r\n", reply->len, reply->str);

   // now unlock to main()
   light_unlock(&async->lock);

   // Disconnect after receiving the reply to GET
   redisAsyncDisconnect(c);
}
예제 #14
0
static PHP_METHOD(swoole_redis, close)
{
    swRedisClient *redis = swoole_get_object(getThis());
    if (redis && redis->context)
    {
        if (redis->connecting)
        {
            swoole_php_fatal_error(E_WARNING, "redis client is connecting, cannot close.");
            RETURN_FALSE;
        }
        else if (redis->state != SWOOLE_REDIS_STATE_CLOSED)
        {
            redisAsyncDisconnect(redis->context);
        }
    }

}
예제 #15
0
        void connection::release(bool close_fd) {
            if (NULL != context && close_fd) {
                redisAsyncDisconnect(context);
            }

            // 回包列表
            while (!reply_list.empty()) {
                cmd_exec* expired_c = reply_list.front();
                reply_list.pop_front();

                // context 已被关闭
                expired_c->call_reply(error_code::REDIS_HAPP_CONNECTION, NULL, NULL);
                cmd_exec::destroy(expired_c);
            }

            context = NULL;
            conn_status = status::DISCONNECTED;
        }
예제 #16
0
파일: client.c 프로젝트: nicolasff/webdis
int
http_client_read(struct http_client *c) {

	char buffer[4096];
	int ret;

	ret = read(c->fd, buffer, sizeof(buffer));
	if(ret <= 0) {
		/* broken link, free buffer and client object */

		/* disconnect pub/sub client if there is one. */
		if(c->pub_sub && c->pub_sub->ac) {
			struct cmd *cmd = c->pub_sub;

			/* disconnect from all channels */
			redisAsyncDisconnect(c->pub_sub->ac);
			// c->pub_sub might be already cleared by an event handler in redisAsyncDisconnect
			cmd->ac = NULL;
			c->pub_sub = NULL;

			/* delete command object */
			cmd_free(cmd);
		}

		close(c->fd);

		http_client_free(c);
		return (int)CLIENT_DISCONNECTED;
	}

	/* save what we've just read */
	c->buffer = realloc(c->buffer, c->sz + ret);
	if(!c->buffer) {
		return (int)CLIENT_OOM;
	}
	memcpy(c->buffer + c->sz, buffer, ret);
	c->sz += ret;

	/* keep track of total sent */
	c->request_sz += ret;

	return ret;
}
예제 #17
0
파일: redis.c 프로젝트: NeverMore93/INTANG
void build_async_conn()
{
    if (actx) {
        redisAsyncDisconnect(actx);
    }

    actx = redisAsyncConnect(REDIS_SERVER_IP, REDIS_SERVER_PORT);
    if (actx == NULL || actx->err) {
        if (actx) {
            log_error("redisAsyncConnect fails: %s", actx->errstr);
            //redisFree(actx);
        } else {
            log_error("redisAsyncConnect fails: can't allocate redis context");
        }
        return;
    }

    redisLibevAttach(EV_DEFAULT_ actx);
    redisAsyncSetConnectCallback(actx, on_connect);
    redisAsyncSetDisconnectCallback(actx, on_disconnect);
    redisAsyncCommand(actx, on_expire, NULL, "SUBSCRIBE __keyevent@0__:expired");
}
예제 #18
0
파일: client.c 프로젝트: filly86/webdis
int
http_client_read(struct http_client *c) {

	char buffer[4096];
	int ret;

	ret = read(c->fd, buffer, sizeof(buffer));
	if(ret <= 0) {
		/* broken link, free buffer and client object */

		/* disconnect pub/sub client if there is one. */
		if(c->pub_sub && c->pub_sub->ac) {
			struct cmd *cmd = c->pub_sub;

			/* disconnect from all channels */
			redisAsyncDisconnect(c->pub_sub->ac);
			if(c->pub_sub) c->pub_sub->ac = NULL;
			c->pub_sub = NULL;

			/* delete command object */
			cmd_free(cmd);
		}

		close(c->fd);

		http_client_free(c);
		return -1;
	}

	/* save what we've just read */
	c->buffer = realloc(c->buffer, c->sz + ret);
	memcpy(c->buffer + c->sz, buffer, ret);
	c->sz += ret;

	return ret;
}
예제 #19
0
void AsyncConnection::disconnect()
{
    status_ = DISCONNECTING;
    redisAsyncDisconnect(async_context_);
}
예제 #20
0
void destroyRedis(redisAsyncContext *ctx)
{
	redisAsyncDisconnect(ctx);
	redisAsyncFree(ctx);
}
예제 #21
0
    void GatewayDataMgr::quitConnCallBack(redisAsyncContext *c, void *r, void *privdata) {

        LOG(INFO)<<"quit";
        redisAsyncDisconnect(c);
    } 
예제 #22
0
static PHP_METHOD(swoole_redis, close)
{
    swRedisClient *redis = swoole_get_object(getThis());
    redisAsyncDisconnect(redis->context);
}
예제 #23
0
struct nbd_handle* nbd_init_redis(char* export_name, char* redis_server,
                                  int redis_port, int redis_db, uint64_t fsize,
                                  char* nodename, char* port, bool old)
{
    struct addrinfo hints;
    struct addrinfo* server = NULL;
    struct event_base* eb = NULL;
    struct nbd_handle* ret = NULL;
    struct event* evsignal = NULL;
    struct evconnlistener* conn = NULL;
    struct redisAsyncContext* redis_c = NULL;

    /* sanity check */
    if (redis_server == NULL || nodename == NULL || port == NULL ||
        export_name == NULL)
        return NULL;

    /* setup network socket */
    memset(&hints, 0, sizeof(struct addrinfo));

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    if (getaddrinfo(nodename, port, &hints, &server))
    {
        if (server)
            freeaddrinfo(server);
        return NULL;
    }

    /* initialize libevent */
    if ((eb = event_base_new()) == NULL)
    {
        freeaddrinfo(server);
        return NULL;
    }

    /* initialize libhiredis */
    if ((redis_c = redisAsyncConnect(redis_server, redis_port)) == NULL)
    {
        freeaddrinfo(server);
        event_base_free(eb);
        return NULL;
    }

    redisLibeventAttach(redis_c, eb);

    /* set disconnect handler */
    if (redisAsyncSetDisconnectCallback(redis_c, &redis_disconnect_callback) ==
        REDIS_ERR)
    {
        redisAsyncDisconnect(redis_c);
        freeaddrinfo(server);
        event_base_free(eb);
        return NULL;
    }

    /* initialize this nbd module */
    if ((ret = (struct nbd_handle*) malloc(sizeof(struct nbd_handle))) == NULL)
    {
        freeaddrinfo(server);
        event_base_free(eb);
        return NULL;
    }

    redis_c->data = ret; /* set unused field for disconnect callback */
    evsignal = evsignal_new(eb, SIGINT, nbd_signal_handler, (void *)ret);

    if (!evsignal || event_add(evsignal, NULL) < 0)
    {
        freeaddrinfo(server);
        event_base_free(eb);
        return NULL;
    }

    /* setup network connection */
    if ((conn = evconnlistener_new_bind(eb, old ? &nbd_old_conn :
                                        &nbd_new_conn, ret,
                                        LEV_OPT_CLOSE_ON_FREE |
                                        LEV_OPT_REUSEABLE, -1,
                                        server->ai_addr,
                                        server->ai_addrlen)) == NULL)
    {
        freeaddrinfo(server);
        event_base_free(eb);
        free(ret);
        return NULL;
    }

    evconnlistener_set_error_cb(conn, nbd_event_error);

    ret->fd = -1;
    ret->size = fsize;
    ret->export_name = export_name;
    ret->redis_server = redis_server;
    ret->name_len = strlen(export_name);
    ret->redis_port = redis_port;
    ret->redis_db = redis_db;
    ret->eb = eb;
    ret->conn = conn;
    ret->redis_c = redis_c;

    assert(redisAsyncCommand(redis_c, &redis_async_callback, NULL, "select %d",
                             redis_db) == REDIS_OK);

    freeaddrinfo(server);

    return ret;
}
예제 #24
0
static void redis_close(void* data)
{
    swRedisClient *redis = data;
    redisAsyncDisconnect(redis->context);
}
예제 #25
0
파일: eredis.c 프로젝트: gatezba/eredis
/*
 * EV connect callback
 *
 * EV_TIMER connect_timer
 */
  static void
_eredis_ev_connect_cb (struct ev_loop *loop, ev_timer *w, int revents)
{
  int i;
  eredis_t *e;

  (void) revents;
  (void) loop;

  e = (eredis_t*) w->data;

  if (IS_SHUTDOWN(e)) {
    if (e->hosts_connected) {
      for (i=0; i<e->hosts_nb; i++) {
        host_t *h = &e->hosts[i];
        if (H_IS_CONNECTED(h) && h->async_ctx)
          redisAsyncDisconnect( h->async_ctx );
      }
    }
    else {
      /* Connect timer */
      ev_timer_stop( e->loop, &e->connect_timer );
      /* Async send */
      ev_async_stop( e->loop, &e->send_async );
      /* Event break */
      ev_break( e->loop, EVBREAK_ALL );
    }
    return;
  }

  for (i=0; i<e->hosts_nb; i++) {
    host_t *h = &e->hosts[i];
    switch (H_CONN_STATE( h )) {
      case HOST_F_CONNECTED:
        break;

      case HOST_F_FAILED:
        if ((h->failures < HOST_FAILED_RETRY_AFTER)
            ||
            ( ! _host_connect( h, 0 ))) {
          h->failures %= HOST_FAILED_RETRY_AFTER;
          h->failures ++;
        }
        break;

      case HOST_F_DISCONNECTED:
        if (! _host_connect( h, 0 )) {
          if ((++ h->failures) > HOST_DISCONNECTED_RETRIES) {
            h->failures = 0;
            H_SET_FAILED( h );
          }
        }
        break;

      default:
        break;
    }
  }

  if (! IS_READY(e)) {
    /* Ready flag - need a connected host or a connection failure */
    int nb = 0;
    /* build ready flag */
    for (i=0; i<e->hosts_nb; i++) {
      host_t *h = &e->hosts[i];
      if (H_IS_INIT( h ))
        nb ++;
    }
    if (nb == e->hosts_nb) {
      SET_READY(e);
      e->send_async_pending = 1;
      ev_async_send( e->loop, &e->send_async );
    }
  }
}
예제 #26
0
파일: client.cpp 프로젝트: bdallas/redox
void Redox::runEventLoop() {

  // Events to connect to Redox
  ev_run(evloop_, EVRUN_ONCE);
  ev_run(evloop_, EVRUN_NOWAIT);

  // Block until connected to Redis, or error
  {
    unique_lock<mutex> ul(connect_lock_);
    connect_waiter_.wait(ul, [this] { return connect_state_ != NOT_YET_CONNECTED; });

    // Handle connection error
    if (connect_state_ != CONNECTED) {
      logger_.warning() << "Did not connect, event loop exiting.";
      {
        unique_lock<mutex> ul(exit_lock_);
        exited_ = true;
      }

      {
        unique_lock<mutex> ul(running_lock_);
        running_ = false;
      }

      running_waiter_.notify_one();
      exit_waiter_.notify_one();
      return;
    }
  }

  // Set up asynchronous watcher which we signal every
  // time we add a command
  ev_async_init(&watcher_command_, processQueuedCommands);
  ev_async_start(evloop_, &watcher_command_);

  // Set up an async watcher to break the loop
  ev_async_init(&watcher_stop_, breakEventLoop);
  ev_async_start(evloop_, &watcher_stop_);

  // Set up an async watcher which we signal every time
  // we want a command freed
  ev_async_init(&watcher_free_, freeQueuedCommands);
  ev_async_start(evloop_, &watcher_free_);

  {
    unique_lock<mutex> ul(running_lock_);
    running_ = true;
    running_waiter_.notify_one();
  }

  // Run the event loop, using NOWAIT if enabled for maximum
  // throughput by avoiding any sleeping
  while (!to_exit_) {
    if (nowait_) {
      ev_run(evloop_, EVRUN_NOWAIT);
    } else {
      ev_run(evloop_);
    }
  }

  logger_.info() << "Stop signal detected. Closing down event loop.";

  // Signal event loop to free all commands
  freeAllCommands();

  // Wait to receive server replies for clean hiredis disconnect
  this_thread::sleep_for(chrono::milliseconds(10));
  ev_run(evloop_, EVRUN_NOWAIT);

  int state;
  {
    unique_lock<mutex> ul(connect_lock_);
    state = connect_state_;
  }
  
  if (connect_state_ == CONNECTED)
    redisAsyncDisconnect(ctx_);

  // Run once more to disconnect
  ev_run(evloop_, EVRUN_NOWAIT);

  long created = commands_created_;
  long deleted = commands_deleted_;
  if (created != deleted) {
    logger_.error() << "All commands were not freed! " << deleted << "/"
                    << created;
  }

  {
    unique_lock<mutex> ul(exit_lock_);
    exited_ = true;
  }

  {
    unique_lock<mutex> ul(running_lock_);
    running_ = false;
  }

  // Let go for block_until_stopped method
  running_waiter_.notify_one();
  exit_waiter_.notify_one();

  logger_.info() << "Event thread exited.";
}