forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
74 lines (62 loc) · 1.96 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package mqtt
import (
"time"
pahomqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/influxdata/kapacitor/tlsconfig"
)
// Client describes an immutable MQTT client, designed to accommodate the
// incongruencies between real clients and mock clients.
type Client interface {
Connect() error
Disconnect()
Publish(topic string, qos QoSLevel, retained bool, message []byte) error
}
// newClient produces a disconnected MQTT client
var newClient = func(c Config) (Client, error) {
opts := pahomqtt.NewClientOptions()
opts.AddBroker(c.URL)
if c.ClientID != "" {
opts.SetClientID(c.ClientID)
} else {
opts.SetClientID(c.Name)
}
opts.SetUsername(c.Username)
opts.SetPassword(c.Password)
tlsConfig, err := tlsconfig.Create(c.SSLCA, c.SSLCert, c.SSLKey, c.InsecureSkipVerify)
if err != nil {
return nil, err
}
opts.SetTLSConfig(tlsConfig)
return &PahoClient{
opts: opts,
}, nil
}
type PahoClient struct {
opts *pahomqtt.ClientOptions
client pahomqtt.Client
}
// DefaultQuiesceTimeout is the duration the client will wait for outstanding
// messages to be published before forcing a disconnection
const DefaultQuiesceTimeout time.Duration = 250 * time.Millisecond
func (p *PahoClient) Connect() error {
// Using a clean session forces the broker to dispose of client session
// information after disconnecting. Retention of this is useful for
// constrained clients. Since Kapacitor is only publishing, it has no
// storage requirements and can reduce load on the broker by using a clean
// session.
p.opts.SetCleanSession(true)
p.client = pahomqtt.NewClient(p.opts)
token := p.client.Connect()
token.Wait()
return token.Error()
}
func (p *PahoClient) Disconnect() {
if p.client != nil {
p.client.Disconnect(uint(DefaultQuiesceTimeout / time.Millisecond))
}
}
func (p *PahoClient) Publish(topic string, qos QoSLevel, retained bool, message []byte) error {
token := p.client.Publish(topic, byte(qos), retained, message)
token.Wait()
return token.Error()
}