Custom Timeouts
Configure custom timeouts and reconnection policies.
Overview
Every client operation has an implicit deadline — how long an RPC blocks before giving up, how long reconnection retries wait between attempts, and how long a cached query response stays valid before it's considered stale. 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 these explicitly is how you trade fast-fail behavior against tolerance for transient slowness.
Each setting targets a different concern. rpc_timeout bounds how long command and query operations wait for a response before failing; reconnect_policy's initial_delay and max_attempts govern the exponential backoff and attempt budget when the connection drops; and default_cache_ttl sets how long a query response is reused from cache before a fresh request is made. Gotchas: an rpc_timeout shorter than the server's real processing time causes spurious failures, not faster detection of a genuinely broken handler; a bounded max_attempts means reconnection eventually gives up for good — pick a value that matches how long an outage should be tolerated; and default_cache_ttl trades staleness for load, so it's the wrong knob to reach for if your query results must always reflect the latest state.
Prerequisites
- KubeMQ server running on
localhost:50000 - Elixir SDK installed (
{:kubemq, "~> 1.0"}in mix.exs)
Code
IO.puts("Connecting with custom timeouts...")
{:ok, client} =
KubeMQ.Client.start_link(
address: "localhost:50000",
client_id: "elixir-timeout-example",
rpc_timeout: 15_000,
reconnect_policy: [initial_delay: 2_000, max_attempts: 10],
default_cache_ttl: 300_000
)
IO.puts("Connected with custom timeout configuration!")
IO.puts("Connection state: #{KubeMQ.Client.connection_state(client)}")
case KubeMQ.Client.ping(client) do
{:ok, info} -> IO.puts("Server version: #{info.version}")
{:error, err} -> IO.puts("Ping failed: #{err.message}")
end
KubeMQ.Client.close(client)
IO.puts("Done.")How It Works
rpc_timeoutsets the default timeout for command and query operations in millisecondsreconnect_policyconfigures automatic reconnection withinitial_delay,max_attempts, and exponential backoffdefault_cache_ttlsets the default cache TTL for query responses
Related
Was this page helpful?