XU XUv0.16.0

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

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

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

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

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

示例代码 · API 声明

示例代码

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

33_standalone_kafka/main.xu

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

Xu本地 SDK 示例
@main

kafka #github.com/xulang-org/packages/kafka
#errors

fn verify_kafka_ping() {
    println("--- 1. Testing Kafka Heartbeat & ApiVersions Ping ---")
    $client := kafka.open("127.0.0.1:9092") >>> (err) {
        println("   [FAIL] Connect error: " + err.msg())
        <<
    }
    is_ok := client.ping()
    if is_ok {
        println("   [PASS] Kafka ApiVersions heartbeat succeeded.")
    } else {
        println("   [FAIL] Kafka ping failed.")
    }
    client.close()
}

fn verify_kafka_produce_fetch() {
    println("\n--- 2. Testing Kafka Produce & Fetch Operations ---")
    $client := kafka.open("127.0.0.1:9092") >>> (err) {
        println("   [FAIL] Connect error: " + err.msg())
        <<
    }

    topic := "xu_test_topic"
    partition := 0

    // Ensure metadata / auto create topic
    client.metadata(topic + "")

    // 1. Produce message
    msg := "Antigravity event message from Xu language"
    println("   Producing message to topic '" + topic + "': " + msg)
    offset := client.produce_key_value(topic + "", partition, "event-key-1", msg + "") >>> (err) {
        println("   [FAIL] Produce failed: " + err.msg())
        client.close()
        <<
    }
    println("   [PASS] Message produced successfully at offset: " + str(offset))

    // 2. Fetch message back
    messages := client.fetch(topic + "", partition, offset) >>> (err) {
        println("   [FAIL] Fetch failed: " + err.msg())
        client.close()
        <<
    }
    println("   Fetched messages count: " + str(messages.len))
    $found := false
    $i := 0
    while i < messages.len {
        println("   Message[" + str(i) + "]: " + messages[i])
        if messages[i] == msg {
            found = true
        }
        i = i + 1
    }

    if found {
        println("   [PASS] Produced message was verified in fetch output!")
    } else {
        println("   [WARN] Message not matched in first fetch chunk (may require offset adjustment).")
    }

    client.close()
}

fn verify_kafka_pool() {
    println("\n--- 3. Testing Kafka Connection Pool ---")
    $p := kafka.open_pool("127.0.0.1:9092", "xu-pool-tester", 5)

    // 1. Connection Reuse Test
    $c1 := p.get() >>> (err) {
        println("   [FAIL] Failed to get c1: " + err.msg())
        <<
    }
    fd1 := c1.fd
    println("   Borrow 1 fd: " + str(fd1))
    p.put(c1)

    $c2 := p.get() >>> (err) {
        println("   [FAIL] Failed to get c2: " + err.msg())
        <<
    }
    fd2 := c2.fd
    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
    $c3 := p.get() >>> (err) {
        println("   [FAIL] Failed to get c3: " + err.msg())
        <<
    }
    fd3 := c3.fd
    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(c2)
    p.put(c3)

    // 3. Delegated Produce & Fetch via Pool
    println("   Testing delegated operations via pool...")
    pool_topic := "xu_pool_topic"
    pool_msg := "pool message payload test"

    offset := p.produce_key_value(pool_topic + "", 0, "pk1", pool_msg + "") >>> (err) {
        println("   [FAIL] Pool produce failed: " + err.msg())
        <<
    }
    println("   [PASS] Message produced via pool at offset: " + str(offset))

    msgs := p.fetch(pool_topic + "", 0, offset) >>> (err) {
        println("   [FAIL] Pool fetch failed: " + err.msg())
        <<
    }
    println("   Fetched messages via pool count: " + str(msgs.len))
    $found := false
    $i := 0
    while i < msgs.len {
        if msgs[i] == pool_msg {
            found = true
        }
        i = i + 1
    }
    if found {
        println("   [PASS] Pool produce & fetch roundtrip verified successfully!")
    } else {
        println("   [WARN] Pool message not in immediate chunk.")
    }

    p.close()
    println("   [PASS] Kafka Pool closed cleanly.")
}

