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/consulPublic API convenience helpers for Consul driver
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.
39_standalone_consul/main.xu@main
consul #github.com/xulang-org/packages/consul
#errors
fn verify_consul_basic() {
println("--- 1. Testing Consul Client Basic Operations ---")
$client := consul.open("http://127.0.0.1:8500")
// 1. Ping
pong := client.ping()
println(" Consul leader ping: " + str(pong))
if !pong {
println(" [FAIL] Consul ping failed.")
<<
}
leader_addr := client.leader() >>> (err) {
println(" [FAIL] get leader failed: " + err.msg())
<<
}
println(" [PASS] Current Raft Leader: " + leader_addr)
// 2. KV Operations
k := "services/payment/rate_limit"
v := "1000req/s"
println(" Writing KV: " + k + " = " + v)
client.put_kv(k + "", v + "")
got_v := client.get_kv(k + "") >>> (err) {
println(" [FAIL] get_kv failed: " + err.msg())
<<
}
println(" Retrieved KV: " + got_v)
if got_v == v {
println(" [PASS] KV value matched exactly!")
} else {
println(" [FAIL] KV mismatch!")
}
// 3. Service Registration
srv_id := "payment-worker-1"
srv_name := "payment-service"
println(" Registering microservice: " + srv_id)
ok_reg := client.register_service(srv_id + "", srv_name + "", "10.0.0.1", 8080)
if ok_reg {
println(" [PASS] Microservice registered.")
} else {
println(" [FAIL] Failed to register service.")
}
srv_info := client.get_service(srv_name + "") >>> (err) {
println(" [FAIL] get_service failed: " + err.msg())
<<
}
println(" Catalog service preview length: " + str(srv_info.len) + " bytes")
client.deregister_service(srv_id + "")
println(" [PASS] Microservice deregistered.")
client.delete_kv(k + "")
println(" [PASS] KV deleted.")
}
fn verify_consul_pool() {
println("\n--- 2. Testing Consul Connection Pool ---")
$p := consul.open_pool("http://127.0.0.1:8500", "", 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 operations via pool
println(" Testing delegated operations via pool...")
pk := "pool/config/app_mode"
pv := "distributed_production"
p.put_kv(pk + "", pv + "")
val := p.get_kv(pk + "") >>> (err) {
println(" [FAIL] Pool get_kv failed: " + err.msg())
<<
}
println(" Retrieved via pool: " + val)
if val == pv {
println(" [PASS] Pool KV matched exactly!")
} else {
println(" [FAIL] Pool KV mismatch!")
}
p.register_service("pool-node-1", "pool-service", "127.0.0.1", 9090)
println(" [PASS] Pool registered microservice.")
p.deregister_service("pool-node-1")
p.delete_kv(pk + "")
println(" [PASS] Pool cleanup completed.")
p.close()
println(" [PASS] Consul Pool closed cleanly.")
}
fn main() {
println("=== Xu HashiCorp Consul Service Discovery Native Driver Verification ===\n")
verify_consul_basic()
verify_consul_pool()
println("\n[CONSUL VERIFICATION 100% COMPLETED]")
}openpub fn open(endpoint string) ClientOpen resource or establish connection.
endpoint stringClient — a value of this type
open_authpub fn open_auth(endpoint string, token string) ClientOpen auth.
endpoint stringtoken stringClient — a value of this type
($&Client) register_servicepub fn (c $&Client) register_service(id string, name string, address string, port int) boolOperation on $&Client: register service.
c $&ClientClient instance used by this call (exclusive borrow).id stringname stringaddress stringport intbool — true or false
($&Client) deregister_servicepub fn (c $&Client) deregister_service(id string) boolOperation on $&Client: deregister service.
c $&ClientClient instance used by this call (exclusive borrow).id stringbool — true or false
($&Client) get_servicepub fn (c $&Client) get_service(name string) stringOperation on $&Client: read service. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).name stringstring — result string
struct Responsepub struct Response {
pub status_code int
pub body string
}Consul Server response payload containing status, headers, and body.
status_code intbody stringstruct Clientpub struct Client {
pub host string
pub port int
pub secure bool
pub token string
}Consul Client session maintaining connection and operations.
host stringport intsecure booltoken stringnew_clientpub fn new_client(endpoint string, token string) ClientConstruct client.
endpoint stringtoken stringClient — a value of this type
($&Client) requestpub fn (c $&Client) request(method string, path string, body 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).method stringpath stringbody stringResponse — a value of this type
($&Client) pingpub fn (c $&Client) ping() boolSend a ping heartbeat request to verify connection liveness.
c $&ClientClient instance used by this call (exclusive borrow).bool — true or false
($&Client) leaderpub fn (c $&Client) leader() stringExecute leader on $&Client. Throws on failure; handle the error or propagate it to the caller.
c $&ClientClient instance used by this call (exclusive borrow).string — result string
($&Client) put_kvpub fn (c $&Client) put_kv(key string, val string) boolOperation on $&Client: put kv.
c $&ClientClient instance used by this call (exclusive borrow).key stringval stringbool — true or false
($&Client) get_kvpub fn (c $&Client) get_kv(key string) stringOperation on $&Client: read kv. 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) delete_kvpub fn (c $&Client) delete_kv(key string) boolOperation on $&Client: delete kv.
c $&ClientClient instance used by this call (exclusive borrow).key stringbool — true or false
struct Poolpub struct Pool {
pub host string
pub port int
pub secure bool
pub token string
pub state chan pool.State
pub max_idle int
}Connection Pool for HashiCorp Consul. Manages reusable TCP/HTTP socket connections.
host stringport intsecure booltoken stringstate chan pool.Statemax_idle intnew_poolpub fn new_pool(endpoint string, token string, max_idle int) PoolConstruct pool.
endpoint stringtoken stringmax_idle intPool — a value of this type
open_poolpub fn open_pool(endpoint string, token string, max_idle int) PoolOpen pool.
endpoint stringtoken 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) put_kvpub fn (p &Pool) put_kv(key string, val string) boolOperation on &Pool: put kv.
p &PoolPool instance used by this call (borrow).key stringval stringbool — true or false
(&Pool) get_kvpub fn (p &Pool) get_kv(key string) stringOperation on &Pool: read kv. 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) delete_kvpub fn (p &Pool) delete_kv(key string) boolOperation on &Pool: delete kv.
p &PoolPool instance used by this call (borrow).key stringbool — true or false
(&Pool) register_servicepub fn (p &Pool) register_service(id string, name string, address string, port int) boolOperation on &Pool: register service.
p &PoolPool instance used by this call (borrow).id stringname stringaddress stringport intbool — true or false
(&Pool) deregister_servicepub fn (p &Pool) deregister_service(id string) boolOperation on &Pool: deregister service.
p &PoolPool instance used by this call (borrow).id stringbool — true or false
(&Pool) get_servicepub fn (p &Pool) get_service(name string) stringOperation on &Pool: read service. Throws on failure; handle the error or propagate it to the caller.
p &PoolPool instance used by this call (borrow).name stringstring — result string
(&Pool) pingpub fn (p &Pool) ping() boolSend a ping heartbeat request to verify connection liveness.
p &PoolPool instance used by this call (borrow).bool — 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.