XU XUv0.16.0

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

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

counts: 5 functions, 22 methods, 3 types, 0 constants, 0 method sets

Public API convenience helpers for Consul driver

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.

39_standalone_consul/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@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]")
}

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_auth

XuAPI
pub fn open_auth(endpoint string, token string) Client

Purpose

Open auth.

Parameters

endpoint string
endpoint (string)
token string
token (string)

Returns

Client — a value of this type

($&Client) register_service

XuAPI
pub fn (c $&Client) register_service(id string, name string, address string, port int) bool

Purpose

Operation on $&Client: register service.

Parameters

c $&Client
The Client instance used by this call (exclusive borrow).
id string
id (string)
name string
name (string)
address string
address (string)
port int
port (int)

Returns

bool — true or false

($&Client) deregister_service

XuAPI
pub fn (c $&Client) deregister_service(id string) bool

Purpose

Operation on $&Client: deregister service.

Parameters

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

Returns

bool — true or false

($&Client) get_service

XuAPI
pub fn (c $&Client) get_service(name string) string

Purpose

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

Parameters

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

Returns

string — result string

struct Response

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

Purpose

Consul 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
    pub token string
}

Purpose

Consul Client session maintaining connection and operations.

Public fields

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

new_client

XuAPI
pub fn new_client(endpoint string, token string) Client

Purpose

Construct client.

Parameters

endpoint string
endpoint (string)
token string
token (string)

Returns

Client — a value of this type

($&Client) request

XuAPI
pub fn (c $&Client) request(method string, path string, body 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).
method string
method (string)
path string
filesystem path (string)
body string
body (string)

Returns

Response — a value of this type

($&Client) ping

XuAPI
pub fn (c $&Client) ping() bool

Purpose

Send a ping heartbeat request to verify connection liveness.

Parameters

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

Returns

bool — true or false

($&Client) leader

XuAPI
pub fn (c $&Client) leader() string

Purpose

Execute leader 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).

Returns

string — result string

($&Client) put_kv

XuAPI
pub fn (c $&Client) put_kv(key string, val string) bool

Purpose

Operation on $&Client: put kv.

Parameters

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

Returns

bool — true or false

($&Client) get_kv

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

Purpose

Operation on $&Client: read kv. 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) delete_kv

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

Purpose

Operation on $&Client: delete kv.

Parameters

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

Returns

bool — true or false

struct Pool

XuAPI
pub struct Pool {
    pub host string
    pub port int
    pub secure bool
    pub token string
    pub state chan pool.State
    pub max_idle int
}

Purpose

Connection Pool for HashiCorp Consul. Manages reusable TCP/HTTP socket connections.

Public fields

host string
host
port int
port
secure bool
field (bool)
token 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, token string, max_idle int) Pool

Purpose

Construct pool.

Parameters

endpoint string
endpoint (string)
token string
token (string)
max_idle int
max_idle (int)

Returns

Pool — a value of this type

open_pool

XuAPI
pub fn open_pool(endpoint string, token string, max_idle int) Pool

Purpose

Open pool.

Parameters

endpoint string
endpoint (string)
token string
token (string)
max_idle int
max_idle (int)

Returns

Pool — a value of this type

See usage example

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

XuAPI
pub fn (p &Pool) put_kv(key string, val string) bool

Purpose

Operation on &Pool: put kv.

Parameters

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

Returns

bool — true or false

(&Pool) get_kv

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

Purpose

Operation on &Pool: read kv. 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_kv

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

Purpose

Operation on &Pool: delete kv.

Parameters

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

Returns

bool — true or false

(&Pool) register_service

XuAPI
pub fn (p &Pool) register_service(id string, name string, address string, port int) bool

Purpose

Operation on &Pool: register service.

Parameters

p &Pool
The Pool instance used by this call (borrow).
id string
id (string)
name string
name (string)
address string
address (string)
port int
port (int)

Returns

bool — true or false

(&Pool) deregister_service

XuAPI
pub fn (p &Pool) deregister_service(id string) bool

Purpose

Operation on &Pool: deregister service.

Parameters

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

Returns

bool — true or false

(&Pool) get_service

XuAPI
pub fn (p &Pool) get_service(name string) string

Purpose

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

Parameters

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

Returns

string — result string

(&Pool) ping

XuAPI
pub fn (p &Pool) ping() bool

Purpose

Send a ping heartbeat request to verify connection liveness.

Parameters

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

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.