plugin(mesos): Added goroutines.

The plugin will iterate over the Servers slice and create a goroutine
for each of them.
This commit is contained in:
Sergio Jimenez 2016-02-02 02:17:38 +01:00 committed by Michele Fadda
parent a165c1c7c0
commit 2cefe2fc2a
1 changed files with 27 additions and 3 deletions

View File

@ -6,6 +6,8 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"strings"
"sync"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
@ -196,14 +198,36 @@ func (m *Mesos) Description() string {
} }
func (m *Mesos) Gather(acc telegraf.Accumulator) error { func (m *Mesos) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
var errorChannel chan error
if len(m.Servers) == 0 { if len(m.Servers) == 0 {
return m.gatherMetrics("localhost:5050", acc) m.Servers = []string{"localhost:5050"}
} }
errorChannel = make(chan error, len(m.Servers)*2)
for _, v := range m.Servers { for _, v := range m.Servers {
if err := m.gatherMetrics(v, acc); err != nil { wg.Add(1)
return err go func() {
errorChannel <- m.gatherMetrics(v, acc)
wg.Done()
return
}()
} }
wg.Wait()
close(errorChannel)
errorStrings := []string{}
for err := range errorChannel {
if err != nil {
errorStrings = append(errorStrings, err.Error())
}
}
if len(errorStrings) > 0 {
return errors.New(strings.Join(errorStrings, "\n"))
} }
return nil return nil
} }