-1

I have a Paho MQTT GO client that listens to the broker. After around 2400-2500 messages, it stops listening to the new messages. Interestingly, it does not get disconnected with the broker. Seems that it is still active and listening but new messages does not appear anymore.

Below is my code -

package main

import (
    "flag"
    "fmt"
    "log"
    "os"
    "os/signal"
    "syscall"

    mqtt "github.com/eclipse/paho.mqtt.golang"
)

var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
    fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
}

var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
    fmt.Println("Connected")
}

var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
    fmt.Printf("Connect lost: %v", err)
}


func createClient() mqtt.Client {
    var broker = "*******"
    var port = 1883
    opts := mqtt.NewClientOptions()
    opts.AddBroker(fmt.Sprintf("tcp://%s:%d", broker, port))
    opts.SetClientID("go_mqtt_client_test1")
    opts.SetDefaultPublishHandler(messagePubHandler)
    opts.OnConnect = connectHandler
    opts.OnConnectionLost = connectLostHandler
    return mqtt.NewClient(opts)
}

func main() {

    var client = createClient()
    if token := client.Connect(); token.Wait() && token.Error() != nil {
        panic(token.Error())
    }

    // load command line arguments if any
    name := flag.String("name", "world", "name to print")
    flag.Parse()

    log.Printf("Starting sleepservice for %s", *name)

    // setup signal catching
    sigs := make(chan os.Signal, 1)

    // catch all signals since not explicitly listing
    //signal.Notify(sigs)
    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

    // method invoked upon seeing signal
    go func() {
        s := <-sigs
        log.Printf("RECEIVED SIGNAL: %s", s)

        switch s {
        case syscall.SIGINT:
            AppCleanup(client)
            os.Exit(1)
        case syscall.SIGTERM:
            AppCleanup(client)
            os.Exit(1)
        case syscall.SIGQUIT:
            AppCleanup(client)
            os.Exit(1)
        default:
            log.Printf("not supported Signal")
        }

    }()

    sub(client)

    for { /* Endless Loop */
    }

}

func AppCleanup(client mqtt.Client) {
    client.Disconnect(250)
    log.Println("CLEANUP APP BEFORE EXIT!!!")
}

func sub(client mqtt.Client) {
    topic := "test/topic"
    token := client.Subscribe(topic, 1, nil)
    token.Wait()
    fmt.Printf("Subscribed to topic: %s", topic)
}


here, I have hidden the IP of broker

your help is much appreciated. Thanks in advance.

Abhishek
  • 75
  • 7
  • 1
    Using an endless loop is not a great strategy - see [this demo](https://github.com/eclipse/paho.mqtt.golang/blob/master/cmd/docker/subscriber/main.go) for an alternative approach. Can you please share info on your broker (and ideally broker logs). Please also confirm what version of the library you are using. – Brits Dec 19 '21 at 06:18

1 Answers1

0

Finally I found the problem, It was that last endless for loop which was causing heck. I removed it and now problem seems to be solved. Thanks @asad and @Brits

Abhishek
  • 75
  • 7
  • I thought I should note that while an infinite do-nothing loop is an issue (it needlessly uses resources) I would not expect it, by itself, to cause a symptoms like this. The paho client runs a number of goroutines and deadlock situations have been discovered/resolved in the past. I suspect that introducing the infinite loop revealed another, usually hidden, issue (unfortunately diagnosing this kind of thing can be tricky!). Glad that its working for you (I have clients using this library that have been up for over a year so it can be stable but I cant say its bug-free!). – Brits Dec 19 '21 at 22:52