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 *imageBuildOperationState) opID() string { return op.snapshot().ID }
func (op *imageBuildOperationState) opIsDone() bool { return op.snapshot().Done }
func (op *imageBuildOperationState) opUpdatedAt() time.Time { return op.snapshot().UpdatedAt }
func (op *imageBuildOperationState) opCancel() { op.cancelOperation() }
type imageBuildProgressKey struct{}
type imageBuildOperationState struct {
@ -161,14 +166,7 @@ func (d *Daemon) BeginImageBuild(_ context.Context, params api.ImageBuildParams)
}
buildCtx, cancel := context.WithCancel(context.Background())
op.setCancel(cancel)
d.imageBuildOpsMu.Lock()
if d.imageBuildOps == nil {
d.imageBuildOps = map[string]*imageBuildOperationState{}
}
d.imageBuildOps[op.op.ID] = op
d.imageBuildOpsMu.Unlock()
d.imageBuildOps.insert(op)
go d.runImageBuildOperation(withImageBuildProgress(buildCtx, op), op, params)
return op.snapshot(), nil
}
@ -183,9 +181,7 @@ func (d *Daemon) runImageBuildOperation(ctx context.Context, op *imageBuildOpera
}
func (d *Daemon) ImageBuildStatus(_ context.Context, id string) (api.ImageBuildOperation, error) {
d.imageBuildOpsMu.Lock()
op, ok := d.imageBuildOps[strings.TrimSpace(id)]
d.imageBuildOpsMu.Unlock()
op, ok := d.imageBuildOps.get(strings.TrimSpace(id))
if !ok {
return api.ImageBuildOperation{}, fmt.Errorf("image build operation not found: %s", id)
}
@ -193,9 +189,7 @@ func (d *Daemon) ImageBuildStatus(_ context.Context, id string) (api.ImageBuildO
}
func (d *Daemon) CancelImageBuild(_ context.Context, id string) error {
d.imageBuildOpsMu.Lock()
op, ok := d.imageBuildOps[strings.TrimSpace(id)]
d.imageBuildOpsMu.Unlock()
op, ok := d.imageBuildOps.get(strings.TrimSpace(id))
if !ok {
return fmt.Errorf("image build operation not found: %s", id)
}
@ -204,15 +198,5 @@ func (d *Daemon) CancelImageBuild(_ context.Context, id string) error {
}
func (d *Daemon) pruneImageBuildOperations(olderThan time.Time) {
d.imageBuildOpsMu.Lock()
defer d.imageBuildOpsMu.Unlock()
for id, op := range d.imageBuildOps {
snapshot := op.snapshot()
if !snapshot.Done {
continue
}
if snapshot.UpdatedAt.Before(olderThan) {
delete(d.imageBuildOps, id)
}
}
d.imageBuildOps.prune(olderThan)
}