KubeMQ
Client SDKsC++How-to guidesConnection

Close a KubeMQ C++ Client

Properly close a KubeMQ C++ SDK client connection to release server resources and avoid leaks on shutdown.

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 operations it has in flight. Skip the close and those linger — the channel stays open — and in short-lived processes or test binaries you leak connections until the process exits.

Calling Close() explicitly drains in-flight operations, then tears down the underlying gRPC channel. The client destructor also runs this cleanup via RAII if you let the object go out of scope, but calling Close() yourself gives you the resulting Status to check instead of a silent teardown.

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; a closed client is dead forever — no reconnect on the same instance, construct a new one; and after Close() returns, every subsequent call on that client returns kErrClientClosed instead of hanging or crashing.

Prerequisites

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

Code

main.cc
// Example: connection/close
//
// Demonstrates how to gracefully close a KubeMQ client connection.
// Close drains in-flight operations before shutting down.
//
// Channel: cpp-connection.close
// Client ID: cpp-connection-close-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).

#include <kubemq/kubemq.h>

#include <iostream>

int main() {
    std::cout << "[1] Connecting to localhost:50000" << std::endl;

    kubemq::ClientOptions options;
    options.set_address("localhost", 50000);
    options.set_client_id("cpp-connection-close-client");

    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;

    // Verify connectivity before closing.
    auto ping_result = client->Ping();
    if (!ping_result.ok()) {
        std::cerr << "[ERROR] Ping failed: " << ping_result.status().message() << std::endl;
        return 1;
    }
    std::cout << "[2] Connected: host=" << ping_result->host << " version=" << ping_result->version
              << std::endl;

    // Close the client gracefully, draining in-flight operations.
    // 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 << "[3] Client closed successfully" << std::endl;

    return 0;
}

How It Works

  • Connects to the broker and verifies with Ping().
  • Calls Close() explicitly to drain in-flight operations before shutting down.
  • The client destructor would also handle cleanup, but explicit close gives you error feedback.
  • After Close(), all subsequent operations return kErrClientClosed.

Was this page helpful?

On this page