thread-safe controller snapshot for GUI, CLI

This commit is contained in:
Dmitry Sergeev
2026-09-01 00:20:17 +03:00
parent ea8be34282
commit 03ebe5bbd0
2 changed files with 184 additions and 0 deletions
+41
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"sync"
)
type VideoSlotRunner interface {
@@ -40,11 +41,21 @@ var (
ErrSyncSlotRequired = errors.New("sync slot is required")
)
type SessionSnapshot struct {
Desired SessionConfig
Plan SessionPlan
Generation uint64
}
type SessionController struct {
videoSlot VideoSlotRunner
audioSlot AudioSlotRunner
syncSlot SyncSlotRunner
canSync SyncPredicate
mu sync.RWMutex
snapshot SessionSnapshot
hasSnapshot bool
}
func NewSessionController(
@@ -100,6 +111,13 @@ func (c *SessionController) Run(
desired := initial
runtime := c.startSessionRuntime(ctx, plan)
generation := uint64(1)
c.publish(SessionSnapshot{
Desired: initial,
Plan: plan,
Generation: generation,
})
for {
var runtimeDone <-chan struct{}
if runtime != nil {
@@ -144,9 +162,17 @@ func (c *SessionController) Run(
return err
}
if plan.Topology != nextPlan.Topology {
generation++
}
desired = nextDesired
plan = nextPlan
runtime = nextRuntime
c.publish(SessionSnapshot{
Desired: desired,
Plan: plan,
Generation: generation,
})
}
}
}
@@ -301,3 +327,18 @@ func sessionRuntimeSendError(ctx context.Context, runtime *sessionRuntime) error
<-runtime.done
return unexpectedSessionRuntimeError(runtime.result)
}
func (c *SessionController) Snapshot() (SessionSnapshot, bool) {
c.mu.RLock()
snapshot := c.snapshot
ok := c.hasSnapshot
c.mu.RUnlock()
return snapshot, ok
}
func (c *SessionController) publish(snapshot SessionSnapshot) {
c.mu.Lock()
c.snapshot = snapshot
c.hasSnapshot = true
c.mu.Unlock()
}