yosssi / gmq

Pure Go MQTT Client
MIT License
346 stars 46 forks source link

How to reliably wait for a message to get sent? #11

Open mwuertinger opened 6 years ago

mwuertinger commented 6 years ago

I have a simple application that just sends one single message and then terminates:

func main() {
    cli = ...

    err = cli.Publish(&client.PublishOptions{
        QoS: mqtt.QoS2,
        TopicName: []byte("foo/bar"),
        Message:   []byte("test"),
    })
    if err != nil {
        log.Fatalf("publish failed: %v", err)
    }
}

Unfortunately the message never gets sent although Publish returns nil. The reason for this behavior is the asynchronous message processing within the library.

Is there any way to reliably wait for a message to get sent?

I could imagine that initializing the cli.conn.send channel with a capacity of 0 (ie. setting sendBufSize = 0) could resolve the issue but I wanted to ask here first whether this is the right approach.

skx commented 6 years ago

As per the referenced issue above I am also encountering the same issue.

I have come up with a horrid solution which does work:

Not ideal, but it has stood up to repeated testing.

mq.go.txt