Skip to content

Commit d602b7a

Browse files
committed
SCFF-35 sendToSumo timer modify, added lock for buffer struct
1 parent 7b75451 commit d602b7a

1 file changed

Lines changed: 19 additions & 27 deletions

File tree

sumoCFFirehose/sumoLogicAppender.go

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ type SumoLogicAppender struct {
2020
nozzleQueue *eventQueue.Queue
2121
eventsBatchSize int
2222
sumoPostMinimumDelay time.Duration
23-
timerPostMinimum time.Time
24-
bufferToSend SumoBuffer
2523
}
2624

2725
type SumoBuffer struct {
2826
logStringToSend *bytes.Buffer
2927
logEventsInCurrentBuffer int
28+
timerPostMinimum time.Time
3029
}
3130

3231
func NewSumoLogicAppender(urlValue string, connectionTimeoutValue int, nozzleQueue *eventQueue.Queue, eventsBatchSize int, sumoPostMinimumDelay time.Duration) *SumoLogicAppender {
@@ -37,40 +36,34 @@ func NewSumoLogicAppender(urlValue string, connectionTimeoutValue int, nozzleQue
3736
nozzleQueue: nozzleQueue,
3837
eventsBatchSize: eventsBatchSize,
3938
sumoPostMinimumDelay: sumoPostMinimumDelay,
40-
timerPostMinimum: time.Now(),
41-
bufferToSend: SumoBuffer{
42-
logStringToSend: bytes.NewBufferString(""),
43-
logEventsInCurrentBuffer: 0,
44-
},
4539
}
4640
}
4741

48-
func (s *SumoLogicAppender) Start() {
49-
runtime.GOMAXPROCS(1)
50-
timer := time.Now()
51-
Buffer := &SumoBuffer{
42+
func newBuffer() SumoBuffer {
43+
return SumoBuffer{
5244
logStringToSend: bytes.NewBufferString(""),
5345
logEventsInCurrentBuffer: 0,
46+
timerPostMinimum: time.Now(),
5447
}
55-
var mutex = &sync.Mutex{} //synchronize access to Buffer
48+
}
5649

50+
func (s *SumoLogicAppender) Start() {
51+
runtime.GOMAXPROCS(1)
52+
timer := time.Now()
53+
Buffer := newBuffer()
54+
var mutex = &sync.Mutex{} //synchronize access to Buffer
5755
logging.Info.Println("Starting Appender Worker")
58-
for {
59-
time.Sleep(300 * time.Millisecond) //delay
6056

61-
mutex.Lock() //lock mutex to ensure exclusive access to buffer
57+
for {
58+
time.Sleep(300 * time.Millisecond) //delay
6259
if Buffer.logEventsInCurrentBuffer >= s.eventsBatchSize { //if buffer is full, create a new one
63-
Buffer = &SumoBuffer{
64-
logStringToSend: bytes.NewBufferString(""),
65-
logEventsInCurrentBuffer: 0,
66-
}
60+
fmt.Println("Creating new Buffer")
61+
Buffer = newBuffer()
6762
}
68-
mutex.Unlock()
63+
mutex.Lock() //lock mutex to ensure exclusive access to buffer
6964
// while queue is not empty && s.eventsBatchSize not completed, queue.POP (appendLogs)
7065
for s.nozzleQueue.GetCount() != 0 && Buffer.logEventsInCurrentBuffer < s.eventsBatchSize {
71-
mutex.Lock() //lock mutex to ensure exclusive access to buffer
72-
s.AppendLogs(Buffer) //this method POP an event from queue to Buffer
73-
mutex.Unlock()
66+
s.AppendLogs(&Buffer) //this method POP an event from queue to Buffer
7467
timer = time.Now() //reset timer
7568
if Buffer.logEventsInCurrentBuffer == s.eventsBatchSize { //if buffer is full, send logs to sumo
7669
logging.Info.Println("Batch Size complete")
@@ -81,8 +74,7 @@ func (s *SumoLogicAppender) Start() {
8174
}
8275
}
8376
//if batch size is met, send to sumo and reset temp buffer
84-
mutex.Lock()
85-
s.SendToSumo(Buffer)
77+
go s.SendToSumo(&Buffer)
8678
mutex.Unlock()
8779
timer = time.Now() //reset timer
8880
}
@@ -115,7 +107,7 @@ func (s *SumoLogicAppender) SendToSumo(buffer *SumoBuffer) {
115107
//wait period between posts to Sumo
116108
fmt.Println(buffer.logStringToSend.String())
117109
fmt.Println("......................")
118-
for time.Since(s.timerPostMinimum) < s.sumoPostMinimumDelay {
110+
for time.Since(buffer.timerPostMinimum) < s.sumoPostMinimumDelay {
119111
time.Sleep(30 * time.Millisecond) // wait to retry
120112
}
121113
logging.Trace.Println("Sending logs to Sumologic...")
@@ -132,7 +124,7 @@ func (s *SumoLogicAppender) SendToSumo(buffer *SumoBuffer) {
132124
return
133125
} else {
134126
logging.Trace.Println("Do(Request) successful")
135-
s.timerPostMinimum = time.Now() //reset timer post minimum
127+
buffer.timerPostMinimum = time.Now() //reset timer post minimum
136128
}
137129

138130
defer response.Body.Close()

0 commit comments

Comments
 (0)