sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.64k stars 351 forks source link

[BUG] Vague error message when "failed to connect to server" happens #445

Closed leonliao closed 1 year ago

leonliao commented 1 year ago

Describe the bug When redis++ encounters connection attempt failure to a Redis node, errors with below messages are thrown

failed to connect to server: Connection refused

And from the message, there is no clue which Redis node was down. Thus, poor info could be provided to Redis server operators.

To Reproduce

#include <sw/redis++/async_redis++.h>

#include <iostream>
#include <future>
#include <unistd.h>

using namespace sw::redis;
using namespace std;

int main(int argc, char **argv) {
    if (argc < 3) {
        std::cout << argv[0] << " host port" << std::endl; 
        return -1;
    }
    ConnectionOptions opts;
    opts.host = argv[1];
    opts.port = atoi(argv[2]);

    ConnectionPoolOptions pool_opts;
    pool_opts.size = 2;

    auto async_cluster = AsyncRedisCluster(opts, pool_opts);

    Future<bool> set_res = async_cluster.set("key", "val1"); 
    cout << set_res.get() << endl;

    cout << "set ok...." << endl;

    for(int i=1;i<=100000;i++) {
            Future<Optional<string>> get_res = async_cluster.get("key");//GET key repeatedly

        try {
            auto resp = get_res.get(); //kill master which serves the "key"
        if(resp) {
            cout << i << ": " << *resp << endl;
        }
        else {
            cout << "key4 not existing" << endl;
        }
        } catch (const sw::redis::IoError &err) {  
            cout << i << ": " << err.what() <<endl;
        }
        usleep(1000000);//sleep 1s

    }

    return 0;
}

Expected behavior Logs showing server info should be printed. Such as

Failed to connect to server (host:1.2.3.4,port: 6397): Connection refused

Environment:

Additional context NA

sewenew commented 1 year ago

Thanks for the suggestion! The problem has been fixed, and you can try it with latest code on dev branch. I'll merge it to master after some tests.

Sorry for the late reply. Too busy these days.

Regards

sewenew commented 1 year ago

The code has been merged into master branch.

Regards