diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2020-09-08 21:36:30 +0300 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2020-09-08 21:36:30 +0300 |
| commit | 40cbef0c243042521bdf589b3c4549ff32508592 (patch) | |
| tree | 1a996d70d02bcb425edd48b8f3c2f32af8f6b303 | |
| parent | 3a7b359055f4277d3a07b3b7bf8580ef575c54a8 (diff) | |
refactor
| -rw-r--r-- | cmd/dtail/main.go | 5 | ||||
| -rw-r--r-- | internal/clients/stats.go | 36 |
2 files changed, 21 insertions, 20 deletions
diff --git a/cmd/dtail/main.go b/cmd/dtail/main.go index 3d0e0f6..1be2a29 100644 --- a/cmd/dtail/main.go +++ b/cmd/dtail/main.go @@ -105,6 +105,11 @@ func main() { } } + /* + sigCh := make(chan os.Signal) + signal.Notify(sigCh, os.Interrupt, syscall.SIGINFO) + */ + status := client.Start(ctx) logger.Flush() os.Exit(status) diff --git a/internal/clients/stats.go b/internal/clients/stats.go index ec6adfe..481d157 100644 --- a/internal/clients/stats.go +++ b/internal/clients/stats.go @@ -5,7 +5,6 @@ import ( "fmt" "runtime" "sync" - "time" "github.com/mimecast/dtail/internal/io/logger" ) @@ -30,13 +29,12 @@ func newTailStats(connectionsTotal int) *stats { } } -func (s *stats) periodicLogStats(ctx context.Context, throttleCh chan struct{}) { - connectedLast := 0 - statsInterval := 5 +func (s *stats) logStatsOnSignal(ctx context.Context, throttleCh chan struct{}, sigCh chan struct{}) { + var connectedLast int for { select { - case <-time.After(time.Second * time.Duration(statsInterval)): + case <-sigCh: case <-ctx.Done(): return } @@ -45,34 +43,32 @@ func (s *stats) periodicLogStats(ctx context.Context, throttleCh chan struct{}) throttle := len(throttleCh) newConnections := connected - connectedLast - connectionsPerSecond := float64(newConnections) / float64(statsInterval) - s.log(connected, newConnections, connectionsPerSecond, throttle) - - connectedLast = connected + s.log(connected, newConnections, throttle) s.mutex.Lock() + defer s.mutex.Unlock() + + connectedLast = connected s.connected = connected - s.mutex.Unlock() } } -func (s *stats) numConnected() int { - s.mutex.Lock() - defer s.mutex.Unlock() - - return s.connected -} - -func (s *stats) log(connected, newConnections int, connectionsPerSecond float64, throttle int) { +func (s *stats) log(connected, newConnections int, throttle int) { 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) - rateStr := fmt.Sprintf("rate=%2.2f/s", connectionsPerSecond) throttleStr := fmt.Sprintf("throttle=%d", throttle) cpusGoroutinesStr := fmt.Sprintf("cpus/goroutines=%d/%d", runtime.NumCPU(), runtime.NumGoroutine()) - logger.Info("stats", connectedStr, newConnStr, rateStr, throttleStr, cpusGoroutinesStr) + logger.Info("stats", connectedStr, newConnStr, throttleStr, cpusGoroutinesStr) +} + +func (s *stats) numConnected() int { + s.mutex.Lock() + defer s.mutex.Unlock() + + return s.connected } func percentOf(total float64, value float64) float64 { |
