Dependencies
Status: early development.
moon add daqing/moonkafka///|
async fn main {
// Produce
let producer = @moonkafka.Producer::connect(
host="127.0.0.1",
port=9092,
topic="events",
)
defer producer.close()
let offset = producer.send(
key=@utf8.encode("k1"),
value=@utf8.encode("hello"),
)
println("produced at offset \{offset}")
// Consume
let consumer = @moonkafka.Consumer::connect(
host="127.0.0.1",
port=9092,
topic="events",
start_from=@moonkafka.StartFrom::Earliest,
)
defer consumer.close()
for ;; {
for record in consumer.poll() {
println("offset=\{record.offset} value=\{record.value}")
}
}
}moon run cmd/main -- consume events [host] [port]moon run cmd/main -- produce events "hello" [key] [host] [port]moon build # build the library
moon test # run tests (blackbox + whitebox)
moon fmt # format code
moon info # regenerate package interfaces (.mbti)pub(all) suberror BrokerError {
BrokerError(Int, String)
}pub(all) suberror SaslError {
SaslError(String)
}pub(all) suberror TransportError {
ConnectionClosed(String)
RequestTimeout(String)
}async fn BrokerConnection::authenticate(self : BrokerConnection, sasl : SaslConfig, timeout_ms? : Int) -> Unitasync fn BrokerConnection::connect(host : String, port : Int, client_id? : String, max_in_flight? : Int, sasl? : SaslConfig?, timeout_ms? : Int, tls? : TlsClientOptions?) -> BrokerConnectionasync fn BrokerConnection::fetch(self : BrokerConnection, topic : String, partitions : Array[(PartitionInfo, Int64)], max_wait_ms~ : Int, max_bytes~ : Int, timeout_ms? : Int) -> Array[FetchPartitionResult]async fn BrokerConnection::fetch_metadata(self : BrokerConnection, topic : String, timeout_ms? : Int) -> Metadataasync fn BrokerConnection::list_offsets(self : BrokerConnection, topic : String, partitions : Array[PartitionInfo], timestamp : Int64, timeout_ms? : Int) -> Map[Int, Int64]async fn BrokerConnection::produce(self : BrokerConnection, topic : String, partitions : Array[(Int, Bytes)], acks~ : Int, timeout_ms~ : Int) -> Array[ProducePartitionResult]async fn BrokerConnection::request(self : BrokerConnection, api_key : Int, api_version : Int, body : Bytes, timeout_ms? : Int) -> Decoderasync fn BrokerConnection::request_raw(self : BrokerConnection, api_key : Int, api_version : Int, body : Bytes, timeout_ms? : Int, flexible? : Bool) -> Decoderpub struct CommonConfig {
bootstrap_servers : Array[String]
client_id : String
request_timeout_ms : Int
connection_max_idle_ms : Int
retries : Int
retry_backoff_ms : Int
retry_backoff_max_ms : Int
security_protocol : SecurityProtocol
sasl : SaslConfig?
tls : TlsClientOptions?
} derive(Debug)fn CommonConfig::new(bootstrap_servers : Array[String], request_timeout_ms? : Int, security_protocol? : SecurityProtocol, sasl? : SaslConfig?, tls? : TlsClientOptions?) -> CommonConfig raisepub struct Consumer {
topic : String
start_from : StartFrom
request_timeout_ms : Int
bootstrap : BootstrapServers
sasl : SaslConfig?
meta_conn : BrokerConnection
brokers : Map[Int, BrokerInfo]
leader_conns : Map[Int, BrokerConnection]
partitions : Array[PartitionState]
closed : Bool
}pub struct ConsumerConfig {
common : CommonConfig
topic : String
start_from : StartFrom
} derive(Debug)fn ConsumerConfig::new(bootstrap_servers : Array[String], topic : String, start_from? : StartFrom, request_timeout_ms? : Int, security_protocol? : SecurityProtocol, sasl? : SaslConfig?, tls? : TlsClientOptions?) -> ConsumerConfig raisepub struct ProducePartitionResult {
partition : Int
error_code : Int
base_offset : Int64
}pub struct Producer {
topic : String
acks : Int
timeout_ms : Int
retries : Int
retry_backoff_ms : Int
retry_backoff_max_ms : Int
bootstrap : BootstrapServers
sasl : SaslConfig?
use_tls : Bool
tls_options : TlsClientOptions?
meta_conn : BrokerConnection
brokers : Map[Int, BrokerInfo]
leader_conns : Map[Int, BrokerConnection]
partitions : Array[PartitionInfo]
round_robin : Int
closed : Bool
}fn ProducerConfig::new(bootstrap_servers : Array[String], topic : String, acks? : Int, request_timeout_ms? : Int, security_protocol? : SecurityProtocol, sasl? : SaslConfig?, tls? : TlsClientOptions?) -> ProducerConfig raisepub(all) struct SaslAuthenticateResult {
error_code : Int
error_message : String?
auth_bytes : Bytes?
session_lifetime_ms : Int64
}pub(all) struct SaslConfig {
mechanism : SaslMechanism
username : String
password : String
} derive(Debug)pub(all) struct TlsClientOptions {
server_name : String
verify_certificates : Bool
ca_pem_file : String?
} derive(Debug)fn TlsClientOptions::new(server_name : String, verify_certificates? : Bool, ca_pem_file? : String?) -> TlsClientOptionsfn backoff_ms(base_ms : Int, max_ms : Int, attempt : Int) -> Intasync fn connect_bootstrap(servers : BootstrapServers, client_id : String, timeout_ms? : Int, sasl? : SaslConfig?, use_tls? : Bool, tls? : TlsClientOptions?) -> BrokerConnectionfn encode_fetch_request(topic : String, partitions : Array[(PartitionInfo, Int64)], max_wait_ms : Int, max_bytes : Int) -> Bytesfn encode_list_offsets_request(topic : String, partitions : Array[PartitionInfo], timestamp : Int64) -> Bytesfn encode_produce_request(topic : String, partitions : Array[(Int, Bytes)], acks : Int, timeout_ms : Int) -> Bytesfn encode_request(api_key : Int, api_version : Int, correlation_id : Int, client_id : String, body : Bytes, flexible? : Bool) -> Bytesfn encode_sasl_authenticate_request(auth_bytes : Bytes) -> Bytesfn encode_sasl_handshake_request(mechanism : String) -> Bytesfn error_name(code : Int) -> Stringfn error_retriable(code : Int) -> Boolfn scram_auth_message(client_first_bare : String, server_first : String, client_final_without_proof : String) -> Stringfn scram_client_proof_b64(sha512 : Bool, salted_password : Bytes, auth_message : String) -> Stringfn scram_escape_username(username : String) -> Stringfn[H : CryptoHasher] scram_pbkdf2_hmac(h : H, password : Bytes, salt : Bytes, iterations : Int, dk_len : Int) -> Bytesfn scram_salted_password(sha512 : Bool, password : Bytes, salt : Bytes, iterations : Int) -> Bytesfn scram_server_final(sha512 : Bool, salted_password : Bytes, auth_message : String) -> Stringfn scram_server_signature_b64(sha512 : Bool, salted_password : Bytes, auth_message : String) -> Stringfn scram_verify_client_proof(sha512 : Bool, salted_password : Bytes, auth_message : String, proof_b64 : String) -> BoolInstall
Download zipDependencies