From ec67d9833095dfbe620dd3c99ea0caba391c4b87 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 11 Sep 2020 12:30:29 +0100 Subject: hit ctrl+c twice to exit --- internal/clients/stats.go | 17 ++++++++++------- internal/io/signal/signal.go | 14 +++++++++++--- 2 files changed, 21 insertions(+), 10 deletions(-) (limited to 'internal') diff --git a/internal/clients/stats.go b/internal/clients/stats.go index a6ac0c5..e7eabd8 100644 --- a/internal/clients/stats.go +++ b/internal/clients/stats.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "runtime" + "strings" "sync" "time" @@ -54,7 +55,8 @@ func (s *stats) Start(ctx context.Context, throttleCh, statsCh <-chan struct{}) if connected == connectedLast && !force { continue } - s.log(connected, newConnections, throttle) + + logger.Info(s.statsLine(connected, newConnections, throttle)) connectedLast = connected s.mutex.Lock() @@ -63,15 +65,16 @@ func (s *stats) Start(ctx context.Context, throttleCh, statsCh <-chan struct{}) } } -func (s *stats) log(connected, newConnections int, throttle int) { +func (s *stats) statsLine(connected, newConnections int, throttle int) string { percConnected := percentOf(float64(s.connectionsTotal), float64(connected)) - connectedStr := fmt.Sprintf("connected=%d/%d(%d%%)", connected, s.connectionsTotal, int(percConnected)) - newConnStr := fmt.Sprintf("new=%d", newConnections) - throttleStr := fmt.Sprintf("throttle=%d", throttle) - cpusGoroutinesStr := fmt.Sprintf("cpus/goroutines=%d/%d", runtime.NumCPU(), runtime.NumGoroutine()) + var stats []string + stats = append(stats, fmt.Sprintf("connected=%d/%d(%d%%)", connected, s.connectionsTotal, int(percConnected))) + stats = append(stats, fmt.Sprintf("new=%d", newConnections)) + stats = append(stats, fmt.Sprintf("throttle=%d", throttle)) + stats = append(stats, fmt.Sprintf("cpus/goroutines=%d/%d", runtime.NumCPU(), runtime.NumGoroutine())) - logger.Info("stats", connectedStr, newConnStr, throttleStr, cpusGoroutinesStr) + return strings.Join(stats, "|") } func (s *stats) numConnected() int { diff --git a/internal/io/signal/signal.go b/internal/io/signal/signal.go index 0d49485..3785d71 100644 --- a/internal/io/signal/signal.go +++ b/internal/io/signal/signal.go @@ -4,13 +4,15 @@ import ( "context" "os" gosignal "os/signal" - "syscall" + "time" + + "github.com/mimecast/dtail/internal/io/logger" ) // StatsCh returns a channel for "please print stats" signalling. -func StatsCh(ctx context.Context) <-chan struct{} { +func InterruptCh(ctx context.Context) <-chan struct{} { sigCh := make(chan os.Signal) - gosignal.Notify(sigCh, syscall.SIGUSR1) + gosignal.Notify(sigCh, os.Interrupt) statsCh := make(chan struct{}) @@ -20,6 +22,12 @@ func StatsCh(ctx context.Context) <-chan struct{} { case <-sigCh: select { case statsCh <- struct{}{}: + logger.Info("Hit Ctrl+C twice to exit") + select { + case <-sigCh: + os.Exit(0) + case <-time.After(time.Second): + } default: // Stats currently already printed. } -- cgit v1.2.3