Skip to content
This repository was archived by the owner on Apr 6, 2019. It is now read-only.
This repository was archived by the owner on Apr 6, 2019. It is now read-only.

No reply when reconnection completed. #70

@xiaoluffy

Description

@xiaoluffy

I tried a simple reconnection in callback of client connect:

void RedisSender::connect() {
    try {
      this->client->connect(this->host, this->port, [this](cpp_redis::redis_client&) {
        LOG(WARNING) << "Redis disconnected, reconnecting..." << endl;
        this_thread::sleep_for(chrono::seconds(1));
        if((++ this->retries) < RECONNECT_RETRIES)
            this->connect();
      });
      
      if(this->auth.length() > 0)
        this->client->auth(this->auth, [](cpp_redis::reply& reply) {
          if(string("OK").compare(reply.as_string()) != 0)
              LOG(FATAL) << "Redis client authenticated failed.";
      });
      
      this->client->select(this->db, [](cpp_redis::reply& reply) {
          if(string("OK").compare(reply.as_string()) != 0)
              LOG(FATAL) << "Redis client select db failed.";
      });
      
      LOG(INFO) << "Redis client connected to " << this->host << ":" << this->port << "/" << this->db;
      this->client->commit();
    }
    catch(exception &e) {
        LOG(ERROR) << "Redis client connected failed with " << e.what();
        this_thread::sleep_for(chrono::seconds(1));
        if((++ this->retries) < RECONNECT_RETRIES)
            this->connect();
    }
}

// and a loop for test:
void RedisSender::test(string key, string value) {
    try{
        this->client->set(key, value);    
        this->client->get(key, [](cpp_redis::reply& reply) {
            LOG(INFO) << "get hello: " << reply;
        });
        LOG(WARNING) << "test " << value;
        this->client->commit();
    }
    catch(exception &e) {
        this_thread::sleep_for(chrono::seconds(1));
        LOG(WARNING) << "failed ";
    }
}

Then I restat the redis-server, after the reconnection successfully completed, no reply such as "get hello: xx" outputs, but "set" command just work fine!
so how should I fix this issue.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions