Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/plugin/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package plugin

import "errors"

var (
ErrNameAlreadyExists = errors.New("name already exists")
)
19 changes: 19 additions & 0 deletions internal/plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -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 {
}
56 changes: 56 additions & 0 deletions internal/plugin/registry.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions internal/plugin/slot.go
Original file line number Diff line number Diff line change
@@ -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...)
}
7 changes: 1 addition & 6 deletions internal/workspace/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package workspace
import (
"context"
"fmt"

"github.com/Skoowshot/qrix-runtime/internal/store"
)

type Service struct {
Expand All @@ -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 {
Expand Down
28 changes: 25 additions & 3 deletions internal/workspace/workspace.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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...)
}
Loading