Documentation
¶
Overview ¶
Package processor wraps slice transforms with lifecycle metrics, tracing, logging, pause handling, and completion callbacks.
A processor honors a pipeline pause controller carried by its context before invoking its transform. A stage interprets the async flag, runs the processor in a goroutine, and does not forward its output. Transforms must not mutate their input slice or pointed-to values.
Index ¶
- Constants
- type Func
- type IProcessor
- type OnFinished
- type Processor
- func (p *Processor[ProcessingData]) GetAsync() bool
- func (p *Processor[ProcessingData]) GetCounterCreated() *expvar.Int
- func (p *Processor[ProcessingData]) GetCounterDone() *expvar.Int
- func (p *Processor[ProcessingData]) GetCounterFailed() *expvar.Int
- func (p *Processor[ProcessingData]) GetCounterInterrupted() *expvar.Int
- func (p *Processor[ProcessingData]) GetCounterRunning() *expvar.Int
- func (p *Processor[ProcessingData]) GetCreatedAt() time.Time
- func (p *Processor[ProcessingData]) GetDescription() string
- func (p *Processor[ProcessingData]) GetDuration() *expvar.Int
- func (p *Processor[ProcessingData]) GetLogger() sypl.ISypl
- func (p *Processor[ProcessingData]) GetMetrics() map[string]string
- func (p *Processor[ProcessingData]) GetName() string
- func (p *Processor[ProcessingData]) GetOnFinished() OnFinished[ProcessingData]
- func (p *Processor[ProcessingData]) GetStatus() *expvar.String
- func (p *Processor[ProcessingData]) GetType() string
- func (p *Processor[ProcessingData]) Run(ctx context.Context, processingData []ProcessingData) ([]ProcessingData, error)
- func (p *Processor[ProcessingData]) SetAsync(async bool)
- func (p *Processor[ProcessingData]) SetOnFinished(onFinished OnFinished[ProcessingData])
- type Transform
Examples ¶
Constants ¶
const Type = "processor"
Type identifies processor entities.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Func ¶
type Func[T any] func(p IProcessor[T]) IProcessor[T]
Func configures a processor and returns the implementation to retain. An option must return a usable implementation; returning one whose embedded interface is nil will panic when it is used, as it would anywhere in Go.
func WithAsync ¶
WithAsync returns a Func that controls goroutine execution by a stage.
WARN: The output of the processing will not be forwarded!
func WithOnFinished ¶
func WithOnFinished[T any](onFinished OnFinished[T]) Func[T]
WithOnFinished returns a Func that sets the completion callback.
type IProcessor ¶
type IProcessor[ProcessingData any] interface { shared.IMeta shared.IMetrics // GetOnFinished returns the `OnFinished` function. GetOnFinished() OnFinished[ProcessingData] // SetOnFinished sets the `OnFinished` function. SetOnFinished(onFinished OnFinished[ProcessingData]) // GetCounterInterrupted returns the counter for paused runs interrupted by // context cancellation or deadline. GetCounterInterrupted() *expvar.Int // SetAsync controls whether a stage runs the processor in a goroutine. It // changes scheduling only; Run still records lifecycle status and counters. // // WARN: The output of the processing will not be forwarded! SetAsync(async bool) // GetAsync reports whether a stage should run the processor in a goroutine. GetAsync() bool // Run transforms processingData without mutating it or pointed-to values. // It records context termination while paused as an interruption and // transform errors as failures. Run(ctx context.Context, processingData []ProcessingData) (processedOut []ProcessingData, err error) }
IProcessor defines processor lifecycle, metrics, configuration, and execution. Implementations must not mutate Run's input slice or the values it points to.
func New ¶
func New[ProcessingData any]( name, description string, fn Transform[ProcessingData], opts ...Func[ProcessingData], ) (IProcessor[ProcessingData], error)
New creates a processor. It applies options in order, retains each returned implementation, and then validates the result. Nil options and results are rejected. An option must return a usable implementation; returning one whose embedded interface is nil will panic when it is used, as it would anywhere in Go. Structural validation runs before lifecycle metric getters; the final implementation must expose non-nil lifecycle metrics. The transform must not mutate its input slice or pointed-to values.
Example ¶
Demonstrates a processor that transforms every item in a slice.
double, err := New(
"example-double",
"doubles every number",
func(_ context.Context, in []int) ([]int, error) {
out := make([]int, len(in))
for i, value := range in {
out[i] = value * 2
}
return out, nil
},
)
if err != nil {
log.Fatalln(err)
}
out, err := double.Run(context.Background(), []int{1, 2, 3})
if err != nil {
log.Fatalln(err)
}
fmt.Println(out)
Output: [2 4 6]
type OnFinished ¶
type OnFinished[T any] func(ctx context.Context, p IProcessor[T], originalIn []T, processedOut []T)
OnFinished runs after a processor succeeds with its original input and processed output.
type Processor ¶
type Processor[ProcessingData any] struct { // Func transforms a slice without mutating its input or pointed-to values. Func Transform[ProcessingData] `json:"-" validate:"required"` // Logger records processor lifecycle events. Logger sypl.ISypl `json:"-" validate:"required"` // Name identifies the processor. Name string `json:"name" validate:"required"` // Description describes the processor. Description string `json:"description"` // Lifecycle metrics. CounterInterrupted counts paused runs whose wait ends // because their context is cancelled or reaches its deadline. CounterCreated *expvar.Int `json:"counterCreated"` CounterDone *expvar.Int `json:"counterDone"` CounterFailed *expvar.Int `json:"counterFailed"` CounterInterrupted *expvar.Int `json:"counterInterrupted"` CounterRunning *expvar.Int `json:"counterRunning"` CreatedAt time.Time `json:"createdAt"` Duration *expvar.Int `json:"duration"` Status *expvar.String `json:"status"` // contains filtered or unexported fields }
Processor wraps a slice transform with lifecycle observability.
func (*Processor[ProcessingData]) GetAsync ¶
GetAsync reports whether a stage should run the processor in a goroutine.
func (*Processor[ProcessingData]) GetCounterCreated ¶
GetCounterCreated returns the `CounterCreated` metric.
func (*Processor[ProcessingData]) GetCounterDone ¶
GetCounterDone returns the `CounterDone` metric.
func (*Processor[ProcessingData]) GetCounterFailed ¶
GetCounterFailed returns the `CounterFailed` metric.
func (*Processor[ProcessingData]) GetCounterInterrupted ¶
GetCounterInterrupted returns the counter for paused runs interrupted by context cancellation or deadline.
func (*Processor[ProcessingData]) GetCounterRunning ¶
GetCounterRunning returns the `CounterRunning` metric.
func (*Processor[ProcessingData]) GetCreatedAt ¶
GetCreatedAt returns when the processor was constructed.
func (*Processor[ProcessingData]) GetDescription ¶
GetDescription returns the `Description` of the processor.
func (*Processor[ProcessingData]) GetDuration ¶
GetDuration returns the most recent successful transform duration in milliseconds. A failed transform does not replace it.
func (*Processor[ProcessingData]) GetMetrics ¶
GetMetrics returns selected processor metrics as strings.
func (*Processor[ProcessingData]) GetOnFinished ¶
func (p *Processor[ProcessingData]) GetOnFinished() OnFinished[ProcessingData]
GetOnFinished returns the `OnFinished` function.
func (*Processor[ProcessingData]) Run ¶
func (p *Processor[ProcessingData]) Run(ctx context.Context, processingData []ProcessingData) ([]ProcessingData, error)
Run waits if its context carries a paused pipeline controller, then invokes the transform and completion callback. A processor running without such a controller does not pause. Context termination while waiting is recorded as an interruption; transform errors are recorded as failures.
func (*Processor[ProcessingData]) SetAsync ¶
SetAsync marks the processor for goroutine execution by a stage. Async scheduling does not change Run's lifecycle status or counters. Async processors receive a shallow copy, so transforms must not mutate input or pointed-to values.
WARN: The output of the processing will not be forwarded!
func (*Processor[ProcessingData]) SetOnFinished ¶
func (p *Processor[ProcessingData]) SetOnFinished(onFinished OnFinished[ProcessingData])
SetOnFinished sets the `OnFinished` function.
Example ¶
Demonstrates setting a callback after constructing a processor.
double, err := New(
"example-double-callback",
"doubles every number",
func(_ context.Context, in []int) ([]int, error) {
out := make([]int, len(in))
for i, value := range in {
out[i] = value * 2
}
return out, nil
},
)
if err != nil {
log.Fatalln(err)
}
double.SetOnFinished(func(_ context.Context, p IProcessor[int], in, out []int) {
fmt.Printf("%s: %v -> %v\n", p.GetName(), in, out)
})
if _, err := double.Run(context.Background(), []int{2, 4}); err != nil {
log.Fatalln(err)
}
Output: example-double-callback: [2 4] -> [4 8]
type Transform ¶
type Transform[ProcessedData any] func(ctx context.Context, processingData []ProcessedData) (processedOut []ProcessedData, err error)
Transform transforms `processingData`, returning the processed data and any error. It must not mutate its input slice or pointed-to values; stages reuse results and async processors receive only a shallow copy.