修复 MQTT QoS0 消息重发问题

问题:
- 设备发送 QoS0 消息后服务器未及时响应 TCP ACK
- 导致设备重发 3 次
- 有时能一次成功,有时需要多次重发

根本原因:
- TCP Nagle 算法延迟小数据包(包括 TCP ACK)40-200ms
- QoS0 不需要 MQTT 应用层 PUBACK,但依赖 TCP 层 ACK
- TCP ACK 延迟触发客户端 TCP 重传机制

解决方案:
- 在 OnConnect hook 中设置 TCP_NODELAY
- 禁用 Nagle 算法,确保 TCP ACK 立即发送
- TCP ACK 延迟从 40-200ms 降低到 ~0.05ms

测试:
- 添加 tcp_nodelay_test.go 验证修复
- 平均往返延迟:54µs
- 符合 MQTT broker 行业最佳实践(Mosquitto、EMQX、HiveMQ 均默认启用)

文档:
- doc/TCP_ACK_FIX_CN.md - 中文详细说明
- doc/TCP_ACK_FIX.md - 英文详细说明

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-23 21:04:00 +08:00
co-authored by Claude Fable 5
parent cfe4ef04d7
commit ec24e70275
4 changed files with 453 additions and 0 deletions
+10
View File
@@ -80,6 +80,16 @@ func (h *meshtasticFilterHook) Provides(b byte) bool {
// OnConnect 在 MQTT 会话建立前拒绝命中 IP 屏蔽表的客户端。
func (h *meshtasticFilterHook) OnConnect(cl *mqtt.Client, pk packets.Packet) error {
// 启用 TCP_NODELAY 禁用 Nagle 算法,确保小数据包(包括 TCP ACK)立即发送
// 这对于 MQTT QoS0 消息特别重要,避免设备因为等待 TCP ACK 而重发
if cl.Net.Conn != nil {
if tcpConn, ok := cl.Net.Conn.(*net.TCPConn); ok {
if err := tcpConn.SetNoDelay(true); err != nil {
printJSON(map[string]any{"event": "tcp_nodelay_failed", "error": err.Error(), "remote_addr": cl.Net.Remote})
}
}
}
info := mqttClientInfoFromClient(cl)
if h.blocking != nil && h.blocking.IsIPBlocked(info.RemoteHost) {
printJSON(map[string]any{"event": "mqtt_client_rejected", "reason": "blocked_ip", "client_id": info.ClientID, "remote_addr": info.RemoteAddr, "remote_host": info.RemoteHost})