Skip to content

Commit b092cea

Browse files
committed
Use WaitTimeout for MQTT
1 parent 6b2025a commit b092cea

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

internal/endpoint/mqtt.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package endpoint
33
import (
44
"crypto/tls"
55
"crypto/x509"
6-
"math/rand"
76
"fmt"
87
"io/ioutil"
8+
"math/rand"
99
"sync"
1010
"time"
1111

@@ -14,7 +14,8 @@ import (
1414
)
1515

1616
const (
17-
mqttExpiresAfter = time.Second * 30
17+
mqttExpiresAfter = time.Second * 30
18+
mqttPublishTimeout = time.Second * 5
1819
)
1920

2021
// MQTTConn is an endpoint connection
@@ -85,12 +86,12 @@ func (conn *MQTTConn) Send(msg string) error {
8586
}
8687
ops = ops.SetTLSConfig(&config)
8788
}
88-
//generate UUID for the client-id.
89+
//generate UUID for the client-id.
8990
b := make([]byte, 16)
9091
_, err := rand.Read(b)
9192
if err != nil {
9293
log.Debugf("Failed to generate guid for the mqtt client. The endpoint will not work")
93-
return err;
94+
return err
9495
}
9596
uuid := fmt.Sprintf("tile38-%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
9697

@@ -106,9 +107,8 @@ func (conn *MQTTConn) Send(msg string) error {
106107

107108
t := conn.conn.Publish(conn.ep.MQTT.QueueName, conn.ep.MQTT.Qos,
108109
conn.ep.MQTT.Retained, msg)
109-
t.Wait()
110110

111-
if t.Error() != nil {
111+
if !t.WaitTimeout(mqttPublishTimeout) || t.Error() != nil {
112112
conn.close()
113113
return t.Error()
114114
}

0 commit comments

Comments
 (0)