KubeMQ
Client SDKsRustHow-to guidesConnection

Close a KubeMQ Rust Client

Close a KubeMQ Rust client gracefully after connecting and verifying with a ping to release resources.

Overview

Closing a client isn't an afterthought — it tells the broker and your own process that this connection is done, so both sides release what they were holding for it. A KubeMQ client is more than a socket: it's a gRPC channel plus whatever child tasks (subscriptions) it has spawned to service it. Skip the close and those linger — subscriptions keep running, the channel stays open — and in short-lived binaries or test suites you leak connections until the process exits.

Awaiting client.close() cancels all child tasks, waits out the drain timeout, then closes the gRPC channel. Once it returns, the client is in a terminal closed state — every call after that fails fast with KubemqError::ClientClosed.

Gotchas: the drain window is bounded, not unlimited, so a slow consumer can still lose the tail of a burst if you close mid-stream; close() is idempotent, so calling it twice is safe, but a closed client is still dead forever — no reconnect on the same instance, build a new one; and forgetting to await close(), or letting the client simply drop, skips the graceful drain, leaving in-flight subscriptions to end abruptly.

Prerequisites

  • KubeMQ server running on localhost:50000
  • Rust SDK installed (cargo add kubemq)

Code

main.rs
use kubemq::prelude::*;

#[tokio::main]
async fn main() -> kubemq::Result<()> {
    let client = KubemqClient::builder()
        .host("localhost")
        .port(50000)
        .build()
        .await?;

    let info = client.ping().await?;
    println!("Connected. Server: {}", info.version);

    client.close().await?;
    println!("Client closed");

    Ok(())
}

How It Works

  • close() cancels all child tasks (subscriptions), waits for the drain timeout, then closes the gRPC channel.
  • The method is idempotent — safe to call multiple times.
  • After close(), all subsequent operations return KubemqError::ClientClosed.
  • Run the program while the server from the prerequisites is available.

Was this page helpful?

On this page