Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRuleNotFound indicates the sequence rule was not found or is inactive. ErrRuleNotFound = errors.New("sequence rule not found or inactive") // ErrSequenceOverflow indicates the sequence value has exceeded its configured MaxValue. ErrSequenceOverflow = errors.New("sequence value exceeded max value") // ErrInvalidCount indicates the requested count is invalid (must be >= 1). ErrInvalidCount = errors.New("sequence generate count must be >= 1") )
Functions ¶
Types ¶
type Generator ¶
type Generator interface {
// Generate generates a new serial number for the given rule key.
Generate(ctx context.Context, key string) (string, error)
// GenerateN generates N serial numbers for the given rule key in a single atomic operation.
GenerateN(ctx context.Context, key string, count int) ([]string, error)
}
Generator provides serial number generation.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore implements Store using in-memory storage. Suitable for single-instance deployments, development, and testing.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore creates a new in-memory sequence store.
func (*MemoryStore) Register ¶
func (s *MemoryStore) Register(rules ...*Rule)
Register preloads rules into the memory store. Existing rules with the same key will be overwritten. A deep copy of each rule is stored to prevent external mutation.
type OverflowStrategy ¶
type OverflowStrategy string
OverflowStrategy defines the behavior when the sequence value exceeds MaxValue.
const ( // OverflowError returns ErrSequenceOverflow when MaxValue is exceeded. This is the default strategy. OverflowError OverflowStrategy = "error" // OverflowReset automatically resets the counter to StartValue when MaxValue is exceeded. OverflowReset OverflowStrategy = "reset" // OverflowExtend lets the counter keep growing past MaxValue without erroring or resetting. // The rendered number may then exceed the SeqLength zero-pad width (more digits than configured). OverflowExtend OverflowStrategy = "extend" )
type ResetCycle ¶
type ResetCycle string
ResetCycle defines when the sequence counter should be reset.
const ( ResetNone ResetCycle = "N" ResetDaily ResetCycle = "D" ResetWeekly ResetCycle = "W" ResetMonthly ResetCycle = "M" ResetQuarterly ResetCycle = "Q" ResetYearly ResetCycle = "Y" )
type Rule ¶
type Rule struct {
Key string
Name string
Prefix string // empty = no prefix
Suffix string // empty = no suffix
DateFormat string // e.g. "yyyyMMdd", empty = no date part
SeqLength int // zero-padded width
SeqStep int // increment per generation
StartValue int // value after reset (first generated = StartValue + Step)
MaxValue int // upper limit (0 = unlimited)
OverflowStrategy OverflowStrategy // behavior when MaxValue is reached
ResetCycle ResetCycle // N/D/W/M/Q/Y
CurrentValue int // current counter value
LastResetAt *timex.DateTime // nil = never reset
IsActive bool // whether the rule is active
}
Rule defines the configuration for serial number generation.
type Store ¶
type Store interface {
// Reserve reserves count sequence values for key based on rule policy at now.
// It returns the rule snapshot used for generation and the final counter value in the reserved batch.
Reserve(ctx context.Context, key string, count int, now timex.DateTime) (rule *Rule, newValue int, err error)
}
Store abstracts rule persistence and atomic counter operations. Implementations must guarantee atomicity of Reserve (read-modify-write must be serialized per key).