sewenew / redis-plus-plus

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

[BUG] Failed to get reply: Resource temporarily unavailable. #604

Closed ivantishchenko closed 4 weeks ago

ivantishchenko commented 4 weeks ago

Describe the bug I have one connection to redis std::shared_ptr<sw::redis::Redis> _redis and trying to reuse it from multiple threads.

For this I use a thread pool with 16 threads.

To Reproduce Call from at least 16 threads on ~100 different keys. In 1-5 cases you will receive Failed to get reply: Resource temporarily unavailable.

    try
    {
        return _redis->get(key);
    }
    catch (const sw::redis::Error &exception)
    {
        std::cout << exception.what() << std::endl;
    }

Expected behavior All keys can be fetched without an exception.

Environment:

ivantishchenko commented 4 weeks ago

After digging in the code I realized that REDIS_SOCKET_TIMEOUT and REDIS_CONNECTION_TIMEOUT were both set to 500ms. Increasing these timeouts solved the issue.

    sw::redis::ConnectionOptions opt;
    opt.host = host;
    opt.port = port;
    opt.socket_timeout = REDIS_SOCKET_TIMEOUT;
    opt.connect_timeout = REDIS_CONNECTION_TIMEOUT;
sewenew commented 4 weeks ago

@ivantishchenko Resource temporarily unavailable means request timeout. You can increase your socket_timeout or set it to 0ms to make the connection blocking for response without timeout.