Documentation
¶
Overview ¶
Package dwrr implements a cost-aware Deficit Weighted Round Robin scheduler. Quantum and item costs are positive uint64 values. Scheduling order is determined by queue visits and FIFO heads, not by downstream execution time.
Index ¶
- Constants
- Variables
- type DWRR
- func (d *DWRR[T]) AddQueue() uint
- func (d *DWRR[T]) DrainQueue(queue uint) []Item[T]
- func (d *DWRR[T]) Enqueue(queue uint, items []Item[T]) error
- func (d *DWRR[T]) RemoveHead(queue uint) (Item[T], bool)
- func (d *DWRR[T]) RemoveQueue()
- func (d *DWRR[T]) SetQuantum(queue uint, quantum uint64) error
- func (d *DWRR[T]) Take(n int) []Scheduled[T]
- type Item
- type Scheduled
Constants ¶
const ( // MaxCost is the largest accepted item cost. MaxCost = uint64(1<<63 - 1) // MaxQuantum is the largest accepted queue quantum. MaxQuantum = uint64(1<<63 - 1) )
Variables ¶
var ( // ErrZeroQuantum is returned when SetQuantum is given a zero quantum. ErrZeroQuantum = errors.New("dwrr: quantum must be positive") // ErrZeroCost is returned when Enqueue receives an item with zero cost. ErrZeroCost = errors.New("dwrr: item cost must be positive") // ErrCostTooLarge is returned when Enqueue receives a cost above MaxCost. ErrCostTooLarge = errors.New("dwrr: item cost exceeds MaxCost") // ErrQuantumTooLarge is returned when SetQuantum receives a value above MaxQuantum. ErrQuantumTooLarge = errors.New("dwrr: quantum exceeds MaxQuantum") )
Functions ¶
This section is empty.
Types ¶
type DWRR ¶
type DWRR[T any] struct { // contains filtered or unexported fields }
DWRR is a concurrent-safe cost-aware deficit round-robin scheduler. Do not copy it after first use. Its zero value is unusable; use NewDWRR.
func NewDWRR ¶
NewDWRR creates count empty queues, each with quantum 1. Queue indices start at zero and remain stable while their queues exist; AddQueue and RemoveQueue change the count, with RemoveQueue affecting only the last queue.
func (*DWRR[T]) AddQueue ¶
AddQueue appends an empty queue with quantum 1 and returns its index. The index is stable while the queue exists; if the last queue is removed, a later AddQueue may reuse its index.
func (*DWRR[T]) DrainQueue ¶
DrainQueue removes and returns a copy of every queued item, resets deficit, and cancels an active visit. Invalid indices panic.
func (*DWRR[T]) Enqueue ¶
Enqueue validates the complete input before appending it atomically. New items join an active visit at the queue tail. Item records, including costs, are copied; values of T are shallow-copied. Zero cost returns ErrZeroCost; a cost above MaxCost returns ErrCostTooLarge. Invalid queue indices panic.
func (*DWRR[T]) RemoveHead ¶
RemoveHead removes and returns a copy of the FIFO head. It returns the zero Item and false when empty. If items remain, deficit and active-visit state are preserved; emptying the queue resets deficit and cancels an active visit. Invalid indices panic.
func (*DWRR[T]) RemoveQueue ¶
func (d *DWRR[T]) RemoveQueue()
RemoveQueue removes the last queue and its contents. Existing queue indices remain stable. Removing the active queue cancels that visit; cursor is kept in range (or reset to zero when no queues remain). Empty removal is a no-op.
func (*DWRR[T]) SetQuantum ¶
SetQuantum changes a queue's quantum while keeping its deficit and any active visit. The new quantum is used at the next grant, not when a paused visit resumes. Setting the current quantum is a no-op. Zero quantum returns ErrZeroQuantum; a quantum above MaxQuantum returns ErrQuantumTooLarge. Both errors leave state unchanged. Invalid indices panic first.
func (*DWRR[T]) Take ¶
Take returns up to n items in service order. A limit can pause an active visit; its credit and position persist for the next call. Take(0) returns a nonnil empty slice without changing state. Negative n panics. The returned slice is always nonnil and never aliases internal queue storage.