Skip to content

Commit

Permalink
fix datarace in input apache plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kaey authored and Srini Chebrolu committed Jun 24, 2016
1 parent 50b826b commit 3a4c85d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [#1374](https://github.com/influxdata/telegraf/pull/1374): Change "default" retention policy to "".
- [#1379] (https://github.com/influxdata/telegraf/issues/1379): Fix covering Amazon Linux for post remove flow.
- [#1377](https://github.com/influxdata/telegraf/issues/1377): Graphite output mangling '%' character.
- [#1384](https://github.com/influxdata/telegraf/pull/1384): Fix datarace in apache input plugin.

## v1.0 beta 1 [2016-06-07]

Expand Down
14 changes: 8 additions & 6 deletions plugins/inputs/apache/apache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/url"
"strconv"
"strings"
"sync"
"time"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -38,23 +37,26 @@ 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)
if err != nil {
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
}
Expand Down
3 changes: 2 additions & 1 deletion plugins/inputs/apache/apache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3a4c85d

Please sign in to comment.