XU XUv0.16.0

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

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

counts: 4 functions, 19 methods, 3 types, 0 constants, 0 method sets

etcd v3 Cloud-Native Distributed Key-Value & Coordination Driver for Xu Features Native HTTP/JSON communication with etcd v3 gRPC-Gateway. Standard Base64 Key-Value storage (put, get, delete). Distributed Lease lifecycle (lease_grant, lease_revoke, lease_keepalive). Connection pooling via reusable TCP socket channels.

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.

30_standalone_etcd/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@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 declarations

open

XuAPI
pub fn open(endpoint string) Client

Purpose

Open resource or establish connection.

Parameters

endpoint string
endpoint (string)

Returns

Client — a value of this type

See usage example

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

struct Response

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

Purpose

Etcd Server response payload containing status, headers, and body.

Public fields

status_code int
field (int)
body string
body

struct Client

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

Purpose

Etcd Client session maintaining connection and operations.

Public fields

host string
host
port int
port
secure bool
field (bool)

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

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

Purpose

Execute request on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
path string
filesystem path (string)
body_json string
body_json (string)

Returns

Response — a value of this type

($&Client) put

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

Purpose

Store or update the value associated with the specified key. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
value string
value (string)

Returns

int — a value of this type

($&Client) put_with_lease

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

Purpose

Operation on $&Client: put with lease. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)
value string
value (string)
lease_id int
lease_id (int)

Returns

int — a value of this type

($&Client) get

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

Purpose

Execute get on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)

Returns

string — result string

($&Client) exists

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

Purpose

Execute exists on $&Client.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)

Returns

bool — true or false

($&Client) delete

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

Purpose

Execute delete on $&Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
key string
key (string)

Returns

int — a value of this type

($&Client) lease_grant

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

Purpose

Operation on $&Client: lease grant. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
ttl_seconds int
ttl_seconds (int)

Returns

int — a value of this type

($&Client) lease_revoke

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

Purpose

Operation on $&Client: lease revoke. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
lease_id int
lease_id (int)

Returns

int — a value of this type

($&Client) lease_keepalive

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

Purpose

Operation on $&Client: lease keepalive. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
lease_id int
lease_id (int)

Returns

int — a value of this type

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
}

Purpose

Connection Pool for etcd v3. Manages reusable TCP/HTTP socket connections.

Public fields

host string
host
port int
port
secure bool
field (bool)
endpoint string
field (string)
state chan pool.State
field (channel; type chan pool.State)
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

(&Pool) get

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

Purpose

Execute get on &Pool.

Parameters

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

Returns

Client — a value of this type

(&Pool) dup

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

Purpose

Shares sockets and close state; clones configuration strings.

Parameters

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

Returns

Pool — a value of this type

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

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

Purpose

Operation on &Pool: discard 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) put

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

Purpose

Store or update the value associated with the specified key. Throws on failure; handle the error or propagate it to the caller.

Parameters

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

Returns

int — a value of this type

(&Pool) get_key

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

Purpose

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

Parameters

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

Returns

string — result string

(&Pool) delete

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

Purpose

Execute delete 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).
key string
key (string)

Returns

int — a value of this type

(&Pool) exists

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

Purpose

Execute exists on &Pool.

Parameters

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

Returns

bool — true or false

(&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.