diff options
| -rw-r--r-- | cmd/dcat/main.go | 2 | ||||
| -rw-r--r-- | cmd/dgrep/main.go | 2 | ||||
| -rw-r--r-- | cmd/dmap/main.go | 2 | ||||
| -rw-r--r-- | cmd/drun/main.go | 2 | ||||
| -rw-r--r-- | cmd/dtail/main.go | 2 | ||||
| -rw-r--r-- | internal/clients/stats.go | 17 | ||||
| -rw-r--r-- | internal/io/signal/signal.go | 14 |
7 files changed, 26 insertions, 15 deletions
diff --git a/cmd/dcat/main.go b/cmd/dcat/main.go index f0ea946..05e46ab 100644 --- a/cmd/dcat/main.go +++ b/cmd/dcat/main.go @@ -55,7 +55,7 @@ func main() { panic(err) } - status := client.Start(ctx, signal.StatsCh(ctx)) + status := client.Start(ctx, signal.InterruptCh(ctx)) logger.Flush() os.Exit(status) } diff --git a/cmd/dgrep/main.go b/cmd/dgrep/main.go index d1fdc21..133631f 100644 --- a/cmd/dgrep/main.go +++ b/cmd/dgrep/main.go @@ -63,7 +63,7 @@ func main() { panic(err) } - status := client.Start(ctx, signal.StatsCh(ctx)) + status := client.Start(ctx, signal.InterruptCh(ctx)) logger.Flush() os.Exit(status) } diff --git a/cmd/dmap/main.go b/cmd/dmap/main.go index 279b343..9f9ca9d 100644 --- a/cmd/dmap/main.go +++ b/cmd/dmap/main.go @@ -62,7 +62,7 @@ func main() { panic(err) } - status := client.Start(ctx, signal.StatsCh(ctx)) + status := client.Start(ctx, signal.InterruptCh(ctx)) logger.Flush() os.Exit(status) } diff --git a/cmd/drun/main.go b/cmd/drun/main.go index ffdf7bf..3916817 100644 --- a/cmd/drun/main.go +++ b/cmd/drun/main.go @@ -64,7 +64,7 @@ func main() { panic(err) } - status := client.Start(ctx, signal.StatsCh(ctx)) + status := client.Start(ctx, signal.InterruptCh(ctx)) logger.Flush() os.Exit(status) } diff --git a/cmd/dtail/main.go b/cmd/dtail/main.go index ff9028b..aefaa6a 100644 --- a/cmd/dtail/main.go +++ b/cmd/dtail/main.go @@ -106,7 +106,7 @@ func main() { } } - status := client.Start(ctx, signal.StatsCh(ctx)) + status := client.Start(ctx, signal.InterruptCh(ctx)) logger.Flush() os.Exit(status) } 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. } |
