open
pub fn open(endpoint string) Client用途
打开资源或建立网络连接。
参数
endpointstring- endpoint(string)
返回值
Client — 该类型的值
#github.com/xulang-org/packages/etcd扩展包 `etcd #github.com/xulang-org/packages/etcd`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
30_standalone_etcd/main.xu@main
etcd #github.com/xulang-org/packages/etcd
#errors
fn verify_etcd_kv() {
println("--- 1. Testing etcd v3 KV Operations ---")
$client := etcd.open("http://127.0.0.1:2379")
key := "/services/payment/config"
val := "version=2.0;timeout=5s"
// 1. Put KV
println(" Writing key: " + key)
client.put(key + "", val + "") >>> (err) {
println(" [FAIL] etcd put error: " + err.msg())
<<
}
println(" [PASS] Key written.")
// 2. Get KV
println(" Reading key: " + key)
got := client.get(key + "") >>> (err) {
println(" [FAIL] etcd get error: " + err.msg())
<<
}
println(" Retrieved value: " + got)
if got == val {
println(" [PASS] Value matches exactly!")
} else {
println(" [FAIL] Value mismatch!")
}
// 3. Delete KV
println(" Deleting key: " + key)
client.delete(key + "") >>> (err) {
println(" [FAIL] etcd delete error: " + err.msg())
<<
}
println(" [PASS] Key deleted.")
}
fn verify_etcd_lease() {
println("\n--- 2. Testing etcd v3 Lease Lifecycle ---")
$client := etcd.open("http://127.0.0.1:2379")
// Grant lease: 60 seconds
println(" Granting 60s lease...")
lease_id := client.lease_grant(60) >>> (err) {
println(" [FAIL] lease_grant error: " + err.msg())
<<
}
println(" [PASS] Granted Lease ID: " + str(lease_id))
// Put with lease
service_key := "/discovery/nodes/node-1"
service_addr := "192.168.1.100:8080"
println(" Registering node with lease: " + service_key)
client.put_with_lease(service_key + "", service_addr + "", lease_id) >>> (err) {
println(" [FAIL] put_with_lease error: " + err.msg())
<<
}
println(" [PASS] Service registered with lease.")
// Verify retrieval
got := client.get(service_key + "") >>> (err) {
println(" [FAIL] get leased key error: " + err.msg())
<<
}
println(" Leased node address: " + got)
// Revoke lease
println(" Revoking lease...")
client.lease_revoke(lease_id) >>> (err) {
println(" [FAIL] lease_revoke error: " + err.msg())
<<
}
println(" [PASS] Lease revoked successfully.")
}
fn verify_etcd_pool() {
println("\n--- 3. Testing etcd Connection Pool ---")
$p := etcd.open_pool("http://127.0.0.1:2379", 5)
// 1. Connection Reuse Test
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)
// 3. Delegated Key-Value Operations via Pool
println(" Testing delegated operations via pool...")
pool_key := "/pool/service/worker-1"
pool_val := "status=online;threads=16"
p.put(pool_key + "", pool_val + "") >>> (err) {
println(" [FAIL] pool.put error: " + err.msg())
<<
}
got_val := p.get_key(pool_key + "") >>> (err) {
println(" [FAIL] pool.get_key error: " + err.msg())
<<
}
println(" Retrieved via pool: " + got_val)
if got_val == pool_val {
println(" [PASS] Pool delegated put & get matched exactly!")
} else {
println(" [FAIL] Pool delegated get mismatch!")
}
p.delete(pool_key + "") >>> (err) {
println(" [FAIL] pool.delete error: " + err.msg())
<<
}
println(" [PASS] Pool delegated delete completed.")
p.close()
println(" [PASS] etcd Pool closed and verified.")
}
fn main() {
println("=== Xu etcd v3 Native Driver Verification ===\n")
verify_etcd_kv()
verify_etcd_lease()
verify_etcd_pool()
println("\n[ETCD VERIFICATION 100% COMPLETED]")
}openpub fn open(endpoint string) Client打开资源或建立网络连接。
endpoint stringClient — 该类型的值
open_poolpub fn open_pool(endpoint string, max_idle int) Pool执行 open pool 操作。
endpoint stringmax_idle intPool — 该类型的值
struct Responsepub struct Response {
pub status_code int
pub body string
}Etcd 服务端响应结果,包含状态码、响应头与正文数据。
status_code intbody stringstruct Clientpub struct Client {
pub host string
pub port int
pub secure bool
}Etcd 客户端会话实例,维护与服务端的连接并提供操作接口。
host stringport intsecure boolnew_clientpub fn new_client(endpoint string) Client构造 client。
endpoint stringClient — 该类型的值
($&Client) requestpub fn (c $&Client) request(path string, body_json string) Response$&Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。path stringbody_json stringResponse — 该类型的值
($&Client) putpub fn (c $&Client) put(key string, value string) int写入或更新指定键对应的值数据。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringvalue stringint — 该类型的值
($&Client) put_with_leasepub fn (c $&Client) put_with_lease(key string, value string, lease_id int) int$&Client 上的操作:执行 put with lease 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringvalue stringlease_id intint — 该类型的值
($&Client) getpub fn (c $&Client) get(key string) string在 $&Client 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringstring — 结果字符串
($&Client) existspub fn (c $&Client) exists(key string) bool在 $&Client 上执行 exists 操作。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringbool — 是否成立
($&Client) deletepub fn (c $&Client) delete(key string) int$&Client 上的操作:删除。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。key stringint — 该类型的值
($&Client) lease_grantpub fn (c $&Client) lease_grant(ttl_seconds int) int$&Client 上的操作:执行 lease grant 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。ttl_seconds intint — 该类型的值
($&Client) lease_revokepub fn (c $&Client) lease_revoke(lease_id int) int$&Client 上的操作:执行 lease revoke 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。lease_id intint — 该类型的值
($&Client) lease_keepalivepub fn (c $&Client) lease_keepalive(lease_id int) int$&Client 上的操作:执行 lease keepalive 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ClientClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。lease_id intint — 该类型的值
struct Poolpub struct Pool {
pub host string
pub port int
pub secure bool
pub endpoint string
pub state chan pool.State
pub max_idle int
}etcd v3 分布式键值存储连接池管理器。
host stringport intsecure boolendpoint stringstate chan pool.Statemax_idle intnew_poolpub fn new_pool(endpoint string, max_idle int) Pool构造 pool。
endpoint stringmax_idle intPool — 该类型的值
(&Pool) getpub fn (p &Pool) get() Client在 &Pool 上执行 get 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。Client — 该类型的值
(&Pool) duppub fn (p &Pool) dup() Pool创建并返回持有独立状态或底层的副本对象。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。Pool — 该类型的值
(&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) discard_connpub fn (p &Pool) discard_conn(fd int)&Pool 上的操作:丢弃连接。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&Pool) putpub fn (p &Pool) put(key string, value string) int写入或更新指定键对应的值数据。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。key stringvalue stringint — 该类型的值
(&Pool) get_keypub fn (p &Pool) get_key(key string) string&Pool 上的操作:读取 key。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。key stringstring — 结果字符串
(&Pool) deletepub fn (p &Pool) delete(key string) int&Pool 上的操作:删除。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。key stringint — 该类型的值
(&Pool) existspub fn (p &Pool) exists(key string) bool在 &Pool 上执行 exists 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。key stringbool — 是否成立
(&Pool) closepub fn (p &Pool) close()&Pool 上的操作:关闭资源。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。不返回值。