XU XUv0.16.0

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

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

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

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

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

示例代码 · API 声明

示例代码

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

30_standalone_etcd/main.xu

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

Xu本地 SDK 示例
@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]")
}

API 声明

open

XuAPI
pub fn open(endpoint string) Client

用途

打开资源或建立网络连接。

参数

endpoint string
endpoint(string)

返回值

Client — 该类型的值

查看用法示例

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 — 该类型的值

查看用法示例

struct Response

XuAPI
pub struct Response {
    pub status_code int
    pub body string
}

用途

Etcd 服务端响应结果,包含状态码、响应头与正文数据。

公开字段

status_code int
字段(int)
body string
正文

struct Client

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

用途

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

公开字段

host string
主机
port int
端口
secure bool
字段(bool)

new_client

XuAPI
pub fn new_client(endpoint string) Client

用途

构造 client。

参数

endpoint string
endpoint(string)

返回值

Client — 该类型的值

($&Client) request

XuAPI
pub fn (c $&Client) request(path string, body_json string) Response

用途

$&Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
path string
文件系统路径(string)
body_json string
body_json(string)

返回值

Response — 该类型的值

($&Client) put

XuAPI
pub fn (c $&Client) put(key string, value string) int

用途

写入或更新指定键对应的值数据。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

int — 该类型的值

($&Client) put_with_lease

XuAPI
pub fn (c $&Client) put_with_lease(key string, value string, lease_id int) int

用途

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

参数

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

返回值

int — 该类型的值

($&Client) get

XuAPI
pub fn (c $&Client) get(key string) string

用途

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

参数

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

返回值

string — 结果字符串

($&Client) exists

XuAPI
pub fn (c $&Client) exists(key string) bool

用途

在 $&Client 上执行 exists 操作。

参数

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

返回值

bool — 是否成立

($&Client) delete

XuAPI
pub fn (c $&Client) delete(key string) int

用途

$&Client 上的操作:删除。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

int — 该类型的值

($&Client) lease_grant

XuAPI
pub fn (c $&Client) lease_grant(ttl_seconds int) int

用途

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

参数

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

返回值

int — 该类型的值

($&Client) lease_revoke

XuAPI
pub fn (c $&Client) lease_revoke(lease_id int) int

用途

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

参数

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

返回值

int — 该类型的值

($&Client) lease_keepalive

XuAPI
pub fn (c $&Client) lease_keepalive(lease_id int) int

用途

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

参数

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

返回值

int — 该类型的值

struct Pool

XuAPI
pub 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 string
主机
port int
端口
secure bool
字段(bool)
endpoint string
字段(string)
state chan pool.State
字段(通道;类型 chan pool.State)
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 — 该类型的值

(&Pool) get

XuAPI
pub fn (p &Pool) get() Client

用途

在 &Pool 上执行 get 操作。

参数

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

返回值

Client — 该类型的值

(&Pool) dup

XuAPI
pub fn (p &Pool) dup() Pool

用途

创建并返回持有独立状态或底层的副本对象。

参数

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

返回值

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) discard_conn

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

用途

&Pool 上的操作:丢弃连接。

参数

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

返回值

不返回值。

(&Pool) put

XuAPI
pub fn (p &Pool) put(key string, value string) int

用途

写入或更新指定键对应的值数据。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

int — 该类型的值

(&Pool) get_key

XuAPI
pub fn (p &Pool) get_key(key string) string

用途

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

参数

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

返回值

string — 结果字符串

(&Pool) delete

XuAPI
pub fn (p &Pool) delete(key string) int

用途

&Pool 上的操作:删除。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

int — 该类型的值

(&Pool) exists

XuAPI
pub fn (p &Pool) exists(key string) bool

用途

在 &Pool 上执行 exists 操作。

参数

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

返回值

bool — 是否成立

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

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

参数

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

返回值

不返回值。