-
Notifications
You must be signed in to change notification settings - Fork 11
/
client_telemetry.go
157 lines (119 loc) · 3.4 KB
/
client_telemetry.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package courier
import (
"net/url"
"sort"
"strconv"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/gojekfarm/xtools/generic/slice"
"github.com/gojekfarm/xtools/generic/xmap"
)
// MQTTClientInfo contains information about the internal MQTT client
type MQTTClientInfo struct {
Addresses []TCPAddress `json:"addresses"`
ClientID string `json:"client_id"`
Username string `json:"username"`
ResumeSubs bool `json:"resume_subs"`
CleanSession bool `json:"clean_session"`
AutoReconnect bool `json:"auto_reconnect"`
Connected bool `json:"connected"`
// Subscriptions contains the topics the client is subscribed to
// Note: Currently, this field only holds shared subscriptions.
Subscriptions []string `json:"subscriptions,omitempty"`
}
type infoResponse struct {
MultiConnMode bool `json:"multi"`
Clients []MQTTClientInfo `json:"clients,omitempty"`
Subscriptions map[string]QOSLevel `json:"subscriptions,omitempty"`
}
func (c *Client) infoResponse() *infoResponse {
subs := c.readSubscriptionMeta()
ci := c.clientInfo()
return &infoResponse{
MultiConnMode: c.options.multiConnectionMode,
Clients: ci,
Subscriptions: subs,
}
}
func (c *Client) clientInfo() []MQTTClientInfo {
if c.options.multiConnectionMode {
return c.multiClientInfo()
}
return c.singleClientInfo()
}
func (c *Client) readSubscriptionMeta() map[string]QOSLevel {
c.subMu.RLock()
subs := make(map[string]QOSLevel, len(c.subscriptions))
for topic, sub := range c.subscriptions {
for _, opt := range sub.options {
switch v := opt.(type) {
case QOSLevel:
subs[topic] = v
}
}
// if no QOSLevel Option is provided, default to QOSZero
if _, ok := subs[topic]; !ok {
subs[topic] = QOSZero
}
}
c.subMu.RUnlock()
return subs
}
func (c *Client) multiClientInfo() []MQTTClientInfo {
c.clientMu.RLock()
cls := xmap.Values(c.mqttClients)
if len(cls) == 0 {
c.clientMu.RUnlock()
return nil
}
bCh := make(chan MQTTClientInfo, len(cls))
_ = c.execute(
func(cc mqtt.Client) error { return nil },
execOptWithState(func(f func(mqtt.Client) error, is *internalState) error {
is.mu.Lock()
subs := is.subsCalled.Values()
is.mu.Unlock()
bCh <- transformClientInfo(is.client, subs...)
return f(is.client)
}),
)
c.clientMu.RUnlock()
close(bCh)
bl := make([]MQTTClientInfo, 0, len(bCh))
for b := range bCh {
bl = append(bl, b)
}
sort.Slice(bl, func(i, j int) bool { return bl[i].ClientID < bl[j].ClientID })
return bl
}
func (c *Client) singleClientInfo() []MQTTClientInfo {
c.clientMu.RLock()
defer c.clientMu.RUnlock()
if c.mqttClient == nil {
return nil
}
var bi MQTTClientInfo
_ = c.execute(func(cc mqtt.Client) error {
bi = transformClientInfo(cc)
return nil
}, execAll)
return []MQTTClientInfo{bi}
}
func transformClientInfo(cc mqtt.Client, subscribedTopics ...string) MQTTClientInfo {
opts := cc.OptionsReader()
return MQTTClientInfo{
Addresses: slice.Map(opts.Servers(), func(u *url.URL) TCPAddress {
i, _ := strconv.Atoi(u.Port())
return TCPAddress{
Host: u.Hostname(),
Port: uint16(i),
}
}),
ClientID: opts.ClientID(),
Username: opts.Username(),
ResumeSubs: opts.ResumeSubs(),
CleanSession: opts.CleanSession(),
AutoReconnect: opts.AutoReconnect(),
Connected: cc.IsConnectionOpen(),
Subscriptions: subscribedTopics,
}
}