Categorygithub.com/golang-io/mqtt
modulepackage
0.0.0-20241231160413-82b333a904a5
Repository: https://github.com/golang-io/mqtt.git
Documentation: pkg.go.dev

# README

mqtt

巨人的肩膀

implement

https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html

mqtt.Client -> http.Transport -> http.Conn -> net.Conn

http.Server -> http.ResponseWriter -> http.Conn -> net.Conn

https://mcxiaoke.gitbooks.io/mqtt-cn/content/mqtt/06-WebSocket.html

p2p模式:https://help.aliyun.com/zh/apsaramq-for-mqtt/cloud-message-queue-mqtt-upgraded/developer-reference/features-p2p-messaging-model?spm=a2c4g.11186623.0.0.44ff2ab0JCP9Sa#concept-96176-zh

注入数据包(Packet Injection)

如果你想要更多的服务端控制,或者想要设置特定的MQTT v5属性或其他属性,你可以选择指定的客户端创建自己的发布包(publish packets)。这种方法允许你将MQTT数据包(packets)直接注入到运行中的服务端,相当于服务端直接自己模拟接收到了某个客户端的数据包。

数据包注入(Packet Injection)可用于任何MQTT数据包,包括ping请求、订阅等。你可以获取客户端的详细信息,因此你甚至可以直接在服务端模拟某个在线的客户端,发布一个数据包。

大多数情况下,您可能希望使用上面描述的内联客户端(Inline Client),因为它具有独特的特权:它可以绕过所有ACL和主题验证检查,这意味着它甚至可以发布到$SYS主题。你也可以自己从头开始制定一个自己的内联客户端,它将与内置的内联客户端行为相同。

cl := server.NewClient(nil, "local", "inline", true)
server.InjectPacket(cl, packets.Packet{
  FixedHeader: packets.FixedHeader{
    Type: packets.Publish,
  },
  TopicName: "direct/publish",
  Payload: []byte("scheduled message"),
})

MQTT数据包仍然需要满足规范的结构,所以请参考测试用例中数据包的定义MQTTv5规范 以获取一些帮助。

具体如何使用请参考 hooks example

测试(Testing)

单元测试(Unit Tests)

Paho 互操作性测试(Paho Interoperability Test)

您可以使用 examples/paho/main.go 启动服务器,然后在 interoperability 文件夹中运行 python3 client_test5.py 来检查代理是否符合 Paho互操作性测试 的要求,包括 MQTT v5 和 v3 的测试。

请注意,关于 paho 测试套件存在一些尚未解决的问题,因此在 paho/main.go 示例中启用了某些兼容性模式。

基准测试(Performance Benchmarks)

Mochi MQTT 的性能与其他的一些主流的mqtt中间件(如 Mosquitto、EMQX 等)不相上下。

基准测试是使用 MQTT-Stresser 在 Apple Macbook Air M2 上进行的,使用 cmd/main.go 默认设置。考虑到高低吞吐量的突发情况,中位数分数是最有用的。数值越高越好。

基准测试中呈现的数值不代表真实每秒消息吞吐量。它们依赖于 mqtt-stresser 的一种不寻常的计算方法,但它们在所有代理之间是一致的。性能基准测试的结果仅供参考。这些比较都是使用默认配置进行的。

mqtt-stresser -broker tcp://localhost:1883 -num-clients=2 -num-messages=10000

Brokerpublish fastestmedianslowestreceive fastestmedianslowest
Mochi v2.2.10124,772125,456124,614314,461313,186311,910
Mosquitto v2.0.15155,920155,919155,918185,485185,097184,709
EMQX v5.0.11156,945156,257155,56817,91817,78317,649
Rumqtt v0.21.0112,208108,480104,753135,784126,446117,108

mqtt-stresser -broker tcp://localhost:1883 -num-clients=10 -num-messages=10000

Brokerpublish fastestmedianslowestreceive fastestmedianslowest
Mochi v2.2.1041,82531,66323,008144,05865,90337,618
Mosquitto v2.0.1542,72938,63329,87923,24119,71418,806
EMQX v5.0.1121,55317,41814,3564,2573,9803,756
Rumqtt v0.21.042,21323,15320,81449,46536,62619,283
mqtt-server[v0.0.1]24,37416,04010,85671,73132,08115,785
mqtt-server[v0.0.2] - buffer38,4352340730615403512784822182
mqtt-server-buffer/sync.Map14,1912,9121,18089,5285,6051,468

百万消息挑战(立即向服务器发送100万条消息):

mqtt-stresser -broker tcp://localhost:1883 -num-clients=100 -num-messages=10000

Brokerpublish fastestmedianslowestreceive fastestmedianslowest
Mochi v2.2.1013,5324,4252,34452,1207,2742,701
Mosquitto v2.0.153,8263,3953,0321,2001,1501,118
EMQX v5.0.114,0862,4322,274434333311
Rumqtt v0.21.078,9725,0473,8044,2863,2492,027
mqtt-server75,9746,0591,68524,7112,7241,819
mqtt-server-buffer/sync.Map14,1912,9121,18089,5285,6051,468
mqtt-server-linux-server2172433792306260821071454

这里还不确定EMQX是不是哪里出了问题,可能是因为 Docker 的默认配置优化不对,所以要持保留意见,因为我们确实知道它是一款可靠的软件。

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

Control packet types.
Control packet types.
Control packet types.
Control packet types.
No description provided by the author
No description provided by the author
Control packet types.
Control packet types.
Control packet types.
Control packet types.
Control packet types.
Control packet types.
Control packet types.
Control packet types.
StateActive represents a connection that has read 1 or more bytes of a request.
StateClosed represents a closed connection.
StateHijacked represents a hijacked connection.
StateIdle represents a connection that has finished handling a request and is in the keep-alive state, waiting for a new request.
StateNew represents a new connection that is expected to send a request immediately.
Control packet types.
Control packet types.
Control packet types.
Control packet types.

# Variables

No description provided by the author
ErrAbortHandler is a sentinel panic value to abort a handler.
ErrServerClosed is returned by the [Server.Serve], [ServeTLS], [ListenAndServe], and [ListenAndServeTLS] methods after a call to [Server.Shutdown] or [Server.Close].
No description provided by the author

# Structs

A Client is an MQTT client.
Endpoint 不提供事务能力,事务需要其他的上层协议来处理.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
A Server defines parameters for running an HTTP server.
No description provided by the author
TopicSubscribed 用来存储当前topic有哪些客户端订阅了.

# Interfaces

A Handler responds to an MQTT request.
No description provided by the author

# Type aliases

A ConnState represents the state of a client connection to a server.
No description provided by the author
No description provided by the author