From 90a0fa87ffeb50393444e0d68f3c7acebdad726a Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 8 Sep 2020 21:36:30 +0300 Subject: refactor --- cmd/dtail/main.go | 5 +++++ 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 { -- cgit v1.2.3