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



## Overview [#overview]

Every client operation has an implicit deadline — how long to wait for a response, how long before a dead socket is detected, how long reconnection retries wait between attempts. The defaults are reasonable for a healthy local network, but they're wrong for high-latency links, connections that pass through load balancers or NAT gateways, or servers that occasionally run slow under load. Tuning timeouts explicitly is how you trade fast-fail behavior against tolerance for transient slowness.

Each setting targets a different phase of the client lifecycle. `default_timeout` sets the gRPC deadline in seconds for unary operations like `ping`; `KeepAliveConfig`'s `ping_interval_seconds` / `ping_timeout_seconds` configure periodic pings that detect a dead connection and keep firewalls from silently closing idle ones; and `ReconnectPolicy`'s `base_interval` (with `multiplier` and `max_delay`) governs the exponential backoff between automatic reconnection attempts. &#x2A;*Gotchas:** a `default_timeout` shorter than the server's real processing time causes spurious failures, not faster detection of a genuinely broken operation; an aggressive `ping_interval_seconds` can flag a slow-but-healthy link as dead; and `ReconnectPolicy` has no attempt cap by default, so it will keep retrying against a server that's down for good unless you bound it yourself.

## Prerequisites [#prerequisites]

* KubeMQ server running on `localhost:50000`
* Ruby SDK installed (`gem install kubemq`)

## Code [#code]

```ruby title="main.rb"
require 'kubemq'

address = ENV.fetch('KUBEMQ_ADDRESS', 'localhost:50000')

begin
  client = KubeMQ::PubSubClient.new(
    address: address,
    client_id: 'timeout-example',
    default_timeout: 30,
    keepalive: KubeMQ::KeepAliveConfig.new(
      ping_interval_seconds: 10,
      ping_timeout_seconds: 5
    ),
    reconnect_policy: KubeMQ::ReconnectPolicy.new(
      base_interval: 3.0
    )
  )

  info = client.ping
  puts "Connected with custom timeouts. Server: #{info.version}"
rescue KubeMQ::Error => e
  puts "KubeMQ error: #{e.message}"
ensure
  client&.close
  puts 'Done'
end
```

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

* `default_timeout` sets the gRPC deadline in seconds for unary operations.
* `KeepAliveConfig` controls periodic pings that detect dead connections and keep firewalls from closing idle connections.
* `ReconnectPolicy` controls the delay between automatic reconnection attempts (exponential backoff via `base_interval`, `multiplier`, and `max_delay`).
* Review timeouts, channel names, and client IDs before running against shared environments.

## Related [#related]

* [Ruby SDK Reference](/sdks/ruby/reference)
* [Connect](/sdks/ruby/tutorials/connect)
* [Reconnection](/sdks/ruby/how-to/error-handling/reconnection)
