Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concurrency in ES client message buffer #54

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions plugins/application/elasticsearch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"sync"
"time"

"github.com/infrawatch/apputils/logging"
Expand Down Expand Up @@ -52,6 +53,7 @@ type Elasticsearch struct {
logger *logging.Logger
client *lib.Client
buffer map[string][]string
bufferMutex sync.RWMutex
dump chan esIndex
}

Expand Down Expand Up @@ -88,19 +90,23 @@ func (es *Elasticsearch) ReceiveEvent(event data.Event) {
// buffer or index record
var recordList []string
if es.configuration.BufferSize > 1 {
es.bufferMutex.Lock()
if _, ok := es.buffer[event.Index]; !ok {
es.buffer[event.Index] = make([]string, 0, es.configuration.BufferSize)
}

es.buffer[event.Index] = append(es.buffer[event.Index], record)
if len(es.buffer[event.Index]) < es.configuration.BufferSize {
es.bufferMutex.Unlock()
// buffer is not full, don't send
es.logger.Metadata(logging.Metadata{"plugin": appname, "record": record})
es.logger.Debug("buffering record")
return
}
recordList = es.buffer[event.Index]
delete(es.buffer, event.Index)
es.bufferMutex.Unlock()

} else {
recordList = []string{record}
}
Expand Down