From 74ace58ae1c42f9a2cd775fd900fec7064667b74 Mon Sep 17 00:00:00 2001 From: NicoMen Date: Thu, 6 Sep 2018 14:24:03 +0200 Subject: [PATCH] Avoid goroutine leak in server --- server/server.go | 4 +++- server/server_configuration.go | 6 ++++-- server/server_signals.go | 26 +++++++++++++++----------- server/server_signals_windows.go | 2 +- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/server/server.go b/server/server.go index 53583281a..4415223b7 100644 --- a/server/server.go +++ b/server/server.go @@ -248,7 +248,9 @@ func (s *Server) Start() { s.listenConfigurations(stop) }) s.startProvider() - go s.listenSignals() + s.routinesPool.Go(func(stop chan bool) { + s.listenSignals(stop) + }) } // StartWithContext starts the server and Stop/Close it when context is Done diff --git a/server/server_configuration.go b/server/server_configuration.go index 41ef7ab2c..773eb8be0 100644 --- a/server/server_configuration.go +++ b/server/server_configuration.go @@ -447,8 +447,10 @@ func (s *Server) throttleProviderConfigReload(throttle time.Duration, publish ch case <-stop: return case nextConfig := <-ring.Out(): - publish <- nextConfig.(types.ConfigMessage) - time.Sleep(throttle) + if config, ok := nextConfig.(types.ConfigMessage); ok { + publish <- config + time.Sleep(throttle) + } } } }) diff --git a/server/server_signals.go b/server/server_signals.go index fb8514c9a..8472696d7 100644 --- a/server/server_signals.go +++ b/server/server_signals.go @@ -13,21 +13,25 @@ func (s *Server) configureSignals() { signal.Notify(s.signals, syscall.SIGUSR1) } -func (s *Server) listenSignals() { +func (s *Server) listenSignals(stop chan bool) { for { - sig := <-s.signals - switch sig { - case syscall.SIGUSR1: - log.Infof("Closing and re-opening log files for rotation: %+v", sig) + select { + case <-stop: + return + case sig := <-s.signals: + switch sig { + case syscall.SIGUSR1: + log.Infof("Closing and re-opening log files for rotation: %+v", sig) - if s.accessLoggerMiddleware != nil { - if err := s.accessLoggerMiddleware.Rotate(); err != nil { - log.Errorf("Error rotating access log: %v", err) + if s.accessLoggerMiddleware != nil { + if err := s.accessLoggerMiddleware.Rotate(); err != nil { + log.Errorf("Error rotating access log: %v", err) + } } - } - if err := log.RotateFile(); err != nil { - log.Errorf("Error rotating traefik log: %v", err) + if err := log.RotateFile(); err != nil { + log.Errorf("Error rotating traefik log: %v", err) + } } } } diff --git a/server/server_signals_windows.go b/server/server_signals_windows.go index 674896b43..05cf4eace 100644 --- a/server/server_signals_windows.go +++ b/server/server_signals_windows.go @@ -4,4 +4,4 @@ package server func (s *Server) configureSignals() {} -func (s *Server) listenSignals() {} +func (s *Server) listenSignals(stop chan bool) {}