From 492cdf2053fc7a5a21fb37eaf07f1f109513ef85 Mon Sep 17 00:00:00 2001 From: ryan nemeth Date: Fri, 29 Mar 2024 15:41:35 -0400 Subject: [PATCH] fix bug with statistics being printed twice on sigterm --- cmd/httping/main.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/httping/main.go b/cmd/httping/main.go index e0940de..d701f06 100644 --- a/cmd/httping/main.go +++ b/cmd/httping/main.go @@ -118,9 +118,10 @@ func main() { func run(ctx context.Context, config config, writer io.Writer) error { var count int var respForStats []*httping.HttpResponse + var statsPrinted bool // Add this line to track if stats were printed defer func() { - if len(respForStats) > 0 { + if !statsPrinted && len(respForStats) > 0 { stats := httping.CalculateStatistics(respForStats) fmt.Printf("Total Requests: %d\n", count) fmt.Println(stats.String()) @@ -137,9 +138,12 @@ func run(ctx context.Context, config config, writer io.Writer) error { for i := 1; i <= config.count; i++ { select { case <-ctx.Done(): - stats := httping.CalculateStatistics(respForStats) - fmt.Printf("Total Requests: %d\n", count) - fmt.Println(stats.String()) + if !statsPrinted && len(respForStats) > 0 { + stats := httping.CalculateStatistics(respForStats) + fmt.Printf("Total Requests: %d\n", count) + fmt.Println(stats.String()) + statsPrinted = true + } return ctx.Err() default: response, err := httping.MakeRequest(config.useHTTP, config.useragent, config.url, config.headers)