XU XUv0.16.0

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

导入方式: zookeeper #github.com/xulang-org/packages/zookeeper

公开 API: 5 函数, 17 方法, 3 类型, 0 常量, 0 方法集

扩展包 `zookeeper #github.com/xulang-org/packages/zookeeper`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

45_standalone_zookeeper/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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 声明

struct Client

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

用途

Zookeeper 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口

new_client

XuAPI
pub fn new_client(endpoint string) Client

用途

构造 client。

参数

endpoint string
endpoint(string)

返回值

Client — 该类型的值

(Client) exec_4lw

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

用途

Client 上的操作:执行 exec 4lw 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c Client
本次调用使用的 Client 实例;按值接收。
cmd string
cmd(string)

返回值

string — 结果字符串

(Client) ruok

XuAPI
pub fn (c Client) ruok() bool

用途

在 Client 上执行 ruok 操作。

参数

c Client
本次调用使用的 Client 实例;按值接收。

返回值

bool — 是否成立

(Client) stat

XuAPI
pub fn (c Client) stat() string

用途

Client 上的操作:读取元信息。失败时抛出错误,调用方需处理或继续传播。

参数

c Client
本次调用使用的 Client 实例;按值接收。

返回值

string — 结果字符串

(Client) mntr

XuAPI
pub fn (c Client) mntr() string

用途

在 Client 上执行 mntr 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c Client
本次调用使用的 Client 实例;按值接收。

返回值

string — 结果字符串

struct Pool

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

用途

Zookeeper 连接池管理器,并发安全地维护和调度复用套接字连接。

公开字段

client Client
字段(Client)
idle chan int
字段(通道;类型 chan int)
max_idle int
字段(int)

new_pool

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

用途

构造 pool。

参数

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

返回值

Pool — 该类型的值

open_pool

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

用途

执行 open pool 操作。

参数

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

返回值

Pool — 该类型的值

查看用法示例

(&Pool) get_conn

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

用途

&Pool 上的操作:读取 conn。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&Pool) put_conn

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

用途

&Pool 上的操作:执行 put conn 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) ruok

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

用途

在 &Pool 上执行 ruok 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

bool — 是否成立

(&Pool) stat

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

用途

&Pool 上的操作:读取元信息。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

string — 结果字符串

(&Pool) mntr

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

用途

在 &Pool 上执行 mntr 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

string — 结果字符串

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。

struct TlsClient

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

用途

Zookeeper 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

conn transport.Conn
字段(transport.Conn)
host string
主机
port int
端口

tls_connect

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

用途

执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

TlsClient — 该类型的值

tls_connect_config

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

用途

执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

TlsClient — 该类型的值

($&TlsClient) write_all

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

用途

$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
data string
数据(string)

返回值

int — 该类型的值

($&TlsClient) read

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

用途

$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
max int
上限(int)

返回值

string — 结果字符串

($&TlsClient) read_exact

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

用途

$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
need int
need(int)

返回值

string — 结果字符串

($&TlsClient) close

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

用途

$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

($&TlsClient) set_deadline

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

用途

$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_read_deadline

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

用途

$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_write_deadline

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

用途

$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值