Skip to content

Commit

Permalink
refactor: use execOptWithState to read internalState in multiClientInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha committed Feb 9, 2024
1 parent 6b4ffe3 commit 63df80a
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions client_telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
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
Expand Down Expand Up @@ -76,7 +77,7 @@ func (c *Client) readSubscriptionMeta() map[string]QOSLevel {
func (c *Client) multiClientInfo() []MQTTClientInfo {
c.clientMu.RLock()

cls := c.mqttClients
cls := xmap.Values(c.mqttClients)

if len(cls) == 0 {
c.clientMu.RUnlock()
Expand All @@ -86,23 +87,18 @@ func (c *Client) multiClientInfo() []MQTTClientInfo {

bCh := make(chan MQTTClientInfo, len(cls))

_ = c.execute(func(cc mqtt.Client) error {
for _, is := range cls {
if is.client == cc {
is.mu.Lock()
subs := is.subsCalled.Values()
is.mu.Unlock()

bCh <- transformClientInfo(cc, subs...)

return nil
}
}
_ = 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(cc)
bCh <- transformClientInfo(is.client, subs...)

return nil
}, execAll)
return f(is.client)
}),
)

c.clientMu.RUnlock()

Expand Down

0 comments on commit 63df80a

Please sign in to comment.