fn main() {
    println("=== Xu Apache Kafka Distributed Event Streaming Driver Verification ===\n")
    verify_kafka_ping()
    verify_kafka_produce_fetch()
    verify_kafka_pool()
    println("\n[KAFKA VERIFICATION 100% COMPLETED]")
}

API 声明

open

XuAPI
pub fn open(broker string) Client

用途

打开资源或建立网络连接。失败时抛出错误,调用方需处理或继续传播。

参数

broker string
broker(string)

返回值

Client — 该类型的值

查看用法示例

open_client

XuAPI
pub fn open_client(broker string, client_id string) Client

用途

打开客户端。失败时抛出错误,调用方需处理或继续传播。

参数

broker string
broker(string)
client_id string
client_id(string)

返回值

Client — 该类型的值

open_pool_client

XuAPI
pub fn open_pool_client(broker string, client_id string, max_idle int) Pool

用途

执行 open pool client 操作。

参数

broker string
broker(string)
client_id string
client_id(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

struct Client

XuAPI
pub struct Client {
    pub fd int
    pub broker string
    pub client_id string
    pub next_correlation_id int
}

用途

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

公开字段

fd int
文件描述符
broker string
字段(string)
client_id string
字段(string)
next_correlation_id int
字段(int)

($&Client) write_exact

XuAPI
pub fn (c $&Client) write_exact(data string) int

用途

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

参数

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

返回值

int — 该类型的值

($&Client) read_exact

XuAPI
pub fn (c $&Client) read_exact(n int) string

用途

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

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
n int
数量、长度或上限(int)

返回值

string — 结果字符串

($&Client) round_trip

XuAPI
pub fn (c $&Client) round_trip(req_bytes string) string

用途

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

参数

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

返回值

string — 结果字符串

connect

XuAPI
pub fn connect(broker string, client_id string) Client

用途

执行 connect 处理。失败时抛出错误,调用方需处理或继续传播。

参数

broker string
broker(string)
client_id string
client_id(string)

返回值

Client — 该类型的值

($&Client) ping

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

用途

发送心跳保活探测请求,检测连接活性与服务可用性。

参数

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

返回值

bool — 是否成立

($&Client) metadata

XuAPI
pub fn (c $&Client) metadata(topic string) bool

用途

在 $&Client 上执行 metadata 操作。

参数

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

返回值

bool — 是否成立

($&Client) produce

XuAPI
pub fn (c $&Client) produce(topic string, partition int, message string) int

用途

在 $&Client 上执行 produce 操作。

参数

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

返回值

int — 该类型的值

($&Client) produce_key_value

XuAPI
pub fn (c $&Client) produce_key_value(topic string, partition int, key string, val string) int

用途

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

参数

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

返回值

int — 该类型的值

($&Client) fetch

XuAPI
pub fn (c $&Client) fetch(topic string, partition int, offset int) []string

用途

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

参数

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

返回值

[]string — 切片结果

($&Client) close

XuAPI
pub fn (c $&Client) close()

用途

$&Client 上的操作:关闭资源。

参数

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

返回值

不返回值。

struct Pool

XuAPI
pub struct Pool {
    pub broker string
    pub client_id string
    pub idle chan int
    pub max_idle int
}

用途

Apache Kafka 连接池管理器,维护复用套接字连接。

公开字段

broker string
字段(string)
client_id string
字段(string)
idle chan int
字段(通道;类型 chan int)
max_idle int
字段(int)

new_pool

XuAPI
pub fn new_pool(broker string, client_id string, max_idle int) Pool

用途

构造 pool。

参数

broker string
broker(string)
client_id string
client_id(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

open_pool

XuAPI
pub fn open_pool(broker string, client_id string, max_idle int) Pool

用途

执行 open pool 操作。

参数

broker string
broker(string)
client_id string
client_id(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

查看用法示例

(&Pool) get

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

用途

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

参数

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

返回值

Client — 该类型的值

(&Pool) ping

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

用途

发送心跳保活探测请求,检测连接活性与服务可用性。

参数

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

返回值

bool — 是否成立

(&Pool) produce

XuAPI
pub fn (p &Pool) produce(topic string, partition int, message string) int

用途

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

参数

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

返回值

int — 该类型的值

(&Pool) produce_key_value

XuAPI
pub fn (p &Pool) produce_key_value(topic string, partition int, key string, val string) int

用途

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

参数

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

返回值

int — 该类型的值

(&Pool) fetch

XuAPI
pub fn (p &Pool) fetch(topic string, partition int, offset int) []string

用途

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

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
topic string
topic(string)
partition int
partition(int)
offset int
偏移(int)

返回值

[]string — 切片结果

(&Pool) put_conn

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

用途

&Pool 上的操作:执行 put conn 操作。

参数

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

返回值

不返回值。

(&Pool) put

XuAPI
pub fn (p &Pool) put(c Client)

用途

写入或更新指定键对应的值数据。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
c Client
字节或字符码(Client)

返回值

不返回值。

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

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

参数

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

返回值

不返回值。

encode_i8

XuAPI
pub fn encode_i8(v int) string

用途

编码 i8 数据。

参数

v int
值(int)

返回值

string — 结果字符串

encode_i16

XuAPI
pub fn encode_i16(v int) string

用途

编码 i16 数据。

参数

v int
值(int)

返回值

string — 结果字符串

encode_i32

XuAPI
pub fn encode_i32(v int) string

用途

编码 i32 数据。

参数

v int
值(int)

返回值

string — 结果字符串

encode_i64

XuAPI
pub fn encode_i64(v int) string

用途

编码 i64 数据。

参数

v int
值(int)

返回值

string — 结果字符串

encode_string

XuAPI
pub fn encode_string(s string) string

用途

编码 string 数据。

参数

s string
源字符串(string)

返回值

string — 结果字符串

encode_null_string

XuAPI
pub fn encode_null_string() string

用途

编码 null string 数据。

参数

无需传入参数。

返回值

string — 结果字符串

encode_bytes

XuAPI
pub fn encode_bytes(b string) string

用途

编码 bytes 数据。

参数

b string
b(string)

返回值

string — 结果字符串

encode_null_bytes

XuAPI
pub fn encode_null_bytes() string

用途

编码 null bytes 数据。

参数

无需传入参数。

返回值

string — 结果字符串

encode_zigzag_i32

XuAPI
pub fn encode_zigzag_i32(n int) string

用途

编码 zigzag i32 数据。

参数

n int
数量、长度或上限(int)

返回值

string — 结果字符串

encode_zigzag_i64

XuAPI
pub fn encode_zigzag_i64(n int) string

用途

编码 zigzag i64 数据。

参数

n int
数量、长度或上限(int)

返回值

string — 结果字符串

struct VarintResult

XuAPI
pub struct VarintResult {
    pub value int
    pub bytes_read int
}

用途

承载 value、bytes_read 等属性的结构体。

公开字段

value int
字段(int)
bytes_read int
字段(int)

decode_varint

XuAPI
pub fn decode_varint(s &string, off int) VarintResult

用途

解码 varint 数据。

参数

s &string
源字符串(只读借用,调用方仍持有原值;类型 &string)
off int
偏移(int)

返回值

VarintResult — 该类型的值

decode_zigzag

XuAPI
pub fn decode_zigzag(s &string, off int) VarintResult

用途

解码 zigzag 数据。

参数

s &string
源字符串(只读借用,调用方仍持有原值;类型 &string)
off int
偏移(int)

返回值

VarintResult — 该类型的值

decode_i16

XuAPI
pub fn decode_i16(s &string, off int) int

用途

解码 i16 数据。

参数

s &string
源字符串(只读借用,调用方仍持有原值;类型 &string)
off int
偏移(int)

返回值

int — 该类型的值

decode_i32

XuAPI
pub fn decode_i32(s &string, off int) int

用途

解码 i32 数据。

参数

s &string
源字符串(只读借用,调用方仍持有原值;类型 &string)
off int
偏移(int)

返回值

int — 该类型的值

decode_i64

XuAPI
pub fn decode_i64(s &string, off int) int

用途

解码 i64 数据。

参数

s &string
源字符串(只读借用,调用方仍持有原值;类型 &string)
off int
偏移(int)

返回值

int — 该类型的值

build_request

XuAPI
pub fn build_request(api_key int, api_version int, correlation_id int, client_id string, body string) string

用途

执行 build request 操作。

参数

api_key int
api_key(int)
api_version int
api_version(int)
correlation_id int
correlation_id(int)
client_id string
client_id(string)
body string
正文(string)

返回值

string — 结果字符串

build_record_batch

XuAPI
pub fn build_record_batch(key string, val string) string

用途

执行 build record batch 操作。

参数

key string
键(string)
val string
值(string)

返回值

string — 结果字符串

build_produce_request

XuAPI
pub fn build_produce_request(correlation_id int, client_id string, topic string, partition int, key string, val string) string

用途

执行 build produce request 操作。

参数

correlation_id int
correlation_id(int)
client_id string
client_id(string)
topic string
topic(string)
partition int
partition(int)
key string
键(string)
val string
值(string)

返回值

string — 结果字符串

build_fetch_request

XuAPI
pub fn build_fetch_request(correlation_id int, client_id string, topic string, partition int, offset int) string

用途

执行 build fetch request 操作。

参数

correlation_id int
correlation_id(int)
client_id string
client_id(string)
topic string
topic(string)
partition int
partition(int)
offset int
偏移(int)

返回值

string — 结果字符串

build_metadata_request

XuAPI
pub fn build_metadata_request(correlation_id int, client_id string, topic string) string

用途

执行 build metadata request 操作。

参数

correlation_id int
correlation_id(int)
client_id string
client_id(string)
topic string
topic(string)

返回值

string — 结果字符串

build_apiversions_request

XuAPI
pub fn build_apiversions_request(correlation_id int, client_id string) string

用途

执行 build apiversions request 操作。

参数

correlation_id int
correlation_id(int)
client_id string
client_id(string)

返回值

string — 结果字符串

parse_produce_response

XuAPI
pub fn parse_produce_response(resp string) int

用途

解析为 produce response。失败时抛出错误,调用方需处理或继续传播。

参数

resp string
resp(string)

返回值

int — 该类型的值

parse_fetch_response

XuAPI
pub fn parse_fetch_response(resp string) []string

用途

解析为 fetch response。失败时抛出错误,调用方需处理或继续传播。

参数

resp string
resp(string)

返回值

[]string — 切片结果

struct TlsClient

XuAPI
pub struct TlsClient {
    pub conn transport.Conn
    pub host string
    pub port int
}

用途

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

公开字段

conn transport.Conn
字段(transport.Conn)
host string
主机
port int
端口

tls_connect

XuAPI
pub fn tls_connect(endpoint string, default_port int) TlsClient

用途

执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。

参数

endpoint string
endpoint(string)
default_port int
default_port(int)

返回值

TlsClient — 该类型的值

tls_connect_config

XuAPI
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient

用途

执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。

参数

endpoint string
endpoint(string)
default_port int
default_port(int)
cfg tls.Config
cfg(tls.Config)

返回值

TlsClient — 该类型的值

($&TlsClient) write_all

XuAPI
pub fn (c $&TlsClient) write_all(data string) int

用途

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

参数

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

返回值

int — 该类型的值

($&TlsClient) read

XuAPI
pub fn (c $&TlsClient) read(max int) string

用途

$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

string — 结果字符串

($&TlsClient) read_exact

XuAPI
pub fn (c $&TlsClient) read_exact(need int) string

用途

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

参数

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

返回值

string — 结果字符串

($&TlsClient) close

XuAPI
pub fn (c $&TlsClient) close() int

用途

$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

int — 该类型的值

($&TlsClient) set_deadline

XuAPI
pub fn (c $&TlsClient) set_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

int — 该类型的值

($&TlsClient) set_read_deadline

XuAPI
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

int — 该类型的值

($&TlsClient) set_write_deadline

XuAPI
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

int — 该类型的值