premature abort (#81)

This commit is contained in:
Michael Quigley 2022-11-14 13:52:49 -05:00
parent 65fcf0f620
commit a6188ac962
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -20,6 +20,9 @@ import (
"io" "io"
"math/rand" "math/rand"
"net/http" "net/http"
"os"
"os/signal"
"syscall"
"time" "time"
) )
@ -69,6 +72,14 @@ func (r *loopCmd) run(_ *cobra.Command, _ []string) {
loopers = append(loopers, l) loopers = append(loopers, l)
go l.run() go l.run()
} }
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
for _, looper := range loopers {
looper.stop = true
}
}()
for _, l := range loopers { for _, l := range loopers {
<-l.done <-l.done
} }
@ -84,6 +95,7 @@ func (r *loopCmd) run(_ *cobra.Command, _ []string) {
} }
totalXferSec := util.BytesToSize(totalXfer) totalXferSec := util.BytesToSize(totalXfer)
logrus.Infof("total: %d mismatches, %s/sec", totalMismatches, totalXferSec) logrus.Infof("total: %d mismatches, %s/sec", totalMismatches, totalXferSec)
os.Exit(0)
} }
type looper struct { type looper struct {
@ -101,6 +113,7 @@ type looper struct {
bytes int64 bytes int64
startTime time.Time startTime time.Time
stopTime time.Time stopTime time.Time
stop bool
} }
func newLooper(id int, cmd *loopCmd) *looper { func newLooper(id int, cmd *loopCmd) *looper {
@ -193,7 +206,7 @@ func (l *looper) iterate() {
l.startTime = time.Now() l.startTime = time.Now()
defer func() { l.stopTime = time.Now() }() defer func() { l.stopTime = time.Now() }()
for i := 0; i < l.cmd.iterations; i++ { for i := 0; i < l.cmd.iterations && !l.stop; i++ {
if i > 0 && i%l.cmd.statusEvery == 0 { if i > 0 && i%l.cmd.statusEvery == 0 {
logrus.Infof("looper #%d: iteration #%d", l.id, i) logrus.Infof("looper #%d: iteration #%d", l.id, i)
} }