@@ -5,10 +5,10 @@ import (
55 "fmt"
66 "os"
77 "strconv"
8- "sync"
98 "time"
109
1110 log "github.com/sirupsen/logrus"
11+ "golang.org/x/sync/errgroup"
1212
1313 "github.com/crowdsecurity/go-cs-lib/trace"
1414
@@ -69,40 +69,25 @@ func initCrowdsec(ctx context.Context, cConfig *csconfig.Config, hub *cwhub.Hub,
6969 return csParsers , datasources , nil
7070}
7171
72- func startParserRoutines (cConfig * csconfig.Config , parsers * parser.Parsers ) {
73- // start go-routines for parsing, buckets pour and outputs.
74- parserWg := & sync.WaitGroup {}
75-
76- parsersTomb .Go (func () error {
77- parserWg .Add (1 )
78-
79- for range cConfig .Crowdsec .ParserRoutinesCount {
80- parsersTomb .Go (func () error {
81- defer trace .CatchPanic ("crowdsec/runParse" )
82-
83- if err := runParse (inputLineChan , inputEventChan , * parsers .Ctx , parsers .Nodes ); err != nil {
84- // this error will never happen as parser.Parse is not able to return errors
85- return err
86- }
87-
88- return nil
89- })
90- }
91-
92- parserWg .Done ()
93-
94- return nil
95- })
96- parserWg .Wait ()
72+ func startParserRoutines (ctx context.Context , g * errgroup.Group , cConfig * csconfig.Config , parsers * parser.Parsers ) {
73+ for idx := range cConfig .Crowdsec .ParserRoutinesCount {
74+ log .WithField ("idx" , idx ).Info ("Starting parser routine" )
75+ g .Go (func () error {
76+ defer trace .CatchPanic ("crowdsec/runParse/" + strconv .Itoa (idx ))
77+ runParse (ctx , inputLineChan , inputEventChan , * parsers .Ctx , parsers .Nodes )
78+ return nil
79+ })
80+ }
9781}
9882
99- func startBucketRoutines (ctx context.Context , cConfig * csconfig.Config ) {
83+ func startBucketRoutines (ctx context.Context , g * errgroup. Group , cConfig * csconfig.Config ) {
10084 for idx := range cConfig .Crowdsec .BucketsRoutinesCount {
101- log .Infof ( " Starting bucket routine %d" , idx )
102- go func () {
85+ log .WithField ( "idx" , idx ). Info ( " Starting bucket routine" )
86+ g . Go ( func () error {
10387 defer trace .CatchPanic ("crowdsec/runPour/" + strconv .Itoa (idx ))
10488 runPour (ctx , inputEventChan , holders , buckets , cConfig )
105- }()
89+ return nil
90+ })
10691 }
10792}
10893
@@ -112,24 +97,13 @@ func startHeartBeat(ctx context.Context, _ *csconfig.Config, apiClient *apiclien
11297}
11398
11499func startOutputRoutines (ctx context.Context , cConfig * csconfig.Config , parsers * parser.Parsers , apiClient * apiclient.ApiClient ) {
115- outputWg := & sync.WaitGroup {}
116-
117- outputsTomb .Go (func () error {
118- outputWg .Add (1 )
119-
120- for range cConfig .Crowdsec .OutputRoutinesCount {
121- outputsTomb .Go (func () error {
122- defer trace .CatchPanic ("crowdsec/runOutput" )
123-
124- return runOutput (ctx , inputEventChan , outputEventChan , buckets , * parsers .PovfwCtx , parsers .Povfwnodes , apiClient )
125- })
126- }
127-
128- outputWg .Done ()
129-
130- return nil
131- })
132- outputWg .Wait ()
100+ for idx := range cConfig .Crowdsec .OutputRoutinesCount {
101+ log .WithField ("idx" , idx ).Info ("Starting output routine" )
102+ outputsTomb .Go (func () error {
103+ defer trace .CatchPanic ("crowdsec/runOutput/" + strconv .Itoa (idx ))
104+ return runOutput (ctx , inputEventChan , outputEventChan , buckets , * parsers .PovfwCtx , parsers .Povfwnodes , apiClient )
105+ })
106+ }
133107}
134108
135109func startLPMetrics (ctx context.Context , cConfig * csconfig.Config , apiClient * apiclient.ApiClient , hub * cwhub.Hub , datasources []acquisition.DataSource ) error {
@@ -142,9 +116,9 @@ func startLPMetrics(ctx context.Context, cConfig *csconfig.Config, apiClient *ap
142116 hub ,
143117 )
144118
145- lpMetricsTomb . Go ( func () error {
146- return mp .Run (ctx , & lpMetricsTomb )
147- })
119+ go func () {
120+ mp .Run (ctx )
121+ }( )
148122
149123 if cConfig .Prometheus != nil && cConfig .Prometheus .Enabled {
150124 aggregated := false
@@ -161,13 +135,12 @@ func startLPMetrics(ctx context.Context, cConfig *csconfig.Config, apiClient *ap
161135}
162136
163137// runCrowdsec starts the log processor service
164- func runCrowdsec (ctx context.Context , cConfig * csconfig.Config , parsers * parser.Parsers , hub * cwhub.Hub , datasources []acquisition.DataSource ) error {
138+ func runCrowdsec (ctx context.Context , g * errgroup. Group , cConfig * csconfig.Config , parsers * parser.Parsers , hub * cwhub.Hub , datasources []acquisition.DataSource ) error {
165139 inputEventChan = make (chan pipeline.Event )
166140 inputLineChan = make (chan pipeline.Event )
167141
168- startParserRoutines (cConfig , parsers )
169-
170- startBucketRoutines (ctx , cConfig )
142+ startParserRoutines (ctx , g , cConfig , parsers )
143+ startBucketRoutines (ctx , g , cConfig )
171144
172145 apiClient , err := apiclient .GetLAPIClient ()
173146 if err != nil {
@@ -195,6 +168,7 @@ func runCrowdsec(ctx context.Context, cConfig *csconfig.Config, parsers *parser.
195168func serveCrowdsec (ctx context.Context , parsers * parser.Parsers , cConfig * csconfig.Config , hub * cwhub.Hub , datasources []acquisition.DataSource , agentReady chan bool ) {
196169 cctx , cancel := context .WithCancel (ctx )
197170
171+ var g errgroup.Group
198172
199173 crowdsecTomb .Go (func () error {
200174 defer trace .CatchPanic ("crowdsec/serveCrowdsec" )
@@ -206,7 +180,7 @@ func serveCrowdsec(ctx context.Context, parsers *parser.Parsers, cConfig *csconf
206180
207181 agentReady <- true
208182
209- if err := runCrowdsec (cctx , cConfig , parsers , hub , datasources ); err != nil {
183+ if err := runCrowdsec (cctx , & g , cConfig , parsers , hub , datasources ); err != nil {
210184 log .Fatalf ("unable to start crowdsec routines: %s" , err )
211185 }
212186 }()
@@ -218,7 +192,7 @@ func serveCrowdsec(ctx context.Context, parsers *parser.Parsers, cConfig *csconf
218192 waitOnTomb ()
219193 log .Debugf ("Shutting down crowdsec routines" )
220194
221- if err := ShutdownCrowdsecRoutines (cancel ); err != nil {
195+ if err := ShutdownCrowdsecRoutines (cancel , & g ); err != nil {
222196 return fmt .Errorf ("unable to shutdown crowdsec routines: %w" , err )
223197 }
224198
0 commit comments