diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2020-09-11 12:30:29 +0100 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2020-09-11 12:30:29 +0100 |
| commit | ec67d9833095dfbe620dd3c99ea0caba391c4b87 (patch) | |
| tree | bb1e9735b84bb2c7ee6222dbcc1f20d4a896d0ce /internal | |
| parent | d402cf103b00b8a55c6cab665ab36d4fd282f3a5 (diff) | |
hit ctrl+c twice to exit
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/clients/stats.go | 17 | ||||
| -rw-r--r-- | internal/io/signal/signal.go | 14 |
2 files changed, 21 insertions, 10 deletions
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. } |
