From 79d67c4a09c3dc4f458a21ebd12ea34fd871b506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 23 Jan 2021 16:29:47 +0100 Subject: [PATCH] screencast atomic expired. --- internal/capture/screencast.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/internal/capture/screencast.go b/internal/capture/screencast.go index f5589710..f08a05ab 100644 --- a/internal/capture/screencast.go +++ b/internal/capture/screencast.go @@ -4,6 +4,7 @@ import ( "fmt" "time" "sync" + "sync/atomic" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -22,8 +23,7 @@ type ScreencastManagerCtx struct { started bool shutdown chan bool refresh chan bool - expires time.Time - timeout time.Duration + expired int32 sample chan types.Sample image types.Sample } @@ -36,7 +36,6 @@ func screencastNew(config *config.Capture) *ScreencastManagerCtx { started: false, shutdown: make(chan bool), refresh: make(chan bool), - timeout: 10 * time.Second, } if !manager.enabled { @@ -44,7 +43,7 @@ func screencastNew(config *config.Capture) *ScreencastManagerCtx { } go func() { - ticker := time.NewTicker(1 * time.Second) + ticker := time.NewTicker(5 * time.Second) manager.logger.Debug().Msg("subroutine started") for { @@ -58,7 +57,7 @@ func screencastNew(config *config.Capture) *ScreencastManagerCtx { case sample := <-manager.sample: manager.image = sample case <-ticker.C: - if manager.started && time.Now().After(manager.expires) { + if manager.started && !atomic.CompareAndSwapInt32(&manager.expired, 0, 1) { manager.stop() } } @@ -77,7 +76,7 @@ func (manager *ScreencastManagerCtx) Started() bool { } func (manager *ScreencastManagerCtx) Image() ([]byte, error) { - manager.expires = time.Now().Add(manager.timeout) + atomic.StoreInt32(&manager.expired, 0) if !manager.started { err := manager.start()