mirror of https://github.com/jetkvm/kvm.git
refactor: rewrite waitForVideoStreamingStatus to use ticker instead of time.Sleep
This commit is contained in:
parent
4244c84a2a
commit
26872ee0b6
|
|
@ -48,6 +48,18 @@ const (
|
|||
VideoStreamingStatusInactive VideoStreamingStatus = 0
|
||||
)
|
||||
|
||||
func (s VideoStreamingStatus) String() string {
|
||||
switch s {
|
||||
case VideoStreamingStatusActive:
|
||||
return "active"
|
||||
case VideoStreamingStatusStopping:
|
||||
return "stopping"
|
||||
case VideoStreamingStatusInactive:
|
||||
return "inactive"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func NewNative(opts NativeOptions) *Native {
|
||||
pid := os.Getpid()
|
||||
nativeSubLogger := nativeLogger.With().Int("pid", pid).Str("scope", "native").Logger()
|
||||
|
|
|
|||
|
|
@ -29,6 +29,23 @@ func isSleepModeSupported() bool {
|
|||
|
||||
const sleepModeWaitTimeout = 3 * time.Second
|
||||
|
||||
func (n *Native) waitForVideoStreamingStatus(status VideoStreamingStatus) error {
|
||||
timeout := time.After(sleepModeWaitTimeout)
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
if videoGetStreamingStatus() == status {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case <-timeout:
|
||||
return fmt.Errorf("timed out waiting for video streaming status to be %s", status.String())
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Native) setSleepMode(enabled bool) error {
|
||||
if !n.sleepModeSupported {
|
||||
return nil
|
||||
|
|
@ -51,16 +68,8 @@ func (n *Native) setSleepMode(enabled bool) error {
|
|||
}
|
||||
|
||||
if shouldWait {
|
||||
start := time.Now()
|
||||
for {
|
||||
if n.VideoGetStreamingStatus() == VideoStreamingStatusInactive {
|
||||
break
|
||||
}
|
||||
if time.Since(start) >= sleepModeWaitTimeout {
|
||||
n.l.Warn().Msg("timed out waiting for video stream to stop")
|
||||
break
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
if err := n.waitForVideoStreamingStatus(VideoStreamingStatusInactive); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue