diff options
| author | Paul Bütow <pbuetow@mimecast.com> | 2020-01-20 18:41:05 +0000 |
|---|---|---|
| committer | Paul Bütow <pbuetow@mimecast.com> | 2020-01-21 14:35:23 +0000 |
| commit | c128865c4c7411c29a59fca9a3a2f95537686d7b (patch) | |
| tree | 193bccc70d942c8b70cc93fae2670263701e43aa /internal/server/stats.go | |
| parent | 3755a9911ecb05886577095f2b8cc8b9e4066a3a (diff) | |
Move commands to cmd/ and move internal dependencies to internal/
Diffstat (limited to 'internal/server/stats.go')
| -rw-r--r-- | internal/server/stats.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/internal/server/stats.go b/internal/server/stats.go new file mode 100644 index 0000000..beb1885 --- /dev/null +++ b/internal/server/stats.go @@ -0,0 +1,88 @@ +package server + +import ( + "github.com/mimecast/dtail/internal/config" + "github.com/mimecast/dtail/internal/logger" + "fmt" + "runtime" + "sync" + "time" +) + +// Used to collect and display various server stats. +type stats struct { + mutex sync.Mutex + currentConnections int + lifetimeConnections uint64 +} + +func (s *stats) incrementConnections() { + defer s.logServerStats() + + s.mutex.Lock() + s.currentConnections++ + s.lifetimeConnections++ + s.mutex.Unlock() +} + +func (s *stats) decrementConnections() { + defer s.logServerStats() + + s.mutex.Lock() + s.currentConnections-- + s.mutex.Unlock() +} + +func (s *stats) hasConnections() bool { + s.mutex.Lock() + currentConnections := s.currentConnections + s.mutex.Unlock() + + has := currentConnections > 0 + logger.Info("stats", "Server with open connections?", has, currentConnections) + + return has +} + +func (s *stats) logServerStats() { + s.mutex.Lock() + defer s.mutex.Unlock() + + currentConnections := fmt.Sprintf("currentConnections=%d", s.currentConnections) + lifetimeConnections := fmt.Sprintf("lifetimeConnections=%d", s.lifetimeConnections) + goroutines := fmt.Sprintf("goroutines=%d", runtime.NumGoroutine()) + logger.Info("stats", currentConnections, lifetimeConnections, goroutines) +} + +func (s *stats) serverLimitExceeded() error { + s.mutex.Lock() + defer s.mutex.Unlock() + + if s.currentConnections >= config.Server.MaxConnections { + return fmt.Errorf("Exceeded max allowed concurrent connections of %d", config.Server.MaxConnections) + } + + return nil +} + +func (s *stats) periodicLogServerStats(stop <-chan struct{}) { + for { + select { + case <-time.NewTimer(time.Second * 10).C: + s.logServerStats() + case <-stop: + return + } + } +} + +func (s *stats) waitForConnections() { + for { + select { + case <-time.NewTimer(time.Second).C: + if !s.hasConnections() { + return + } + } + } +} |
