Refactor command handling: rename types.go to commands.go, update command response types, and streamline session management logic

This commit is contained in:
2025-09-10 13:05:34 +00:00
parent 6ebc541de0
commit e56bb210f8
6 changed files with 275 additions and 474 deletions

View File

@@ -1,75 +1,40 @@
package manager
import (
"fmt"
"log/slog"
"gitlab.michelsen.id/phillmichelsen/tessera/services/data_service/internal/domain"
"gitlab.michelsen.id/phillmichelsen/tessera/services/data_service/internal/provider"
)
// Lightweight error helper to define package-level errors inline.
type constErr string
func lg() *slog.Logger { return slog.Default().With("cmp", "manager") }
func (e constErr) Error() string { return string(e) }
func errorf(s string) error { return constErr(s) }
// copySet copies a set of identifiers to a new map.
func copySet(in map[domain.Identifier]struct{}) map[domain.Identifier]struct{} {
out := make(map[domain.Identifier]struct{}, len(in))
for k := range in {
out[k] = struct{}{}
func identifierSetDifferences(oldIDs, nextIDs []domain.Identifier) (toAdd, toDel []domain.Identifier) {
oldSet := make(map[domain.Identifier]struct{}, len(oldIDs))
for _, id := range oldIDs {
oldSet[id] = struct{}{}
}
return out
}
// identifierSetDifferences computes additions and deletions from old -> next.
func identifierSetDifferences(old map[domain.Identifier]struct{}, next []domain.Identifier) (toAdd, toDel []domain.Identifier) {
newSet := make(map[domain.Identifier]struct{}, len(next))
for _, id := range next {
newSet := make(map[domain.Identifier]struct{}, len(nextIDs))
for _, id := range nextIDs {
newSet[id] = struct{}{}
if _, ok := old[id]; !ok {
if _, ok := oldSet[id]; !ok {
toAdd = append(toAdd, id)
}
}
for id := range old {
for _, id := range oldIDs {
if _, ok := newSet[id]; !ok {
toDel = append(toDel, id)
}
}
return
}
// resolveProvider parses a raw identifier and looks up the provider.
func (m *Manager) resolveProvider(id domain.Identifier) (provider.Provider, string, error) {
provName, subj, ok := id.ProviderSubject()
if !ok || provName == "" || subj == "" {
return nil, "", ErrInvalidIdentifier
func identifierMapToSlice(m map[domain.Identifier]struct{}) []domain.Identifier {
ids := make([]domain.Identifier, 0, len(m))
for id := range m {
ids = append(ids, id)
}
p := m.providers[provName]
if p == nil {
return nil, "", fmt.Errorf("%w: %s", ErrUnknownProvider, provName)
}
return p, subj, nil
}
// incrementStreamRefCount increments refcount and returns true if transitioning 0->1.
func (m *Manager) incrementStreamRefCount(id domain.Identifier) bool {
rc := m.streamRef[id] + 1
m.streamRef[id] = rc
return rc == 1
}
// decrementStreamRefCount decrements refcount and returns true if transitioning 1->0.
func (m *Manager) decrementStreamRefCount(id domain.Identifier) bool {
rc, ok := m.streamRef[id]
if !ok {
return false
}
rc--
if rc <= 0 {
delete(m.streamRef, id)
return true
}
m.streamRef[id] = rc
return false
return ids
}