From 88c13f9709bbb0033dddca21e181a5d62f7317e7 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sun, 23 Feb 2020 16:13:54 +0300 Subject: Remove data race Smart restart --- service/reload/service.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'service') diff --git a/service/reload/service.go b/service/reload/service.go index a8e862cb..d5b6a024 100644 --- a/service/reload/service.go +++ b/service/reload/service.go @@ -111,9 +111,6 @@ func (s *Service) Serve() error { serviceConfig ServiceConfig service string }{serviceConfig: s.cfg.Services[e.service], service: e.service} - - ticker.Stop() - ticker = time.NewTicker(s.cfg.Interval) } }() @@ -126,6 +123,14 @@ func (s *Service) Serve() error { case config := <-treshholdc: // replace previous value in map by more recent without adding new one updated[config.service] = config.serviceConfig + // stop ticker + ticker.Stop() + // restart + // logic is following: + // if we getting a lot of events, we should't restart particular service on each of it (user doing bug move or very fast typing) + // instead, we are resetting the ticker and wait for Interval time + // If there is no more events, we restart service only once + ticker = time.NewTicker(s.cfg.Interval) case <-ticker.C: if len(updated) > 0 { for k, v := range updated { -- cgit v1.2.3 From 8602c72a17f8ecabef2355d1d24ad569239f7bfa Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sun, 23 Feb 2020 16:24:45 +0300 Subject: Send message to the service stop channel --- service/reload/service.go | 1 + service/reload/watcher.go | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'service') diff --git a/service/reload/service.go b/service/reload/service.go index d5b6a024..7bfb0f28 100644 --- a/service/reload/service.go +++ b/service/reload/service.go @@ -161,4 +161,5 @@ func (s *Service) Serve() error { func (s *Service) Stop() { s.watcher.Stop() + s.stopc <- struct{}{} } diff --git a/service/reload/watcher.go b/service/reload/watcher.go index d5211d19..a3b8fe1f 100644 --- a/service/reload/watcher.go +++ b/service/reload/watcher.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "sync" "time" ) @@ -225,7 +226,9 @@ func (w *Watcher) waitEvent(d time.Duration) error { select { case <-w.close: ticker.Stop() - return nil + // just exit + // no matter for the pollEvents + runtime.Goexit() case <-ticker.C: // this is not very effective way // because we have to wait on Lock -- cgit v1.2.3