Manage number of goroutines

This commit is contained in:
Jack Zampolin 2016-09-02 13:09:54 -07:00
parent 522c45eb8a
commit 2287edce0a
1 changed files with 12 additions and 12 deletions

View File

@ -40,7 +40,7 @@ type RabbitMQParser struct {
ch *amqp.Channel
q amqp.Queue
drops int
acks int
parsed int
log *os.File
cl *ConcurrencyLimiter
@ -66,11 +66,11 @@ func (rmq *RabbitMQParser) SampleConfig() string {
// Gather satisfies the telegraf.ServiceInput interface
// All gathering is done in the Start function
func (rmq *RabbitMQParser) Gather(_ telegraf.Accumulator) error {
numMessages := rmq.drops + rmq.acks
numMessages := rmq.drops + rmq.parsed
percentDrops := (float64(rmq.drops) / float64(numMessages)) * 100.0
log.Printf("Dropped %.2f%% of %d metrics in the last interval", percentDrops, numMessages)
rmq.drops = 0
rmq.acks = 0
rmq.parsed = 0
return nil
}
@ -85,7 +85,7 @@ func (rmq *RabbitMQParser) Start(acc telegraf.Accumulator) error {
rmq.log = f
// Limit number of workers to the number of CPU on system
rmq.cl = NewConcurrencyLimiter(runtime.NumCPU() * 2)
rmq.cl = NewConcurrencyLimiter(runtime.NumCPU())
// Create queue connection and assign it to RabbitMQParser
conn, err := amqp.Dial(rmq.RabbitmqAddress)
@ -147,7 +147,7 @@ func (rmq *RabbitMQParser) registerConsumer() <-chan amqp.Delivery {
func (rmq *RabbitMQParser) listen(msgs <-chan amqp.Delivery, acc telegraf.Accumulator) {
for d := range msgs {
rmq.cl.Increment()
rmq.handleMessage(d, acc)
go rmq.handleMessage(d, acc)
}
}
@ -168,7 +168,7 @@ func (rmq *RabbitMQParser) handleMessage(d amqp.Delivery, acc telegraf.Accumulat
}
d.Ack(false)
acc.AddFields(msg.Name(), msg.Fields(), msg.Tags(), msg.Time())
rmq.acks++
rmq.parsed++
rmq.cl.Decrement()
}