Move subsystem state/locks off Daemon into owning types

Daemon no longer owns a coarse mu shared across unrelated concerns.
Each subsystem now carries its own state and lock:

- tapPool: entries, next, and mu move onto a new tapPool struct.
- sessionRegistry: sessionControllers + its mutex move off Daemon.
- opRegistry[T asyncOp]: generic registry collapses the two ad-hoc
  vm-create and image-build operation maps (and their mutexes) into one
  shared type; the Begin/Status/Cancel/Prune methods simplify.
- vmLockSet: the sync.Map of per-VM mutexes moves into its own type;
  lockVMID forwards.
- Daemon.mu splits into imageOpsMu (image-registry mutations) and
  createVMMu (CreateVM serialisation) so image ops and VM creates no
  longer block each other.

Lock ordering collapses to vmLocks[id] -> {createVMMu, imageOpsMu} ->
subsystem-local leaves. doc.go and ARCHITECTURE.md updated.

No behavior change; tests green.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Thales Maciel 2026-04-15 15:58:33 -03:00
parent ea0db1e17e
commit 59f2766139
No known key found for this signature in database
GPG key ID: 33112E6833C34679
11 changed files with 238 additions and 152 deletions

View file

@ -11,6 +11,11 @@ import (
"banger/internal/model"
)
func (op *vmCreateOperationState) opID() string { return op.snapshot().ID }
func (op *vmCreateOperationState) opIsDone() bool { return op.snapshot().Done }
func (op *vmCreateOperationState) opUpdatedAt() time.Time { return op.snapshot().UpdatedAt }
func (op *vmCreateOperationState) opCancel() { op.cancelOperation() }
type vmCreateProgressKey struct{}
type vmCreateOperationState struct {
@ -148,14 +153,7 @@ func (d *Daemon) BeginVMCreate(_ context.Context, params api.VMCreateParams) (ap
}
createCtx, cancel := context.WithCancel(context.Background())
op.setCancel(cancel)
d.createOpsMu.Lock()
if d.createOps == nil {
d.createOps = map[string]*vmCreateOperationState{}
}
d.createOps[op.op.ID] = op
d.createOpsMu.Unlock()
d.createOps.insert(op)
go d.runVMCreateOperation(withVMCreateProgress(createCtx, op), op, params)
return op.snapshot(), nil
}
@ -170,9 +168,7 @@ func (d *Daemon) runVMCreateOperation(ctx context.Context, op *vmCreateOperation
}
func (d *Daemon) VMCreateStatus(_ context.Context, id string) (api.VMCreateOperation, error) {
d.createOpsMu.Lock()
op, ok := d.createOps[strings.TrimSpace(id)]
d.createOpsMu.Unlock()
op, ok := d.createOps.get(strings.TrimSpace(id))
if !ok {
return api.VMCreateOperation{}, fmt.Errorf("vm create operation not found: %s", id)
}
@ -180,9 +176,7 @@ func (d *Daemon) VMCreateStatus(_ context.Context, id string) (api.VMCreateOpera
}
func (d *Daemon) CancelVMCreate(_ context.Context, id string) error {
d.createOpsMu.Lock()
op, ok := d.createOps[strings.TrimSpace(id)]
d.createOpsMu.Unlock()
op, ok := d.createOps.get(strings.TrimSpace(id))
if !ok {
return fmt.Errorf("vm create operation not found: %s", id)
}
@ -191,15 +185,5 @@ func (d *Daemon) CancelVMCreate(_ context.Context, id string) error {
}
func (d *Daemon) pruneVMCreateOperations(olderThan time.Time) {
d.createOpsMu.Lock()
defer d.createOpsMu.Unlock()
for id, op := range d.createOps {
snapshot := op.snapshot()
if !snapshot.Done {
continue
}
if snapshot.UpdatedAt.Before(olderThan) {
delete(d.createOps, id)
}
}
d.createOps.prune(olderThan)
}