From 6622050a34f37192e7f051ae1cc166e7cfab5b3a Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Mon, 25 Nov 2019 03:24:36 -0800 Subject: [PATCH] shave a few more nanoseconds --- promunifi/clients.go | 5 ++--- promunifi/collector.go | 25 +++++++++++++------------ promunifi/site.go | 4 ++-- promunifi/uap.go | 5 ++--- promunifi/udm.go | 5 ++--- promunifi/usg.go | 6 +++--- promunifi/usw.go | 5 ++--- 7 files changed, 26 insertions(+), 29 deletions(-) diff --git a/promunifi/clients.go b/promunifi/clients.go index 7bab559b..68e8118c 100644 --- a/promunifi/clients.go +++ b/promunifi/clients.go @@ -82,11 +82,10 @@ func descClient(ns string) *uclient { } } -func (u *unifiCollector) exportClients(clients []*unifi.Client) (e []*metricExports) { +func (u *unifiCollector) exportClients(clients []*unifi.Client, ch chan []*metricExports) { for _, c := range clients { - e = append(e, u.exportClient(c)...) + ch <- u.exportClient(c) } - return } // CollectClient exports Clients' Data diff --git a/promunifi/collector.go b/promunifi/collector.go index 9cfa90d3..6714f048 100644 --- a/promunifi/collector.go +++ b/promunifi/collector.go @@ -54,7 +54,7 @@ type Report struct { Elapsed time.Duration Start time.Time ch chan []*metricExports - wait sync.WaitGroup + wg sync.WaitGroup } // NewUnifiCollector returns a prometheus collector that will export any available @@ -104,7 +104,7 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) { var err error r := &Report{Start: time.Now(), ch: make(chan []*metricExports)} defer func() { - r.wait.Wait() + r.wg.Wait() close(r.ch) }() @@ -116,22 +116,23 @@ func (u *unifiCollector) Collect(ch chan<- prometheus.Metric) { go u.exportMetrics(ch, r) - r.wait.Add(2) - go func() { r.ch <- u.exportClients(r.Metrics.Clients) }() - go func() { r.ch <- u.exportSites(r.Metrics.Sites) }() + r.wg.Add(len(r.Metrics.Clients) + len(r.Metrics.Sites)) + go u.exportClients(r.Metrics.Clients, r.ch) + go u.exportSites(r.Metrics.Sites, r.ch) if r.Metrics.Devices == nil { return } - r.wait.Add(4) - go func() { r.ch <- u.exportUAPs(r.Metrics.Devices.UAPs) }() - go func() { r.ch <- u.exportUSGs(r.Metrics.Devices.USGs) }() - go func() { r.ch <- u.exportUSWs(r.Metrics.Devices.USWs) }() - go func() { r.ch <- u.exportUDMs(r.Metrics.Devices.UDMs) }() + r.wg.Add(len(r.Metrics.Devices.UAPs) + len(r.Metrics.Devices.USGs) + len(r.Metrics.Devices.USWs) + len(r.Metrics.Devices.UDMs)) + go u.exportUAPs(r.Metrics.Devices.UAPs, r.ch) + go u.exportUSGs(r.Metrics.Devices.USGs, r.ch) + go u.exportUSWs(r.Metrics.Devices.USWs, r.ch) + go u.exportUDMs(r.Metrics.Devices.UDMs, r.ch) } -// Call this once (at least as-is). It sets all the counters and runs the logging function. +// This is closely tied to the method above with a sync.WaitGroup. +// This method runs in a go routine and exits when the channel closes. func (u *unifiCollector) exportMetrics(ch chan<- prometheus.Metric, r *Report) { descs := make(map[*prometheus.Desc]bool) // used as a counter for newMetrics := range r.ch { @@ -154,7 +155,7 @@ func (u *unifiCollector) exportMetrics(ch chan<- prometheus.Metric, r *Report) { } } } - r.wait.Done() + r.wg.Done() } if u.Config.LoggingFn == nil { diff --git a/promunifi/site.go b/promunifi/site.go index 140372f8..b89ef2f3 100644 --- a/promunifi/site.go +++ b/promunifi/site.go @@ -67,9 +67,9 @@ func descSite(ns string) *site { } } -func (u *unifiCollector) exportSites(sites unifi.Sites) (e []*metricExports) { +func (u *unifiCollector) exportSites(sites unifi.Sites, ch chan []*metricExports) (e []*metricExports) { for _, s := range sites { - e = append(e, u.exportSite(s)...) + ch <- u.exportSite(s) } return } diff --git a/promunifi/uap.go b/promunifi/uap.go index d6c791d3..7a71203b 100644 --- a/promunifi/uap.go +++ b/promunifi/uap.go @@ -102,11 +102,10 @@ func descUAP(ns string) *uap { } } -func (u *unifiCollector) exportUAPs(uaps []*unifi.UAP) (e []*metricExports) { +func (u *unifiCollector) exportUAPs(uaps []*unifi.UAP, ch chan []*metricExports) { for _, a := range uaps { - e = append(e, u.exportUAP(a)...) + ch <- u.exportUAP(a) } - return } // exportUAP exports Access Point Data diff --git a/promunifi/udm.go b/promunifi/udm.go index b09bbaa0..081c25c9 100644 --- a/promunifi/udm.go +++ b/promunifi/udm.go @@ -11,11 +11,10 @@ func descUDM(ns string) *udm { return &udm{} } -func (u *unifiCollector) exportUDMs(udms []*unifi.UDM) (e []*metricExports) { +func (u *unifiCollector) exportUDMs(udms []*unifi.UDM, ch chan []*metricExports) { for _, d := range udms { - e = append(e, u.exportUDM(d)...) + ch <- u.exportUDM(d) } - return } // exportUDM exports UniFi Dream Machine (and Pro) Data diff --git a/promunifi/usg.go b/promunifi/usg.go index e4cc58b5..7e832920 100644 --- a/promunifi/usg.go +++ b/promunifi/usg.go @@ -109,11 +109,10 @@ func descUSG(ns string) *usg { } } -func (u *unifiCollector) exportUSGs(usgs []*unifi.USG) (e []*metricExports) { +func (u *unifiCollector) exportUSGs(usgs []*unifi.USG, ch chan []*metricExports) { for _, sg := range usgs { - e = append(e, u.exportUSG(sg)...) + ch <- u.exportUSG(sg) } - return } // exportUSG Exports Security Gateway Data @@ -122,6 +121,7 @@ func (u *unifiCollector) exportUSG(s *unifi.USG) []*metricExports { labels := []string{s.SiteName, s.Mac, s.Model, s.Name, s.Serial, s.SiteID, s.Type, s.Version, s.DeviceID, s.IP} labelWan := append([]string{"all"}, labels...) + // r.wait.Add(1) // closed by channel receiver. // Gateway System Data. return append([]*metricExports{ diff --git a/promunifi/usw.go b/promunifi/usw.go index 6c496428..7cbefcf9 100644 --- a/promunifi/usw.go +++ b/promunifi/usw.go @@ -136,11 +136,10 @@ func descUSW(ns string) *usw { } } -func (u *unifiCollector) exportUSWs(usws []*unifi.USW) (e []*metricExports) { +func (u *unifiCollector) exportUSWs(usws []*unifi.USW, ch chan []*metricExports) { for _, sw := range usws { - e = append(e, u.exportUSW(sw)...) + ch <- u.exportUSW(sw) } - return } // exportUSW exports Network Switch Data