From d55fa88dd0d6eb77a2cd263c31206f975c9cb6c4 Mon Sep 17 00:00:00 2001 From: Konstantin Kulikov Date: Fri, 17 Jun 2016 23:14:29 +0300 Subject: [PATCH] fix datarace in input apache plugin --- plugins/inputs/apache/apache.go | 14 ++++++++------ plugins/inputs/apache/apache_test.go | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/plugins/inputs/apache/apache.go b/plugins/inputs/apache/apache.go index dc5dddb9d..be891bb31 100644 --- a/plugins/inputs/apache/apache.go +++ b/plugins/inputs/apache/apache.go @@ -8,7 +8,6 @@ import ( "net/url" "strconv" "strings" - "sync" "time" "github.com/influxdata/telegraf" @@ -38,8 +37,8 @@ func (n *Apache) Gather(acc telegraf.Accumulator) error { n.Urls = []string{"http://localhost/server-status?auto"} } - var wg sync.WaitGroup var outerr error + var errch = make(chan error) for _, u := range n.Urls { addr, err := url.Parse(u) @@ -47,14 +46,17 @@ func (n *Apache) Gather(acc telegraf.Accumulator) error { return fmt.Errorf("Unable to parse address '%s': %s", u, err) } - wg.Add(1) go func(addr *url.URL) { - defer wg.Done() - outerr = n.gatherUrl(addr, acc) + errch <- n.gatherUrl(addr, acc) }(addr) } - wg.Wait() + // Drain channel, waiting for all requests to finish and save last error. + for range n.Urls { + if err := <-errch; err != nil { + outerr = err + } + } return outerr } diff --git a/plugins/inputs/apache/apache_test.go b/plugins/inputs/apache/apache_test.go index 8eed61ca6..2a80b3868 100644 --- a/plugins/inputs/apache/apache_test.go +++ b/plugins/inputs/apache/apache_test.go @@ -36,7 +36,8 @@ func TestHTTPApache(t *testing.T) { defer ts.Close() a := Apache{ - Urls: []string{ts.URL}, + // Fetch it 2 times to catch possible data races. + Urls: []string{ts.URL, ts.URL}, } var acc testutil.Accumulator