Documentation
¶
Overview ¶
Package warmupx provides gradual capacity ramp-up (slow start) for industrial Go services.
A Warmer increases its capacity from a minimum to a maximum over a configurable duration using one of four strategies: Linear, Exponential, Logarithmic, or Step.
w := warmupx.New(
warmupx.WithDuration(30 * time.Second),
warmupx.WithStrategy(warmupx.Exponential),
)
w.Start()
defer w.Stop()
if w.Allow() {
// handle request
}
Use cases: cold start, post-deploy rollout, circuit-breaker recovery, auto-scaling new instances.
Index ¶
- Constants
- type Option
- func WithDuration(d time.Duration) Option
- func WithExpFactor(f float64) Option
- func WithInterval(d time.Duration) Option
- func WithMaxCapacity(v float64) Option
- func WithMinCapacity(v float64) Option
- func WithOnCapacityChange(fn func(oldCap, newCap float64)) Option
- func WithOnComplete(fn func()) Option
- func WithStepCount(n int) Option
- func WithStrategy(s Strategy) Option
- type Stats
- type Strategy
- type Warmer
- func (w *Warmer) Allow() bool
- func (w *Warmer) AllowOrError() error
- func (w *Warmer) Capacity() float64
- func (w *Warmer) IsComplete() bool
- func (w *Warmer) IsWarming() bool
- func (w *Warmer) MaxRequests(baseLimit int) int
- func (w *Warmer) Progress() float64
- func (w *Warmer) Reset()
- func (w *Warmer) ResetStats()
- func (w *Warmer) Start()
- func (w *Warmer) StartAt(capacity float64)
- func (w *Warmer) Stats() Stats
- func (w *Warmer) Stop()
- func (w *Warmer) WaitForCompletion(ctx context.Context) error
Constants ¶
const (
// CodeRejected indicates a request was rejected during warmup.
CodeRejected = "REJECTED"
)
const DomainWarmup = "WARMUP"
DomainWarmup is the errx domain for all warmup errors.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*config)
Option configures New behavior.
func WithDuration ¶
WithDuration sets the total warmup duration.
func WithExpFactor ¶
WithExpFactor sets the exponential factor for the Exponential strategy. Higher values mean faster initial ramp.
func WithInterval ¶
WithInterval overrides the capacity-update interval. Default: Duration/100, clamped to [10ms, 1s].
func WithMaxCapacity ¶
WithMaxCapacity sets the target capacity (clamped to [0, 1]).
func WithMinCapacity ¶
WithMinCapacity sets the starting capacity (clamped to [0, 1]).
func WithOnCapacityChange ¶
WithOnCapacityChange registers a callback invoked asynchronously when capacity changes by more than 1%. May be delivered out-of-order.
func WithOnComplete ¶
func WithOnComplete(fn func()) Option
WithOnComplete registers a callback invoked asynchronously when warmup finishes.
func WithStepCount ¶
WithStepCount sets the number of steps for the Step strategy.
type Stats ¶
type Stats struct {
Strategy string `json:"strategy"`
Capacity float64 `json:"capacity"`
MinCapacity float64 `json:"min_capacity"`
MaxCapacity float64 `json:"max_capacity"`
Progress float64 `json:"progress"`
IsWarming bool `json:"is_warming"`
IsComplete bool `json:"is_complete"`
Duration time.Duration `json:"duration"`
Elapsed time.Duration `json:"elapsed"`
Remaining time.Duration `json:"remaining"`
Allowed int64 `json:"allowed"`
Rejected int64 `json:"rejected"`
}
Stats holds a snapshot of warmer state.
type Strategy ¶
type Strategy uint8
Strategy defines the capacity ramp-up curve.
const ( // Linear increases capacity uniformly: capacity(t) = min + delta*t. Linear Strategy = iota // Exponential increases fast then flattens: capacity(t) = min + delta*(1 - e^(-k*t*5)). Exponential // Logarithmic increases fast at start, slow at end. Logarithmic // Step increases in discrete jumps. Step )
type Warmer ¶
type Warmer struct {
// contains filtered or unexported fields
}
Warmer provides gradual capacity ramp-up with probabilistic admission control. It is safe for concurrent use.
func New ¶
New creates a Warmer with the given options. Defaults: Linear strategy, 10% → 100% over 1 minute.
func (*Warmer) Allow ¶
Allow returns true if a request should be admitted based on the current capacity. Uses probabilistic admission: with capacity 0.5, approximately 50% of calls return true.
func (*Warmer) AllowOrError ¶
AllowOrError returns nil if admitted, or an *errx.Error with code CodeRejected and capacity/progress metadata if rejected.
func (*Warmer) IsComplete ¶
IsComplete reports whether warmup has finished.
func (*Warmer) MaxRequests ¶
MaxRequests scales a base limit by the current capacity, rounding up. Returns 0 if baseLimit <= 0.
func (*Warmer) Reset ¶
func (w *Warmer) Reset()
Reset stops and restarts warmup from WithMinCapacity.
func (*Warmer) ResetStats ¶
func (w *Warmer) ResetStats()
ResetStats zeroes the allowed/rejected counters.