From 483484e2e2be6fd93edcfee455a737b61bacb2a1 Mon Sep 17 00:00:00 2001 From: "Daniel.Sy" Date: Fri, 28 Mar 2025 02:56:49 +0000 Subject: [PATCH] fix(#1): :ambulance: Fix application stopping early (#4) Co-authored-by: Daniel Sy Reviewed-on: https://forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Daniel.Sy/loic-go/pulls/4 --- cmd/web.go | 4 ++-- pkg/loic/loic.go | 8 ++++++-- pkg/web/web.go | 8 ++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 21b8d92..677fe0e 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -7,8 +7,8 @@ import ( var webCmd = &cobra.Command{ Use: "web", - Short: "Startet den Web-Server", - Long: "Startet den Web-Server für myapp", + Short: "starts the web server", + Long: "starts the web UI for LOIC", Run: func(cmd *cobra.Command, args []string) { web.Start() }, diff --git a/pkg/loic/loic.go b/pkg/loic/loic.go index 48142e5..0fd9483 100644 --- a/pkg/loic/loic.go +++ b/pkg/loic/loic.go @@ -16,6 +16,7 @@ var requestsSent metric.Int64Counter var currentTest *Test var isRunning bool var TestDone = make(chan struct{}) +var closeOnce sync.Once func init() { var err error @@ -47,7 +48,7 @@ func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp } log.Printf("Starting test with target: %s, concurrency: %d, duration: %v, rampUp: %v", targetURL, concurrency, duration, rampUp) ctx, cancel := context.WithCancel(context.Background()) - currentTest := &Test{ + currentTest = &Test{ TargetURL: targetURL, Concurrency: concurrency, Duration: duration, @@ -56,14 +57,16 @@ func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp cancel: cancel, } isRunning = true + closeOnce = sync.Once{} // Reset closeOnce for the new test log.Println("isRunning set to true, launching goroutine") go func() { currentTest.run() - close(TestDone) + closeOnce.Do(func() { close(TestDone) }) }() } func StopTest() { + log.Printf("StopTest called: currentTest=%v, isRunning=%v", currentTest, isRunning) if currentTest != nil && isRunning { log.Println("Stopping test") currentTest.cancel() @@ -71,6 +74,7 @@ func StopTest() { currentTest = nil isRunning = false log.Println("Test stopped, isRunning set to false") + closeOnce.Do(func() { close(TestDone) }) } else { log.Println("No test running to stop") } diff --git a/pkg/web/web.go b/pkg/web/web.go index 88b1296..ad38b26 100644 --- a/pkg/web/web.go +++ b/pkg/web/web.go @@ -23,6 +23,10 @@ func Start() { }) r.POST("/start", func(c *gin.Context) { + if c.Request.Method != http.MethodPost { + http.Error(c.Writer, "Invalid request method", http.StatusMethodNotAllowed) + return + } targetURL := c.PostForm("target_url") concurrency, _ := strconv.Atoi(c.PostForm("concurrency")) duration, _ := time.ParseDuration(c.PostForm("duration")) @@ -33,6 +37,10 @@ func Start() { }) r.POST("/stop", func(c *gin.Context) { + if c.Request.Method != http.MethodPost { + http.Error(c.Writer, "Invalid request method", http.StatusMethodNotAllowed) + return + } loic.StopTest() c.Redirect(http.StatusSeeOther, "/") })