
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
github.com/patrulek/stepper
Stepper is a thread-safe, in-memory finite state machine written in Golang.
Aim of this package is to provide simple API that will let you manage your objects states in a safe manner.
import (
"context"
"errors"
"github.com/patrulek/stepper"
)
const (
// final states
idle = iota
closed
// transitive states
processing
closing
)
var state = stepper.New(idle)
var ErrClosed = errors.New("closed")
func heavyTask(context.Context) error {
// much heavy
return nil
}
func rollbackHeavyTask() {
// rollback
}
func Process(ctx context.Context) error {
transition := stepper.NewTransition(
processing,
idle,
func(fctx context.Context) error {
return heavyTask(fctx)
},
func(err error) int32 {
rollbackHeavyTask()
return idle
},
)
return state.Step(ctx, map[int32]any{
closed: ErrClosed,
idle: transition,
})
}
func Close(ctx context.Context) error {
transition := stepper.NewTransition(
closing,
closed,
func(fctx context.Context) error {
// release resources
return nil
},
nil, // in a case when transition fails, stepper will set invalid, non-functional state (unless proper repair function defined)
)
return state.Queue(ctx, map[int32]any{
closed: ErrClosed,
idle: transition,
})
}
Stepper expose three functions that let you change states:
Step(context.Context, TransitionMap) error
: Performs transition only if no other transition is queued or currently in progress, otherwise immediately return an errorQueue(context.Context, TransitionMap) error
: Queues given transition with lowest possible priority; if it's first element in queue transition will execute immediatelyQueueWithPriority(context.Context, int32, TransitionMap) error
: Queues transition with given priority; if it's first element in queue transition will execute immediatelyError returned by any of these methods may be validation cancellation or state transition error.
Priority
can takes one of three values:
Any other values will return error.
TransitionMap
is in fact an alias for map[int32]any
, but only these types will work:
stepper.transition
: stepper will perform transition for a key that matches current machine state; this will panic if transition function was not definederror
: stepper will return given error for a key that matches current machine statenil
: stepper will return nil for a key that matches current machine stateAny other type will yield panic.
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Go 5 180 100 810
Markdown 1 29 0 93
YAML 2 10 1 84
-------------------------------------------------------------------------------
TOTAL 8 219 101 987
-------------------------------------------------------------------------------
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.