55 "fmt"
66 "net/http"
77 "runtime"
8- "sync"
98 "time"
109
1110 "bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventQueue"
@@ -26,6 +25,7 @@ type SumoBuffer struct {
2625 logStringToSend * bytes.Buffer
2726 logEventsInCurrentBuffer int
2827 timerPostMinimum time.Time
28+ channelMessage chan string
2929}
3030
3131func NewSumoLogicAppender (urlValue string , connectionTimeoutValue int , nozzleQueue * eventQueue.Queue , eventsBatchSize int , sumoPostMinimumDelay time.Duration ) * SumoLogicAppender {
@@ -44,25 +44,29 @@ func newBuffer() SumoBuffer {
4444 logStringToSend : bytes .NewBufferString ("" ),
4545 logEventsInCurrentBuffer : 0 ,
4646 timerPostMinimum : time .Now (),
47+ channelMessage : make (chan string ),
4748 }
4849}
4950
5051func (s * SumoLogicAppender ) Start () {
5152 runtime .GOMAXPROCS (1 )
5253 timer := time .Now ()
53- Buffer := newBuffer ()
54- var mutex = & sync. Mutex {} //synchronize access to Buffer
54+ Buffer := newBuffer () //creating Buffer
55+ msgFromChannel := ""
5556 logging .Info .Println ("Starting Appender Worker" )
56-
5757 for {
58- time .Sleep (300 * time .Millisecond ) //delay
59- if Buffer .logEventsInCurrentBuffer >= s .eventsBatchSize { //if buffer is full, create a new one
58+ fmt .Println ("first for" )
59+ time .Sleep (300 * time .Millisecond ) //delay
60+ if Buffer .logEventsInCurrentBuffer >= s .eventsBatchSize || msgFromChannel == "Buffer in use" { //if buffer is full, create a new one
6061 fmt .Println ("Creating new Buffer" )
62+ fmt .Println (Buffer .logEventsInCurrentBuffer )
63+ fmt .Println (msgFromChannel )
6164 Buffer = newBuffer ()
6265 }
63- mutex .Lock () //lock mutex to ensure exclusive access to buffer
66+ // mutex.Lock() //lock mutex to ensure exclusive access to buffer
6467 // while queue is not empty && s.eventsBatchSize not completed, queue.POP (appendLogs)
6568 for s .nozzleQueue .GetCount () != 0 && Buffer .logEventsInCurrentBuffer < s .eventsBatchSize {
69+ fmt .Println ("second for" )
6670 s .AppendLogs (& Buffer ) //this method POP an event from queue to Buffer
6771 timer = time .Now () //reset timer
6872 if Buffer .logEventsInCurrentBuffer == s .eventsBatchSize { //if buffer is full, send logs to sumo
@@ -73,9 +77,10 @@ func (s *SumoLogicAppender) Start() {
7377 break
7478 }
7579 }
76- //if batch size is met, send to sumo and reset temp buffer
80+ //if batch size is met, send to sumo
7781 go s .SendToSumo (& Buffer )
78- mutex .Unlock ()
82+ msgFromChannel = <- Buffer .channelMessage
83+
7984 timer = time .Now () //reset timer
8085 }
8186
@@ -104,6 +109,7 @@ func (s *SumoLogicAppender) AppendLogs(buffer *SumoBuffer) {
104109}
105110
106111func (s * SumoLogicAppender ) SendToSumo (buffer * SumoBuffer ) {
112+ buffer .channelMessage <- "Buffer in use"
107113 //wait period between posts to Sumo
108114 fmt .Println (buffer .logStringToSend .String ())
109115 fmt .Println ("......................" )
@@ -128,5 +134,5 @@ func (s *SumoLogicAppender) SendToSumo(buffer *SumoBuffer) {
128134 }
129135
130136 defer response .Body .Close ()
131-
137+ buffer . channelMessage <- "Buffer finished"
132138}
0 commit comments