-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add InfoHandler and ClientInfoEmitter for exposing connected clients …
…information (#34) * add TelemetryHandler for exposing connected brokers information * add ClientInfoEmitter * chore: add json tags to TCPAddress * feat: add subscriptions to info response * test: add spec for multi conn mode * feat: add shared subscriptions called information * use IsConnectionOpen to determine connected state * refactor: use execOptWithState to read internalState in multiClientInfo
- Loading branch information
1 parent
fcaba40
commit 6e2933c
Showing
12 changed files
with
598 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package courier | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClient_readSubscriptionMeta(t *testing.T) { | ||
c := &Client{ | ||
subMu: sync.RWMutex{}, | ||
subscriptions: map[string]*subscriptionMeta{ | ||
"topic1": { | ||
topic: "topic1", | ||
options: []Option{QOSOne}, | ||
}, | ||
"topic2": { | ||
topic: "topic2", | ||
}, | ||
"topic3": { | ||
topic: "topic3", | ||
options: []Option{QOSTwo}, | ||
}, | ||
}, | ||
} | ||
|
||
subs := c.readSubscriptionMeta() | ||
|
||
assert.Equal(t, map[string]QOSLevel{ | ||
"topic1": 1, | ||
"topic2": 0, | ||
"topic3": 2, | ||
}, subs) | ||
} | ||
|
||
func TestClient_clientInfo(t *testing.T) { | ||
t.Run("single connection mode", func(t *testing.T) { | ||
c := &Client{options: &clientOptions{}} | ||
ci := c.clientInfo() | ||
assert.Equal(t, []MQTTClientInfo(nil), ci) | ||
}) | ||
|
||
t.Run("multi connection mode", func(t *testing.T) { | ||
c := &Client{options: &clientOptions{multiConnectionMode: true}} | ||
ci := c.clientInfo() | ||
assert.Equal(t, []MQTTClientInfo(nil), ci) | ||
}) | ||
} |
Oops, something went wrong.