Custom Timeouts
Configure connection and per-operation timeouts on the KubeMQ Ruby client to tune reliability under slow networks and load.
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. 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
- KubeMQ server running on
localhost:50000 - Ruby SDK installed (
gem install kubemq)
Code
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'
endHow It Works
default_timeoutsets the gRPC deadline in seconds for unary operations.KeepAliveConfigcontrols periodic pings that detect dead connections and keep firewalls from closing idle connections.ReconnectPolicycontrols the delay between automatic reconnection attempts (exponential backoff viabase_interval,multiplier, andmax_delay).- Review timeouts, channel names, and client IDs before running against shared environments.
Related
Was this page helpful?