@@ -19,9 +19,9 @@ import (
1919)
2020
2121// StartServer starts the server
22- func StartServer (ctx context.Context , cancel context.CancelFunc ) {
22+ func StartServer (ctx context.Context , cancel context.CancelFunc , shutdown chan bool ) {
2323 bufferChannel := make (chan collection.CollectRequest , config .Worker .ChannelSize )
24- httpServices := services .Create (bufferChannel )
24+ httpServices := services .Create (bufferChannel , ctx )
2525 logger .Info ("Start Server -->" )
2626 httpServices .Start (ctx , cancel )
2727 logger .Info ("Start publisher -->" )
@@ -37,38 +37,47 @@ func StartServer(ctx context.Context, cancel context.CancelFunc) {
3737 workerPool .StartWorkers ()
3838 go kPublisher .ReportStats ()
3939 go reportProcMetrics ()
40- go shutDownServer (ctx , cancel , httpServices , bufferChannel , workerPool , kPublisher )
40+ // create signal channel at startup
41+ signalChan := make (chan os.Signal , 1 )
42+ signal .Notify (signalChan , syscall .SIGHUP , syscall .SIGINT , syscall .SIGTERM , syscall .SIGQUIT )
43+
44+ go shutDownServer (ctx , cancel , httpServices , bufferChannel , workerPool , kPublisher , shutdown , signalChan )
4145}
4246
43- func shutDownServer (ctx context.Context , cancel context.CancelFunc , httpServices services.Services , bufferChannel chan collection.CollectRequest , workerPool * worker.Pool , kp * publisher.Kafka ) {
44- signalChan := make (chan os.Signal )
45- signal .Notify (signalChan , syscall .SIGHUP , syscall .SIGINT , syscall .SIGTERM , syscall .SIGQUIT )
47+ func shutDownServer (ctx context.Context , cancel context.CancelFunc , httpServices services.Services , bufferChannel chan collection.CollectRequest ,
48+ workerPool * worker.Pool , kp * publisher.Kafka , shutdown chan bool , signalChan chan os.Signal ) {
4649 for {
4750 sig := <- signalChan
4851 switch sig {
4952 case syscall .SIGHUP , syscall .SIGINT , syscall .SIGTERM , syscall .SIGQUIT :
5053 logger .Info (fmt .Sprintf ("[App.Server] Received a signal %s" , sig ))
5154 httpServices .Shutdown (ctx )
5255 logger .Info ("Server shutdown all the listeners" )
56+ cancel ()
57+ close (bufferChannel )
5358 timedOut := workerPool .FlushWithTimeOut (config .Worker .WorkerFlushTimeout )
5459 if timedOut {
5560 logger .Info (fmt .Sprintf ("WorkerPool flush timedout %t" , timedOut ))
61+ } else {
62+ logger .Info ("WorkerPool flushed all events" )
5663 }
5764 flushInterval := config .PublisherKafka .FlushInterval
5865 logger .Info ("Closing Kafka producer" )
5966 logger .Info (fmt .Sprintf ("Wait %d ms for all messages to be delivered" , flushInterval ))
6067 eventsInProducer := kp .Close ()
61- /**
62- @TODO - should compute the actual no., of events per batch and therefore the total. We can do this only when we close all the active connections
63- Until then we fall back to approximation */
64- eventsInChannel := len (bufferChannel ) * 7
65- logger .Info (fmt .Sprintf ("Outstanding unprocessed events in the channel, data lost ~ (No batches %d * 5 events) = ~%d" , len (bufferChannel ), eventsInChannel ))
66- metrics .Count ("kafka_messages_delivered_total" , eventsInChannel + eventsInProducer , "success=false" )
68+ eventCountInChannel := 0
69+ for i := 0 ; i < len (bufferChannel ); i ++ {
70+ req := <- bufferChannel
71+ eventCountInChannel += len (req .Events )
72+ }
73+ logger .Info (fmt .Sprintf ("number of events dropped during the shutdown %d" , eventCountInChannel + eventsInProducer ))
74+ metrics .Count ("total_data_loss" , eventCountInChannel + eventsInProducer , "reason=shutdown" )
6775 logger .Info ("Exiting server" )
68- cancel ()
76+ shutdown <- true
6977 default :
7078 logger .Info (fmt .Sprintf ("[App.Server] Received a unexpected signal %s" , sig ))
7179 }
80+ return
7281 }
7382}
7483
0 commit comments