A native MoonBit client library for the NSQ messaging platform.
| 范围 | 当前实现 |
|---|---|
| 协议 | NSQ V2 握手、帧编解码、消息解析、增量帧解析 |
| 发布 | PUB、延迟发布 DPUB、批量发布 MPUB、绑定 Topic 的 Producer |
| 消费 | SUB、RDY、FIN、REQ、TOUCH,以及 Consumer 启动、暂停、恢复和单条处理 |
| 连接 | 明文 TCP、基础 IDENTIFY、读取帧时回复心跳、CLS 关闭请求 |
| 发现 | nsqlookupd 查询、端点轮询、健康状态和冷却路由 |
| 辅助组件 | 批量缓冲、重试决策、计数器、延迟直方图、投递窗口、拓扑配置数据模型 |
| 管理操作 | 操作描述、参数检查及 URL 构造;没有管理 HTTP 请求执行 API |
git clone https://github.com/clhhhhhh/moon-nsq.git
cd moon-nsq
moon update
moon check --target wasm --deny-warn
moon build --target wasm --deny-warn
moon test --target wasm --deny-warnnsqd --tcp-address=127.0.0.1:4150 --http-address=127.0.0.1:4151moon run cmd/main --target wasm -- 127.0.0.1 orders.created "hello"moon run cmd/main --target wasmmoon info --target wasm
moon fmt
git diff --checkpub suberror ProtocolError {
InvalidFrameLength(Int)
UnknownFrameType(Int)
TruncatedFrame(Int)
InvalidCommand(String)
TruncatedMessage(Int)
ServerError(String)
UnexpectedFrame(String)
InvalidOption(String)
InvalidState(String)
} derive(Debug)fn AdminClient::new(host : String, port? : Int, base_path? : String) -> AdminClient raise ProtocolErrorfn BackoffOptions::new(initial_delay? : Int, max_delay? : Int, multiplier? : Int) -> BackoffOptions raise ProtocolErrorfn BrokerSet::replace(self : BrokerSet, producers : Array[ProducerEndpoint]) -> Unit raise ProtocolErrorpub struct ChannelSpec {
topic : String
name : String
max_in_flight : Int
max_attempts : Int
} derive(Debug)fn ChannelSpec::new(topic : String, name : String, max_in_flight? : Int, max_attempts? : Int) -> ChannelSpec raise ProtocolErrorpub struct Connection {
// private fields
}async fn Connection::publish_deferred(self : Connection, topic : String, delay_ms : Int, body : Bytes) -> Unitpub struct ConnectionHealth {
opened_at : Int64
last_activity : Int64
heartbeats_sent : Int
heartbeats_received : Int
requests : Int
request_failures : Int
bytes_read : Int64
bytes_written : Int64
} derive(Debug)fn ConnectionHealth::record_read(self : ConnectionHealth, now_ms : Int64, bytes : Int) -> Unit raise ProtocolErrorfn ConnectionHealth::record_write(self : ConnectionHealth, now_ms : Int64, bytes : Int) -> Unit raise ProtocolErrorpub struct ConnectionLease {
endpoint : ProducerEndpoint
generation : Int
released : Bool
// private fields
} derive(Debug)pub struct ConnectionOptions {
client_id : String
hostname : String
heartbeat_interval : Int
output_buffer_size : Int
output_buffer_timeout : Int
message_timeout : Int
sample_rate : Int
tls_v1 : Bool
feature_negotiation : Bool
} derive(Debug)fn ConnectionOptions::new(client_id? : String, hostname? : String, heartbeat_interval? : Int, output_buffer_size? : Int, output_buffer_timeout? : Int, message_timeout? : Int, sample_rate? : Int, tls_v1? : Bool, feature_negotiation? : Bool) -> ConnectionOptions raise ProtocolErrorpub struct ConnectionPool {
routing : RoutingTable
generation : Int
max_connections : Int
active : Int
stats : PoolStats
} derive(Debug)fn ConnectionPool::lease(self : ConnectionPool, now_ms : Int64) -> ConnectionLease raise ProtocolErrorfn ConnectionPool::mark_failure(self : ConnectionPool, lease : ConnectionLease, now_ms : Int64) -> Boolfn ConnectionPool::new(producers : Array[ProducerEndpoint], max_connections? : Int) -> ConnectionPool raise ProtocolErrorfn ConnectionPool::refresh(self : ConnectionPool, producers : Array[ProducerEndpoint]) -> Unit raise ProtocolErrorpub struct Consumer {
topic : String
channel : String
options : ConsumerOptions
started : Bool
received : Int
finished : Int
requeued : Int
discarded : Int
touched : Int
// private fields
}async fn Consumer::connect(host : String, port : Int, topic : String, channel : String, options? : ConsumerOptions, connection_options? : ConnectionOptions) -> Consumerpub struct ConsumerOptions {
max_in_flight : Int
requeue_delay : Int
max_attempts : Int
touch_interval : Int
} derive(Debug)fn ConsumerOptions::new(max_in_flight? : Int, requeue_delay? : Int, max_attempts? : Int, touch_interval? : Int) -> ConsumerOptions raise ProtocolErrorpub struct ConsumerStats {
received : Int
finished : Int
requeued : Int
discarded : Int
touched : Int
} derive(Debug)pub struct FrameStream {
buffered : Bytes
}fn LatencyHistogram::percentile(self : LatencyHistogram, percentile : Int) -> Int raise ProtocolErrorpub struct PoolStats {
leases : Int
releases : Int
failures : Int
successes : Int
rejected : Int
} derive(Debug)pub struct Producer {
topic : String
published : Int
deferred : Int
batches : Int
batch_messages : Int
// private fields
}async fn Producer::connect(host : String, port : Int, topic : String, options? : ConnectionOptions) -> Producerfn Producer::new_batch(self : Producer, max_messages? : Int, max_bytes? : Int) -> PublishBatch raise ProtocolErrorfn ProducerEndpoint::new(broadcast_address : String, tcp_port : Int, http_port? : Int, version? : String) -> ProducerEndpoint raise ProtocolErrorpub struct ProducerHealth {
endpoint : ProducerEndpoint
failures : Int
successes : Int
last_failure_ms : Int64
healthy : Bool
} derive(Debug)fn ProducerHealth::recover_if_ready(self : ProducerHealth, now_ms : Int64, cooldown_ms : Int64) -> Boolpub struct ProducerMetrics {
published : Int
failed : Int
bytes : Int64
latency : LatencyHistogram
} derive(Debug)fn ProducerMetrics::record_success(self : ProducerMetrics, payload_bytes : Int, latency_ms : Int) -> Unit raise ProtocolErrorpub struct ProducerStats {
published : Int
deferred : Int
batches : Int
batch_messages : Int
} derive(Debug)fn PublishBatch::new(topic : String, max_messages? : Int, max_bytes? : Int) -> PublishBatch raise ProtocolErrorpub struct RetryPolicy {
max_attempts : Int
initial_delay : Int
max_delay : Int
multiplier : Int
jitter_window : Int
} derive(Debug)fn RetryPolicy::decide(self : RetryPolicy, message : Message, reason : DeadLetterReason, seed : Int) -> RetryActionfn RetryPolicy::new(max_attempts? : Int, initial_delay? : Int, max_delay? : Int, multiplier? : Int, jitter_window? : Int) -> RetryPolicy raise ProtocolErrorpub struct RoutingTable {
entries : Array[ProducerHealth]
cursor : Int
cooldown_ms : Int64
} derive(Debug)fn RoutingTable::new(producers : Array[ProducerEndpoint], cooldown_ms? : Int64) -> RoutingTable raise ProtocolErrorfn RoutingTable::replace(self : RoutingTable, producers : Array[ProducerEndpoint]) -> Unit raise ProtocolErrorpub struct TopicSpec {
name : String
partitions : Int
retention_days : Int
max_message_bytes : Int
} derive(Debug)fn TopicSpec::new(name : String, partitions? : Int, retention_days? : Int, max_message_bytes? : Int) -> TopicSpec raise ProtocolErrorfn TopologyPlan::new(topic : TopicSpec, channels : Array[ChannelSpec]) -> TopologyPlan raise ProtocolErrorfn close_command() -> Bytesfn deferred_publish_command(topic : String, delay_ms : Int, body : Bytes) -> Bytes raise ProtocolErrorlet magic_v2 : Byteslet max_frame_size : Intfn nop_command() -> BytesInstall
Download zipA native MoonBit client library for the NSQ messaging platform.