Prevent the mqtt establish the second connection (#4594)#4814
Closed
trankennykhang wants to merge 2 commits intoinfluxdata:masterfrom
g3i:bugfix/4594
Closed
Prevent the mqtt establish the second connection (#4594)#4814trankennykhang wants to merge 2 commits intoinfluxdata:masterfrom g3i:bugfix/4594
trankennykhang wants to merge 2 commits intoinfluxdata:masterfrom
g3i:bugfix/4594
Conversation
Contributor
|
@trankennykhang Thanks this looks right but I'm still suspect about our handling of the connected boolean in diff --git a/plugins/inputs/mqtt_consumer/mqtt_consumer.go b/plugins/inputs/mqtt_consumer/mqtt_consumer.go
index 5853ad93..85b7074a 100644
--- a/plugins/inputs/mqtt_consumer/mqtt_consumer.go
+++ b/plugins/inputs/mqtt_consumer/mqtt_consumer.go
@@ -150,27 +150,25 @@ func (m *MQTTConsumer) connect() error {
return err
}
- go m.receiver()
-
- return nil
-}
-
-func (m *MQTTConsumer) onConnect(c mqtt.Client) {
log.Printf("I! MQTT Client Connected")
- if !m.PersistentSession || !m.connected {
+ m.connected = true
+
+ if !m.PersistentSession {
topics := make(map[string]byte)
for _, topic := range m.Topics {
topics[topic] = byte(m.QoS)
}
- subscribeToken := c.SubscribeMultiple(topics, m.recvMessage)
+ subscribeToken := m.client.SubscribeMultiple(topics, m.recvMessage)
subscribeToken.Wait()
if subscribeToken.Error() != nil {
m.acc.AddError(fmt.Errorf("E! MQTT Subscribe Error\ntopics: %s\nerror: %s",
strings.Join(m.Topics[:], ","), subscribeToken.Error()))
}
- m.connected = true
}
- return
+
+ go m.receiver()
+
+ return nil
}
func (m *MQTTConsumer) onConnectionLost(c mqtt.Client, err error) {
@@ -271,10 +269,9 @@ func (m *MQTTConsumer) createOpts() (*mqtt.ClientOptions, error) {
opts.AddBroker(server)
}
- opts.SetAutoReconnect(true)
+ opts.SetAutoReconnect(false)
opts.SetKeepAlive(time.Second * 60)
opts.SetCleanSession(!m.PersistentSession)
- opts.SetOnConnectHandler(m.onConnect)
opts.SetConnectionLostHandler(m.onConnectionLost)
return opts, nil |
3 tasks
Contributor
|
@trankennykhang Based on your change here I reorganized how the mqtt_consumer plugin makes connections. Could you review #4846? |
Contributor
|
This change was included into #4846 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Required for all PRs: