From 01c0f269ee8101ca22936bbbe1497f7b656a42b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Thu, 19 Aug 2021 21:19:02 +0200 Subject: [PATCH] use defer for ticker. --- internal/capture/screencast.go | 5 +++-- internal/desktop/manager.go | 12 ++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/internal/capture/screencast.go b/internal/capture/screencast.go index df788899..28bb1e74 100644 --- a/internal/capture/screencast.go +++ b/internal/capture/screencast.go @@ -40,14 +40,15 @@ func screencastNew(enabled bool, pipelineStr string) *ScreencastManagerCtx { } go func() { - ticker := time.NewTicker(screencastTimeout) manager.logger.Debug().Msg("started emitting samples") + ticker := time.NewTicker(screencastTimeout) + defer ticker.Stop() + for { select { case <-manager.emitStop: manager.logger.Debug().Msg("stopped emitting samples") - ticker.Stop() return case <-manager.emitUpdate: manager.logger.Debug().Msg("update emitting samples") diff --git a/internal/desktop/manager.go b/internal/desktop/manager.go index cef9b6ab..788e3d65 100644 --- a/internal/desktop/manager.go +++ b/internal/desktop/manager.go @@ -18,7 +18,6 @@ var mu = sync.Mutex{} type DesktopManagerCtx struct { logger zerolog.Logger - cleanup *time.Ticker shutdown chan bool emmiter events.EventEmmiter config *config.Desktop @@ -27,7 +26,6 @@ type DesktopManagerCtx struct { func New(config *config.Desktop) *DesktopManagerCtx { return &DesktopManagerCtx{ logger: log.With().Str("module", "desktop").Logger(), - cleanup: time.NewTicker(1 * time.Second), shutdown: make(chan bool), emmiter: events.New(), config: config, @@ -64,16 +62,15 @@ func (manager *DesktopManagerCtx) Start() { }) go func() { - defer func() { - xorg.DisplayClose() - manager.logger.Info().Msg("shutdown") - }() + ticker := time.NewTicker(1 * time.Second) + defer ticker.Stop() for { select { case <-manager.shutdown: + xorg.DisplayClose() return - case <-manager.cleanup.C: + case <-ticker.C: xorg.CheckKeys(time.Second * 10) } } @@ -95,7 +92,6 @@ func (manager *DesktopManagerCtx) OnAfterScreenSizeChange(listener func()) { func (manager *DesktopManagerCtx) Shutdown() error { manager.logger.Info().Msgf("desktop shutting down") - manager.cleanup.Stop() manager.shutdown <- true return nil }