diff --git a/internal/plugin/errors.go b/internal/plugin/errors.go new file mode 100644 index 0000000..2288cdc --- /dev/null +++ b/internal/plugin/errors.go @@ -0,0 +1,7 @@ +package plugin + +import "errors" + +var ( + ErrNameAlreadyExists = errors.New("name already exists") +) diff --git a/internal/plugin/plugin.go b/internal/plugin/plugin.go new file mode 100644 index 0000000..588bdb2 --- /dev/null +++ b/internal/plugin/plugin.go @@ -0,0 +1,19 @@ +package plugin + +import "context" + +// ID is a unique runtime hot-path identifier of Plugin. +type ID uint64 + +// Name is a unique cold-path name of Plugin. +type Name string + +// Plugin defines a runtime extension that initializes its resources within an isolated Slot. +type Plugin interface { + // Init sets up the plugin using the provided configuration and registers its resources into the slot. + Init(ctx context.Context, config InitConfig, slot *Slot) error +} + +// InitConfig holds initialization parameters and metadata passed to a plugin during startup. +type InitConfig struct { +} diff --git a/internal/plugin/registry.go b/internal/plugin/registry.go new file mode 100644 index 0000000..93fd238 --- /dev/null +++ b/internal/plugin/registry.go @@ -0,0 +1,56 @@ +package plugin + +import ( + "fmt" + "sync" +) + +// Registry maps ID to Plugin and Name to ID. +type Registry struct { + nextID uint64 + byID map[ID]Plugin + nameToID map[Name]ID + mu sync.RWMutex +} + +// NewRegistry creates a new Registry. +func NewRegistry() *Registry { + return &Registry{ + byID: make(map[ID]Plugin), + nameToID: make(map[Name]ID), + } +} + +// Put maps the given Plugin by a generated ID, and maps the given Name to the generated ID. +func (r *Registry) Put(name Name, plugin Plugin) (ID, error) { + r.mu.Lock() + defer r.mu.Unlock() + + if _, ok := r.nameToID[name]; ok { + return 0, fmt.Errorf("%w: %v", ErrNameAlreadyExists, name) + } + + r.nextID++ + id := ID(r.nextID) + r.byID[id] = plugin + r.nameToID[name] = id + return id, nil +} + +// Get returns the Plugin by the given ID. +func (r *Registry) Get(id ID) (Plugin, bool) { + r.mu.RLock() + defer r.mu.RUnlock() + + plugin, ok := r.byID[id] + return plugin, ok +} + +// GetID returns the ID by the given Name. +func (r *Registry) GetID(name Name) (ID, bool) { + r.mu.RLock() + defer r.mu.RUnlock() + + plugin, ok := r.nameToID[name] + return plugin, ok +} diff --git a/internal/plugin/slot.go b/internal/plugin/slot.go new file mode 100644 index 0000000..e3604ae --- /dev/null +++ b/internal/plugin/slot.go @@ -0,0 +1,41 @@ +package plugin + +import ( + "errors" + "io" + "slices" +) + +// Slot provides an isolated execution context for a plugin and manages the lifecycle of its tracked resources. +type Slot struct { + closers []io.Closer +} + +// NewSlot creates a Slot. +func NewSlot() *Slot { + return &Slot{ + closers: make([]io.Closer, 0), + } +} + +// TrackCloser registers an io.Closer to be disposed of when the slot closes. +// Nil closers are ignored. +func (s *Slot) TrackCloser(closer io.Closer) { + if closer == nil { + return + } + s.closers = append(s.closers, closer) +} + +// Close disposes of all tracked closers in LIFO order and joins any returned errors. +func (s *Slot) Close() error { + errs := make([]error, 0, len(s.closers)) + for _, v := range slices.Backward(s.closers) { + if err := v.Close(); err != nil { + errs = append(errs, err) + } + } + + s.closers = nil + return errors.Join(errs...) +} diff --git a/internal/workspace/service.go b/internal/workspace/service.go index c0b238d..faff409 100644 --- a/internal/workspace/service.go +++ b/internal/workspace/service.go @@ -3,8 +3,6 @@ package workspace import ( "context" "fmt" - - "github.com/Skoowshot/qrix-runtime/internal/store" ) type Service struct { @@ -22,10 +20,7 @@ func (s *Service) Create(ctx context.Context, name Name) (ID, error) { return 0, err } - ws := &Workspace{ - Stores: store.NewRegistry(), - Network: nil, - } + ws := NewWorkspace() id, err := s.registry.Put(name, ws) if err != nil { diff --git a/internal/workspace/workspace.go b/internal/workspace/workspace.go index f32c881..63cee06 100644 --- a/internal/workspace/workspace.go +++ b/internal/workspace/workspace.go @@ -1,8 +1,10 @@ package workspace import ( + "errors" + "github.com/Skoowshot/qrix-runtime/internal/network" - "github.com/Skoowshot/qrix-runtime/internal/store" + "github.com/Skoowshot/qrix-runtime/internal/plugin" ) // ID is a unique runtime hot-path identifier of Workspace. @@ -13,6 +15,26 @@ type Name string // Workspace is an isolated self-contained unit holding application state. type Workspace struct { - Stores *store.Registry - Network *network.Network + slots map[plugin.ID]*plugin.Slot + network *network.Network +} + +// NewWorkspace creates a Workspace. +func NewWorkspace() *Workspace { + return &Workspace{ + slots: make(map[plugin.ID]*plugin.Slot), + } +} + +// Close disposes all plugin slots and joins any returned errors. +func (w *Workspace) Close() error { + errs := make([]error, 0, len(w.slots)) + for _, v := range w.slots { + if err := v.Close(); err != nil { + errs = append(errs, err) + } + } + + w.slots = nil + return errors.Join(errs...) }