fix: 完成 broker 复审 B-03 至 B-12

每连接异步下发与背压、写出后断开、校验当前连接与订阅、生命周期串行、登录条件更新、闲置按在线计、认证超时并发与 Shutdown 0x8B。
This commit is contained in:
Nixevol
2026-09-30 16:21:05 +08:00
parent 0b9ce0359a
commit 91e887ba46
12 changed files with 1099 additions and 89 deletions
+31 -1
View File
@@ -30,6 +30,7 @@ func (h *nixHook) Provides(b byte) bool {
mqtt.OnQosComplete,
mqtt.OnQosDropped,
mqtt.OnSubscribed,
mqtt.OnPacketSent,
}, []byte{b})
}
@@ -79,7 +80,9 @@ func (h *nixHook) OnConnect(cl *mqtt.Client, pk packets.Packet) error {
"endpoint", endpointID, "receive_maximum", rm)
}
res, err := h.b.auth.Authenticate(context.Background(), endpointID, pk.Connect.Password, remoteIP)
authCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
res, err := h.b.auth.Authenticate(authCtx, endpointID, pk.Connect.Password, remoteIP)
if err != nil {
return err // mochi 不回 CONNACK,直接断开;不登记连接表
}
@@ -171,6 +174,18 @@ func (h *nixHook) OnSubscribed(cl *mqtt.Client, pk packets.Packet, reasonCodes [
}
}
func (h *nixHook) OnPacketSent(cl *mqtt.Client, pk packets.Packet, _ []byte) {
if pk.FixedHeader.Type != packets.Publish {
return
}
h.b.connsMu.RLock()
st := h.b.byClient[cl]
h.b.connsMu.RUnlock()
if st != nil {
st.sentPub.Add(1)
}
}
func (h *nixHook) OnPublishDropped(cl *mqtt.Client, pk packets.Packet) {
h.b.log.Debug("publish dropped", "client", cl.ID, "topic", pk.TopicName, "size", len(pk.Payload))
h.b.connsMu.RLock()
@@ -188,7 +203,9 @@ func (h *nixHook) OnPublishDropped(cl *mqtt.Client, pk packets.Packet) {
func (h *nixHook) OnSessionEstablished(cl *mqtt.Client, _ packets.Packet) {
h.b.connsMu.Lock()
st := h.b.byClient[cl]
var old *connState
if st != nil {
old = h.b.current[st.endpointID]
h.b.current[st.endpointID] = st
st.established = true
}
@@ -196,6 +213,15 @@ func (h *nixHook) OnSessionEstablished(cl *mqtt.Client, _ packets.Packet) {
if st == nil {
return
}
lk := h.b.endpointLife(st.endpointID)
lk.Lock()
if old != nil && old != st {
old.mu.Lock()
old.superseded = true
old.mu.Unlock()
}
lk.Unlock()
st.startDownLoop(h.b)
info := port.ConnInfo{
ConnID: st.connID,
EndpointID: st.endpointID,
@@ -224,6 +250,10 @@ func (h *nixHook) OnDisconnect(cl *mqtt.Client, err error, _ bool) {
if st == nil {
return
}
lk := h.b.endpointLife(st.endpointID)
lk.Lock()
st.stopDownLoop()
lk.Unlock()
h.b.releaseAllLarge(st)
h.b.cancelHandshakeDeadline(st.endpointID, st.connID)