Documentation
¶
Overview ¶
Package transcodelimiter implements a Temporal SlotSupplier that gates transcode-activity admission by a load-probe-driven controller. The supplier admits work while the worker is underutilized; once saturation is detected, no further activities are accepted until the worker drains.
Falls back to a configurable static cap when the load probe is unavailable or fails. The fallback transition is observable via the media_worker_transcode_admission_mode metric.
Index ¶
- type Config
- type Limiter
- func (l *Limiter) Close()
- func (l *Limiter) MarkSlotUsed(info worker.SlotMarkUsedInfo)
- func (l *Limiter) MaxSlots() int
- func (l *Limiter) ReleaseSlot(info worker.SlotReleaseInfo)
- func (l *Limiter) ReserveSlot(ctx context.Context, _ worker.SlotReservationInfo) (*worker.SlotPermit, error)
- func (l *Limiter) TryReserveSlot(_ worker.SlotReservationInfo) *worker.SlotPermit
- type Option
- type Sampler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// StaticCap is the maximum number of in-flight activities, regardless
// of probe value. Acts as a defensive backstop in probe mode and as the
// sole gate in fallback mode.
StaticCap int
// GPUThreshold is the smoothed load value at or above which the
// supplier blocks new reservations in probe mode.
GPUThreshold float64
// PostAdmissionCooldown is the minimum interval between successive
// MarkSlotUsed calls; the second is held until the cooldown elapses.
PostAdmissionCooldown time.Duration
}
Config holds the operator-tunable parameters of the limiter.
Defaults are applied by New when fields are zero-valued: StaticCap=5, GPUThreshold=0.8, PostAdmissionCooldown=3s. Sampler-side fields (sample interval, smoothing window) live on loadprobe.SamplerConfig.
PostAdmissionCooldown is treated specially: a zero value means "use the default" (so a Config{} struct boots with the documented defaults), and a strictly negative value also normalises to the default. Operators wanting no cooldown should set a small positive value (e.g. one nanosecond).
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter is a Temporal worker.SlotSupplier whose admission decisions are driven by a load-probe sampler. Construct with New and attach to a worker via worker.NewCompositeTuner(CompositeTunerOptions{ActivitySlotSupplier: limiter}).
Close releases the fallback-watch goroutine; the underlying sampler must be closed by the caller separately. The limiter is safe for concurrent use.
func New ¶
func New(cfg Config, sampler Sampler, reg prometheus.Registerer, opts ...Option) (*Limiter, error)
New constructs a Limiter that draws admission signals from sampler. The supplied registerer is used to publish the four media_worker_transcode_* metrics; a nil registerer disables metric emission (useful in tests).
Returns an error if sampler is nil. A sampler that is already in fallback state (e.g. constructed via loadprobe.Failed) is accepted: the limiter initializes in static-cap mode and the admission_mode gauge reflects this from the first scrape.
func (*Limiter) Close ¶
func (l *Limiter) Close()
Close releases the fallback-watch goroutine. Idempotent. The underlying sampler is not closed — the caller owns its lifecycle.
func (*Limiter) MarkSlotUsed ¶
func (l *Limiter) MarkSlotUsed(info worker.SlotMarkUsedInfo)
MarkSlotUsed stamps the cooldown clock and increments the in-flight count. Temporal calls this once per reserved slot that is actually used; reserved slots that are released without use (SlotReleaseReasonUnused) skip this call so the cooldown is not consumed by no-op poll cycles.
func (*Limiter) MaxSlots ¶
MaxSlots returns the static cap. Temporal uses this as a defensive backstop on the maximum number of slots the supplier will ever issue.
func (*Limiter) ReleaseSlot ¶
func (l *Limiter) ReleaseSlot(info worker.SlotReleaseInfo)
ReleaseSlot decrements the in-flight count when the permit was previously passed through MarkSlotUsed; releases of unmarked permits (Temporal's "unused" reservation path) leave the counter alone so they cannot cancel out an unrelated in-flight transcode.
func (*Limiter) ReserveSlot ¶
func (l *Limiter) ReserveSlot(ctx context.Context, _ worker.SlotReservationInfo) (*worker.SlotPermit, error)
ReserveSlot blocks until the limiter can admit an activity or ctx is cancelled. The returned permit is opaque — Temporal hands it back via MarkSlotUsed and ReleaseSlot for cooldown stamping and in-flight accounting.
func (*Limiter) TryReserveSlot ¶
func (l *Limiter) TryReserveSlot(_ worker.SlotReservationInfo) *worker.SlotPermit
TryReserveSlot returns a permit only if the limiter can admit immediately; otherwise it returns nil. Used by Temporal for eager activity reservation.
type Option ¶
type Option func(*Limiter)
Option configures a Limiter at construction time.
func WithLogger ¶
WithLogger sets the slog logger used for fallback-transition log lines. The default is slog.Default().
func WithNow ¶
WithNow overrides the time source. Tests inject a controlled clock to make cooldown timing deterministic.
func WithPollInterval ¶
WithPollInterval overrides the bounded re-check interval used while a reservation is blocked. Tests use a small value (e.g. 1ms) to keep the state machine responsive without sleeping for the production default.
type Sampler ¶
type Sampler interface {
// Value returns the smoothed load value in [0, 1].
Value() float64
// FailedC returns a channel that is closed when the sampler enters
// fallback mode (probe init failure or mid-stream sampling failure).
FailedC() <-chan struct{}
}
Sampler is the read-only surface the limiter consumes from a load-probe sampler. The concrete *loadprobe.Sampler satisfies it; tests can substitute a fake to drive the state machine deterministically.