Explorar o código

serialize lapi and lp startup

this should reduce leaky tests (esp. with external DBs) because lp is now waiting for lapi to be ready before initialization
marco hai 1 ano
pai
achega
465f0c64db

+ 3 - 1
cmd/crowdsec/api.go

@@ -56,7 +56,8 @@ func initAPIServer(cConfig *csconfig.Config) (*apiserver.APIServer, error) {
 	return apiServer, nil
 }
 
-func serveAPIServer(apiServer *apiserver.APIServer, apiReady chan bool) {
+func serveAPIServer(apiServer *apiserver.APIServer) {
+	apiReady := make(chan bool, 1)
 	apiTomb.Go(func() error {
 		defer trace.CatchPanic("crowdsec/serveAPIServer")
 		go func() {
@@ -80,6 +81,7 @@ func serveAPIServer(apiServer *apiserver.APIServer, apiReady chan bool) {
 		}
 		return nil
 	})
+	<-apiReady
 }
 
 func hasPlugins(profiles []*csconfig.ProfileCfg) bool {

+ 1 - 2
cmd/crowdsec/crowdsec.go

@@ -139,8 +139,7 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H
 	log.Info("Starting processing data")
 
 	if err := acquisition.StartAcquisition(dataSources, inputLineChan, &acquisTomb); err != nil {
-		log.Fatalf("starting acquisition error : %s", err)
-		return err
+		return fmt.Errorf("starting acquisition error: %w", err)
 	}
 
 	return nil

+ 3 - 3
cmd/crowdsec/metrics.go

@@ -177,7 +177,9 @@ func registerPrometheus(config *csconfig.PrometheusCfg) {
 	}
 }
 
-func servePrometheus(config *csconfig.PrometheusCfg, dbClient *database.Client, apiReady chan bool, agentReady chan bool) {
+func servePrometheus(config *csconfig.PrometheusCfg, dbClient *database.Client, agentReady chan bool) {
+	<-agentReady
+
 	if !config.Enabled {
 		return
 	}
@@ -185,8 +187,6 @@ func servePrometheus(config *csconfig.PrometheusCfg, dbClient *database.Client,
 	defer trace.CatchPanic("crowdsec/servePrometheus")
 
 	http.Handle("/metrics", computeDynamicMetrics(promhttp.Handler(), dbClient))
-	<-apiReady
-	<-agentReady
 	log.Debugf("serving metrics after %s ms", time.Since(crowdsecT0))
 	if err := http.ListenAndServe(fmt.Sprintf("%s:%d", config.ListenAddr, config.ListenPort), nil); err != nil {
 		log.Warningf("prometheus: %s", err)

+ 3 - 4
cmd/crowdsec/run_in_svc.go

@@ -33,7 +33,6 @@ func StartRunSvc() error {
 
 	log.Infof("Crowdsec %s", version.String())
 
-	apiReady := make(chan bool, 1)
 	agentReady := make(chan bool, 1)
 
 	// Enable profiling early
@@ -52,8 +51,8 @@ func StartRunSvc() error {
 
 		registerPrometheus(cConfig.Prometheus)
 
-		go servePrometheus(cConfig.Prometheus, dbClient, apiReady, agentReady)
-	}
+		go servePrometheus(cConfig.Prometheus, dbClient, agentReady)
+	} // XXX: avoid leaking agentReady
 
-	return Serve(cConfig, apiReady, agentReady)
+	return Serve(cConfig, agentReady)
 }

+ 2 - 3
cmd/crowdsec/run_in_svc_windows.go

@@ -73,7 +73,6 @@ func WindowsRun() error {
 
 	log.Infof("Crowdsec %s", version.String())
 
-	apiReady := make(chan bool, 1)
 	agentReady := make(chan bool, 1)
 
 	// Enable profiling early
@@ -89,7 +88,7 @@ func WindowsRun() error {
 			}
 		}
 		registerPrometheus(cConfig.Prometheus)
-		go servePrometheus(cConfig.Prometheus, dbClient, apiReady, agentReady)
+		go servePrometheus(cConfig.Prometheus, dbClient, agentReady)
 	}
-	return Serve(cConfig, apiReady, agentReady)
+	return Serve(cConfig, agentReady)
 }

+ 5 - 6
cmd/crowdsec/serve.go

@@ -73,8 +73,7 @@ func reloadHandler(sig os.Signal) (*csconfig.Config, error) {
 			return nil, fmt.Errorf("unable to init api server: %w", err)
 		}
 
-		apiReady := make(chan bool, 1)
-		serveAPIServer(apiServer, apiReady)
+		serveAPIServer(apiServer)
 	}
 
 	if !cConfig.DisableAgent {
@@ -295,7 +294,7 @@ func HandleSignals(cConfig *csconfig.Config) error {
 	return err
 }
 
-func Serve(cConfig *csconfig.Config, apiReady chan bool, agentReady chan bool) error {
+func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
 	acquisTomb = tomb.Tomb{}
 	parsersTomb = tomb.Tomb{}
 	bucketsTomb = tomb.Tomb{}
@@ -346,10 +345,8 @@ func Serve(cConfig *csconfig.Config, apiReady chan bool, agentReady chan bool) e
 		}
 
 		if !flags.TestMode {
-			serveAPIServer(apiServer, apiReady)
+			serveAPIServer(apiServer)
 		}
-	} else {
-		apiReady <- true
 	}
 
 	if !cConfig.DisableAgent {
@@ -366,6 +363,8 @@ func Serve(cConfig *csconfig.Config, apiReady chan bool, agentReady chan bool) e
 		// if it's just linting, we're done
 		if !flags.TestMode {
 			serveCrowdsec(csParsers, cConfig, hub, agentReady)
+		} else {
+			agentReady <- true
 		}
 	} else {
 		agentReady <- true