MQTT: serialize resubscribe after reconnect (#30138)

This commit is contained in:
andig 2026-05-23 10:21:45 +02:00 • committed by GitHub
parent 88d0f9c401
commit 2839f873ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -135,12 +135,24 @@ func (m *Client) ConnectionHandler(client paho.Client) {
m.log.DEBUG.Printf("%s connected", m.broker)
m.mux.Lock()
defer m.mux.Unlock()
topics := make([]string, 0, len(m.listener))
for topic := range m.listener {
m.log.DEBUG.Printf("%s subscribe %s", m.broker, topic)
go m.listen(topic)
topics = append(topics, topic)
}
m.mux.Unlock()
// Resubscribe sequentially to avoid bursting the broker right after reconnect.
go func() {
for _, topic := range topics {
m.log.DEBUG.Printf("%s subscribe %s", m.broker, topic)
token := m.listen(topic)
if !token.WaitTimeout(request.Timeout) {
m.log.ERROR.Printf("subscribe %s: timeout", topic)
} else if err := token.Error(); err != nil {
m.log.ERROR.Printf("subscribe %s: %v", topic, err)
}
}
}()
}
// Cleanup recursively removes a topic