From 35c1bf3673e3052175e5b203628f957bc288b571 Mon Sep 17 00:00:00 2001 From: Skoow <89569841+Skoowshot@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:35:19 +0500 Subject: [PATCH 1/5] feat: add plugin.Plugin, plugin.Slot, plugin.InitConfig --- internal/plugin/plugin.go | 19 +++++++++++++++++++ internal/plugin/slot.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 internal/plugin/plugin.go create mode 100644 internal/plugin/slot.go 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/slot.go b/internal/plugin/slot.go new file mode 100644 index 0000000..b117eb7 --- /dev/null +++ b/internal/plugin/slot.go @@ -0,0 +1,34 @@ +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 +} + +// 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...) +} From 00caa4c207d6e05acdf2d57cbc339d247e73c3d4 Mon Sep 17 00:00:00 2001 From: Skoow <89569841+Skoowshot@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:35:25 +0500 Subject: [PATCH 2/5] feat: add plugin.Registry --- internal/plugin/errors.go | 7 +++++ internal/plugin/registry.go | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 internal/plugin/errors.go create mode 100644 internal/plugin/registry.go 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/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 +} From b0400a013d2cbd62c2b7d38d58e4779fbc1c9048 Mon Sep 17 00:00:00 2001 From: Skoow <89569841+Skoowshot@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:47:51 +0500 Subject: [PATCH 3/5] feat: add plugin.NewSlot() --- internal/plugin/slot.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/plugin/slot.go b/internal/plugin/slot.go index b117eb7..e3604ae 100644 --- a/internal/plugin/slot.go +++ b/internal/plugin/slot.go @@ -11,6 +11,13 @@ 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) { From bfb1b7a50f605bed8a320a1946f73143d1cad0ed Mon Sep 17 00:00:00 2001 From: Skoow <89569841+Skoowshot@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:48:01 +0500 Subject: [PATCH 4/5] feat: add slots to Workspace --- internal/workspace/errors.go | 1 - internal/workspace/workspace.go | 28 +++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/workspace/errors.go b/internal/workspace/errors.go index f341d76..a21eedc 100644 --- a/internal/workspace/errors.go +++ b/internal/workspace/errors.go @@ -4,5 +4,4 @@ import "errors" var ( ErrNameAlreadyExists = errors.New("name already exists") - ErrNotFound = errors.New("not found") ) 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...) } From 24db75170c52b59ce29c3398981f6e202baaa2db Mon Sep 17 00:00:00 2001 From: Skoow <89569841+Skoowshot@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:34:09 +0500 Subject: [PATCH 5/5] fix: workspace.Service --- internal/workspace/errors.go | 1 + internal/workspace/service.go | 7 +------ 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/internal/workspace/errors.go b/internal/workspace/errors.go index a21eedc..f341d76 100644 --- a/internal/workspace/errors.go +++ b/internal/workspace/errors.go @@ -4,4 +4,5 @@ import "errors" var ( ErrNameAlreadyExists = errors.New("name already exists") + ErrNotFound = errors.New("not found") ) 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 {