XU XUv0.16.0

#github.com/xulang-org/packages/zookeeper

import: zookeeper #github.com/xulang-org/packages/zookeeper

counts: 5 functions, 17 methods, 3 types, 0 constants, 0 method sets

Apache ZooKeeper Distributed Coordination Driver for Xu Communicates via 4LW (Four-Letter Words) and native TCP commands.

github.com/xulang-org/packages · 1.17.2 · Install and use

Examples · API declarations

Examples

These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.

45_standalone_zookeeper/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@main

zookeeper #github.com/xulang-org/packages/zookeeper
#errors
#strings

fn verify_zookeeper_pool() {
    println("--- 1. Testing ZooKeeper Connection Pool ---")
    $p := zookeeper.open_pool("127.0.0.1:2181", 5)

    // 1. Connection Reuse
    fd1 := p.get_conn()
    println("   Borrow 1 fd: " + str(fd1))
    p.put_conn(fd1)

    fd2 := p.get_conn()
    println("   Borrow 2 (after return) fd: " + str(fd2))
    if fd1 == fd2 {
        println("   [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
    } else {
        println("   [WARN] Different fd allocated: " + str(fd1) + " vs " + str(fd2))
    }

    // 2. Multi-connection concurrent borrow
    fd3 := p.get_conn()
    println("   Borrow 3 (concurrently) fd: " + str(fd3))
    if fd2 != fd3 {
        println("   [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
    } else {
        println("   [FAIL] Expected distinct fds for concurrent connections!")
    }
    p.put_conn(fd2)
    p.put_conn(fd3)

    p.close()
    println("   [PASS] ZooKeeper Pool closed cleanly.")
}

fn verify_zookeeper_4lw() {
    println("\n--- 2. Testing ZooKeeper 4LW Commands ---")
    $p := zookeeper.open_pool("127.0.0.1:2181", 5)

    // 1. ruok
    ok := p.ruok()
    if ok {
        println("   [PASS] ZooKeeper health check ruok -> imok")
    } else {
        println("   [NOTE] ZooKeeper 2181 not responding or not in standalone mode")
    }

    p.close()
}

fn main() {
    println("=== Xu ZooKeeper Native Driver Verification ===\n")
    verify_zookeeper_pool()
    verify_zookeeper_4lw()
    println("\n[ZOOKEEPER VERIFICATION 100% COMPLETED]")
}

API declarations

struct Client

XuAPI
pub struct Client {
    pub host string
    pub port int
}

Purpose

Zookeeper Client session maintaining connection and operations.

Public fields

host string
host
port int
port

new_client

XuAPI
pub fn new_client(endpoint string) Client

Purpose

Construct client.

Parameters

endpoint string
endpoint (string)

Returns

Client — a value of this type

(Client) exec_4lw

XuAPI
pub fn (c Client) exec_4lw(cmd string) string

Purpose

Operation on Client: exec 4lw. Throws on failure; handle the error or propagate it to the caller.

Parameters

c Client
The Client instance used by this call (value receiver).
cmd string
cmd (string)

Returns

string — result string

(Client) ruok

XuAPI
pub fn (c Client) ruok() bool

Purpose

Execute ruok on Client.

Parameters

c Client
The Client instance used by this call (value receiver).

Returns

bool — true or false

(Client) stat

XuAPI
pub fn (c Client) stat() string

Purpose

Operation on Client: read metadata. Throws on failure; handle the error or propagate it to the caller.

Parameters

c Client
The Client instance used by this call (value receiver).

Returns

string — result string

(Client) mntr

XuAPI
pub fn (c Client) mntr() string

Purpose

Execute mntr on Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c Client
The Client instance used by this call (value receiver).

Returns

string — result string

struct Pool

XuAPI
pub struct Pool {
    pub client Client
    pub idle chan int
    pub max_idle int
}

Purpose

Zookeeper Connection pool manager scheduling reusable socket connections.

Public fields

client Client
field (Client)
idle chan int
field (channel; type chan int)
max_idle int
field (int)

new_pool

XuAPI
pub fn new_pool(endpoint string, max_idle int) Pool

Purpose

Construct pool.

Parameters

endpoint string
endpoint (string)
max_idle int
max_idle (int)

Returns

Pool — a value of this type

open_pool

XuAPI
pub fn open_pool(endpoint string, max_idle int) Pool

Purpose

Open pool.

Parameters

endpoint string
endpoint (string)
max_idle int
max_idle (int)

Returns

Pool — a value of this type

See usage example

(&Pool) get_conn

XuAPI
pub fn (p &Pool) get_conn() int

Purpose

Operation on &Pool: read conn.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

int — a value of this type

(&Pool) put_conn

XuAPI
pub fn (p &Pool) put_conn(fd int)

Purpose

Operation on &Pool: put connection.

Parameters

p &Pool
The Pool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&Pool) ruok

XuAPI
pub fn (p &Pool) ruok() bool

Purpose

Execute ruok on &Pool.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

bool — true or false

(&Pool) stat

XuAPI
pub fn (p &Pool) stat() string

Purpose

Operation on &Pool: read metadata. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

string — result string

(&Pool) mntr

XuAPI
pub fn (p &Pool) mntr() string

Purpose

Execute mntr on &Pool. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

string — result string

(&Pool) close

XuAPI
pub fn (p &Pool) close()

Purpose

Operation on &Pool: close the resource.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

This call does not return a value.

struct TlsClient

XuAPI
pub struct TlsClient {
    pub conn transport.Conn
    pub host string
    pub port int
}

Purpose

Zookeeper Client session maintaining connection and operations.

Public fields

conn transport.Conn
field (transport.Conn)
host string
host
port int
port

tls_connect

XuAPI
pub fn tls_connect(endpoint string, default_port int) TlsClient

Purpose

Tls connect. Throws on failure; handle the error or propagate it to the caller.

Parameters

endpoint string
endpoint (string)
default_port int
default_port (int)

Returns

TlsClient — a value of this type

tls_connect_config

XuAPI
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient

Purpose

Tls connect config. Throws on failure; handle the error or propagate it to the caller.

Parameters

endpoint string
endpoint (string)
default_port int
default_port (int)
cfg tls.Config
cfg (tls.Config)

Returns

TlsClient — a value of this type

($&TlsClient) write_all

XuAPI
pub fn (c $&TlsClient) write_all(data string) int

Purpose

Operation on $&TlsClient: write all. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
data string
data (string)

Returns

int — a value of this type

($&TlsClient) read

XuAPI
pub fn (c $&TlsClient) read(max int) string

Purpose

Operation on $&TlsClient: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
max int
maximum (int)

Returns

string — result string

($&TlsClient) read_exact

XuAPI
pub fn (c $&TlsClient) read_exact(need int) string

Purpose

Operation on $&TlsClient: read exact. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
need int
need (int)

Returns

string — result string

($&TlsClient) close

XuAPI
pub fn (c $&TlsClient) close() int

Purpose

Operation on $&TlsClient: close the resource. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).

Returns

int — a value of this type

($&TlsClient) set_deadline

XuAPI
pub fn (c $&TlsClient) set_deadline(t_ns int) int

Purpose

Operation on $&TlsClient: set deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type

($&TlsClient) set_read_deadline

XuAPI
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int

Purpose

Operation on $&TlsClient: set read deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type

($&TlsClient) set_write_deadline

XuAPI
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int

Purpose

Operation on $&TlsClient: set write deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type