struct Client
pub struct Client {
pub host string
pub port int
}用途
Zookeeper 客户端会话实例,维护与服务端的连接并提供操作接口。
公开字段
hoststring- 主机
portint- 端口
#github.com/xulang-org/packages/zookeeper扩展包 `zookeeper #github.com/xulang-org/packages/zookeeper`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
45_standalone_zookeeper/main.xu@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]")
}struct Clientpub struct Client {
pub host string
pub port int
}Zookeeper 客户端会话实例,维护与服务端的连接并提供操作接口。
host stringport intnew_clientpub fn new_client(endpoint string) Client构造 client。
endpoint stringClient — 该类型的值
(Client) exec_4lwpub fn (c Client) exec_4lw(cmd string) stringClient 上的操作:执行 exec 4lw 操作。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。cmd stringstring — 结果字符串
(Client) ruokpub fn (c Client) ruok() bool在 Client 上执行 ruok 操作。
c ClientClient 实例;按值接收。bool — 是否成立
(Client) statpub fn (c Client) stat() stringClient 上的操作:读取元信息。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。string — 结果字符串
(Client) mntrpub fn (c Client) mntr() string在 Client 上执行 mntr 操作。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。string — 结果字符串
struct Poolpub struct Pool {
pub client Client
pub idle chan int
pub max_idle int
}Zookeeper 连接池管理器,并发安全地维护和调度复用套接字连接。
client Clientidle chan intmax_idle intnew_poolpub fn new_pool(endpoint string, max_idle int) Pool构造 pool。
endpoint stringmax_idle intPool — 该类型的值
open_poolpub fn open_pool(endpoint string, max_idle int) Pool执行 open pool 操作。
endpoint stringmax_idle intPool — 该类型的值
(&Pool) get_connpub fn (p &Pool) get_conn() int&Pool 上的操作:读取 conn。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&Pool) put_connpub fn (p &Pool) put_conn(fd int)&Pool 上的操作:执行 put conn 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&Pool) ruokpub fn (p &Pool) ruok() bool在 &Pool 上执行 ruok 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Pool) statpub fn (p &Pool) stat() string&Pool 上的操作:读取元信息。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。string — 结果字符串
(&Pool) mntrpub fn (p &Pool) mntr() string在 &Pool 上执行 mntr 操作。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。string — 结果字符串
(&Pool) closepub fn (p &Pool) close()&Pool 上的操作:关闭资源。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。不返回值。
struct TlsClientpub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}Zookeeper 客户端会话实例,维护与服务端的连接并提供操作接口。
conn transport.Connhost stringport inttls_connectpub fn tls_connect(endpoint string, default_port int) TlsClient执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intTlsClient — 该类型的值
tls_connect_configpub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intcfg tls.ConfigTlsClient — 该类型的值
($&TlsClient) write_allpub fn (c $&TlsClient) write_all(data string) int$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。data stringint — 该类型的值
($&TlsClient) readpub fn (c $&TlsClient) read(max int) string$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。max intstring — 结果字符串
($&TlsClient) read_exactpub fn (c $&TlsClient) read_exact(need int) string$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。need intstring — 结果字符串
($&TlsClient) closepub fn (c $&TlsClient) close() int$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
($&TlsClient) set_deadlinepub fn (c $&TlsClient) set_deadline(t_ns int) int$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_read_deadlinepub fn (c $&TlsClient) set_read_deadline(t_ns int) int$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_write_deadlinepub fn (c $&TlsClient) set_write_deadline(t_ns int) int$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值