Skip to content

Commit

Permalink
fix bug with statistics being printed twice on sigterm
Browse files Browse the repository at this point in the history
  • Loading branch information
rnemeth90 committed Mar 29, 2024
1 parent 14681d2 commit 492cdf2
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/httping/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand Down

0 comments on commit 492cdf2

Please sign in to comment.