screencast atomic expired.

This commit is contained in:
Miroslav Šedivý 2021-01-23 16:29:47 +01:00
parent 1ffbe337d4
commit 79d67c4a09

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"time" "time"
"sync" "sync"
"sync/atomic"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -22,8 +23,7 @@ type ScreencastManagerCtx struct {
started bool started bool
shutdown chan bool shutdown chan bool
refresh chan bool refresh chan bool
expires time.Time expired int32
timeout time.Duration
sample chan types.Sample sample chan types.Sample
image types.Sample image types.Sample
} }
@ -36,7 +36,6 @@ func screencastNew(config *config.Capture) *ScreencastManagerCtx {
started: false, started: false,
shutdown: make(chan bool), shutdown: make(chan bool),
refresh: make(chan bool), refresh: make(chan bool),
timeout: 10 * time.Second,
} }
if !manager.enabled { if !manager.enabled {
@ -44,7 +43,7 @@ func screencastNew(config *config.Capture) *ScreencastManagerCtx {
} }
go func() { go func() {
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(5 * time.Second)
manager.logger.Debug().Msg("subroutine started") manager.logger.Debug().Msg("subroutine started")
for { for {
@ -58,7 +57,7 @@ func screencastNew(config *config.Capture) *ScreencastManagerCtx {
case sample := <-manager.sample: case sample := <-manager.sample:
manager.image = sample manager.image = sample
case <-ticker.C: case <-ticker.C:
if manager.started && time.Now().After(manager.expires) { if manager.started && !atomic.CompareAndSwapInt32(&manager.expired, 0, 1) {
manager.stop() manager.stop()
} }
} }
@ -77,7 +76,7 @@ func (manager *ScreencastManagerCtx) Started() bool {
} }
func (manager *ScreencastManagerCtx) Image() ([]byte, error) { func (manager *ScreencastManagerCtx) Image() ([]byte, error) {
manager.expires = time.Now().Add(manager.timeout) atomic.StoreInt32(&manager.expired, 0)
if !manager.started { if !manager.started {
err := manager.start() err := manager.start()