open
pub fn open(endpoint string) ClientPurpose
Open resource or establish connection.
Parameters
endpointstring- endpoint (string)
Returns
Client — a value of this type
#github.com/xulang-org/packages/etcdetcd 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
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@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) ClientOpen resource or establish connection.
endpoint stringClient — a value of this type
open_poolpub fn open_pool(endpoint string, max_idle int) PoolOpen pool.
endpoint stringmax_idle intPool — a value of this type
struct Responsepub struct Response {
pub status_code int
pub body string
}Etcd Server response payload containing status, headers, and body.
status_code intbody stringstruct Clientpub struct Client {
pub host string
pub port int
pub secure bool
}Etcd Client session maintaining connection and operations.
host stringport intsecure boolnew_clientpub fn new_client(endpoint string) ClientConstruct client.
endpoint stringClient — a value of this type
($&Client) requestpub fn (c $&Client) request(path string, body_json string) ResponseExecute request on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).path stringbody_json stringResponse — a value of this type
($&Client) putpub fn (c $&Client) put(key string, value string) intStore or update the value associated with the specified key. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringvalue stringint — a value of this type
($&Client) put_with_leasepub fn (c $&Client) put_with_lease(key string, value string, lease_id int) intOperation on $&Client: put with lease. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringvalue stringlease_id intint — a value of this type
($&Client) getpub fn (c $&Client) get(key string) stringExecute get on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringstring — result string
($&Client) existspub fn (c $&Client) exists(key string) boolExecute exists on $&Client.
c $&ClientClient instance used by this call (exclusive borrow).key stringbool — true or false
($&Client) deletepub fn (c $&Client) delete(key string) intExecute delete on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).key stringint — a value of this type
($&Client) lease_grantpub fn (c $&Client) lease_grant(ttl_seconds int) intOperation on $&Client: lease grant. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).ttl_seconds intint — a value of this type
($&Client) lease_revokepub fn (c $&Client) lease_revoke(lease_id int) intOperation on $&Client: lease revoke. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).lease_id intint — a value of this type
($&Client) lease_keepalivepub fn (c $&Client) lease_keepalive(lease_id int) intOperation on $&Client: lease keepalive. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).lease_id intint — a value of this type
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
}Connection Pool for etcd v3. Manages reusable TCP/HTTP socket connections.
host stringport intsecure boolendpoint stringstate chan pool.Statemax_idle intnew_poolpub fn new_pool(endpoint string, max_idle int) PoolConstruct pool.
endpoint stringmax_idle intPool — a value of this type
(&Pool) getpub fn (p &Pool) get() ClientExecute get on &Pool.
p &PoolPool instance used by this call (borrow).Client — a value of this type
(&Pool) duppub fn (p &Pool) dup() PoolShares sockets and close state; clones configuration strings.
p &PoolPool instance used by this call (borrow).Pool — a value of this type
(&Pool) get_connpub fn (p &Pool) get_conn() intOperation on &Pool: read conn.
p &PoolPool instance used by this call (borrow).int — a value of this type
(&Pool) put_connpub fn (p &Pool) put_conn(fd int)Operation on &Pool: put connection.
p &PoolPool instance used by this call (borrow).fd intThis call does not return a value.
(&Pool) discard_connpub fn (p &Pool) discard_conn(fd int)Operation on &Pool: discard connection.
p &PoolPool instance used by this call (borrow).fd intThis call does not return a value.
(&Pool) putpub fn (p &Pool) put(key string, value string) intStore or update the value associated with the specified key. Throws on failure; handle the error or propagate it to the caller.
p &PoolPool instance used by this call (borrow).key stringvalue stringint — a value of this type
(&Pool) get_keypub fn (p &Pool) get_key(key string) stringOperation on &Pool: read key. Throws on failure; handle the error or propagate it to the caller.
p &PoolPool instance used by this call (borrow).key stringstring — result string
(&Pool) deletepub fn (p &Pool) delete(key string) intExecute delete on &Pool. Throws on failure; handle the error or propagate it to the caller.
p &PoolPool instance used by this call (borrow).key stringint — a value of this type
(&Pool) existspub fn (p &Pool) exists(key string) boolExecute exists on &Pool.
p &PoolPool instance used by this call (borrow).key stringbool — true or false
(&Pool) closepub fn (p &Pool) close()Operation on &Pool: close the resource.
p &PoolPool instance used by this call (borrow).This call does not return a value.