Resolve "Deprecate Providers package in favor of Worker"

This commit is contained in:
2025-10-09 15:53:02 +00:00
parent a1993c6c36
commit c512f51a57
26 changed files with 1695 additions and 2239 deletions

View File

@@ -3,10 +3,7 @@
package worker
import (
"context"
"errors"
"log/slog"
"sync"
"time"
"github.com/google/uuid"
@@ -18,128 +15,37 @@ var (
ErrWorkerRunning = errors.New("worker already running")
)
type SessionController interface {
NewSession(idleAfter time.Duration) uuid.UUID
AttachClient(id uuid.UUID, inBuf, outBuf int) (chan<- domain.Message, <-chan domain.Message, error)
DetachClient(id uuid.UUID) error
ConfigureSession(id uuid.UUID, next []domain.Identifier) error
CloseSession(id uuid.UUID) error
}
type (
ReceiverFunc func() (domain.Message, error)
SenderFunc func(m domain.Message) error
)
type Instruction struct{}
type SessionController interface {
CreateSession(idleAfter time.Duration) uuid.UUID
LeaseSessionReceiver(sid uuid.UUID) (ReceiverFunc, error)
LeaseSessionSender(sid uuid.UUID) (SenderFunc, error)
ReleaseSessionReceiver(sid uuid.UUID) error
ReleaseSessionSender(sid uuid.UUID) error
ConfigureSession(sid uuid.UUID, cfg any) error
CloseSession(sid uuid.UUID) error
}
type Worker interface {
Start(workerID uuid.UUID, controller SessionController, cfg []byte) error
Start(spec []byte, ctrl SessionController) error
Stop() error
Execute(ctx context.Context, inst Instruction) error
IsRunning() bool
ID() uuid.UUID
SetUnits(units [][]byte) error
GetSpecification() []byte
GetUnits() [][]byte
}
type BaseStatefulWorker struct {
workerUUID uuid.UUID
sc SessionController
sid uuid.UUID
in chan<- domain.Message
out <-chan domain.Message
running bool
mu sync.RWMutex
type Normalizer interface {
NormalizeSpecification(spec []byte) ([]byte, error)
NormalizeUnit(unit []byte) ([]byte, error)
}
func (w *BaseStatefulWorker) Start(workerUUID uuid.UUID, sessionController SessionController, _ []byte) error {
if sessionController == nil {
return errors.New("nil SessionController provided")
}
w.mu.Lock()
if w.running {
w.mu.Unlock()
return ErrWorkerRunning
}
sid := sessionController.NewSession(time.Duration(0)) // set a zero duration to disable idle timeout
in, out, err := sessionController.AttachClient(sid, 256, 256)
if err != nil {
w.mu.Unlock()
return err
}
w.sc, w.in, w.out = sessionController, in, out
w.workerUUID = workerUUID
w.running = true
w.mu.Unlock()
return nil
}
func (w *BaseStatefulWorker) Stop() error {
w.mu.Lock()
if !w.running {
w.mu.Unlock()
return ErrWorkerNotRunning
}
err := w.sc.DetachClient(w.sid)
if err != nil {
slog.Default().Error("error when detaching client", "error", err.Error())
}
err = w.sc.CloseSession(w.sid)
if err != nil {
slog.Default().Error("error when closing session", "error", err.Error())
}
w.sc, w.in, w.out = nil, nil, nil
w.workerUUID, w.sid = uuid.Nil, uuid.Nil
w.running = false
w.mu.Unlock()
return nil
}
func (w *BaseStatefulWorker) IsRunning() bool {
w.mu.RLock()
running := w.running
w.mu.RUnlock()
return running
}
func (w *BaseStatefulWorker) ID() uuid.UUID {
w.mu.RLock()
id := w.workerUUID
w.mu.RUnlock()
return id
}
func (w *BaseStatefulWorker) SetReceiveIdentifiers(ids []domain.Identifier) error {
w.mu.RLock()
if !w.running {
w.mu.RUnlock()
return ErrWorkerNotRunning
}
w.mu.RUnlock()
return w.sc.ConfigureSession(w.sid, ids)
}
func (w *BaseStatefulWorker) In() chan<- domain.Message {
w.mu.RLock()
ch := w.in
w.mu.RUnlock()
return ch
}
func (w *BaseStatefulWorker) Out() <-chan domain.Message {
w.mu.RLock()
ch := w.out
w.mu.RUnlock()
return ch
}
type Factory func() Worker