# Custom Timeouts (/sdks/cpp/how-to/connection/custom-timeouts)



## Overview [#overview]

Every client operation has an implicit deadline — how long to wait for the initial connection, how long before a dead socket is detected, how long `Close()` waits for in-flight work to drain, and how large a message payload is allowed to be. The defaults are reasonable for a healthy local network, but they're wrong for high-latency links, connections through load balancers or NAT gateways, or workloads pushing larger-than-typical payloads. Tuning these explicitly trades fast-fail behavior against tolerance for transient slowness and real traffic shapes.

`set_connection_timeout()` bounds the initial dial; `set_keepalive_time()` / `set_keepalive_timeout()` configure gRPC HTTP/2 keepalive pings that detect a stale connection before you try to use it; `set_drain_timeout()` bounds how long `Close()` waits for outstanding calls before forcing the transport shut; and `set_max_receive_message_size()` / `set_max_send_message_size()` override gRPC's default 4 MB payload cap. &#x2A;*Gotchas:** a connection timeout shorter than your network's real handshake latency causes spurious startup failures, not faster detection of a down server; aggressive keepalive pings can flag a slow-but-healthy link as dead; and raising message size limits only helps if the server's own limits are raised to match — otherwise you've just moved the failure from client to server.

## Prerequisites [#prerequisites]

* KubeMQ server running on `localhost:50000`
* C++ SDK installed (vcpkg or CMake FetchContent)
* C++17 compiler (GCC 9+, Clang 9+, MSVC 2019+)

## Code [#code]

```cpp title="main.cc"
// Example: connection/custom_timeouts
//
// Demonstrates how to configure custom timeouts for the KubeMQ client
// including connection timeout, keepalive, drain timeout, and message sizes.
//
// Channel: cpp-connection.custom-timeouts
// Client ID: cpp-connection-custom-timeouts-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).

#include <kubemq/kubemq.h>

#include <chrono>
#include <iostream>

int main() {
    std::cout << "[1] Configuring custom timeouts" << std::endl;

    kubemq::ClientOptions options;
    options.set_address("localhost", 50000);
    options.set_client_id("cpp-connection-custom-timeouts-client");
    // Custom connection timeout (default: 10s)
    options.set_connection_timeout(std::chrono::seconds(15));
    // Custom keepalive settings (default: 10s interval, 20s timeout)
    options.set_keepalive_time(std::chrono::seconds(30));
    options.set_keepalive_timeout(std::chrono::seconds(10));
    // Custom drain timeout for Close() (default: 5s)
    options.set_drain_timeout(std::chrono::seconds(10));
    // Max message sizes (50 MB)
    options.set_max_receive_message_size(50 * 1024 * 1024);
    options.set_max_send_message_size(50 * 1024 * 1024);

    std::cout << "[2] Connecting to localhost:50000" << std::endl;

    auto client_result = kubemq::Client::Create(options);
    if (!client_result.ok()) {
        std::cerr << "[ERROR] Failed to create client: " << client_result.status().message()
                  << std::endl;
        return 1;
    }
    auto& client = *client_result;

    auto ping_result = client->Ping();
    if (!ping_result.ok()) {
        std::cerr << "[ERROR] Ping failed: " << ping_result.status().message() << std::endl;
        return 1;
    }
    std::cout << "[3] Connected with custom timeouts: host=" << ping_result->host
              << " version=" << ping_result->version << std::endl;

    // Close the client explicitly.
    // Note: Client destructor would also handle cleanup (RAII)
    auto close_status = client->Close();
    if (!close_status.ok()) {
        std::cerr << "[ERROR] Close failed: " << close_status.message() << std::endl;
        return 1;
    }
    std::cout << "[4] Client closed" << std::endl;

    return 0;
}
```

## How It Works [#how-it-works]

* Configures custom connection timeout (15s), keepalive settings, drain timeout, and message size limits.
* Uses `set_connection_timeout()`, `set_keepalive_time()`, `set_keepalive_timeout()`, `set_drain_timeout()`.
* Adjusts max message sizes with `set_max_receive_message_size()` and `set_max_send_message_size()`.
* All timeout values use `std::chrono` duration types for type safety.

## Related [#related]

* [C++ SDK Reference](/sdks/cpp/reference/client)
* [Connect](/sdks/cpp/tutorials/connect)
* [Reconnection](/sdks/cpp/how-to/error-handling/reconnection)
