Documentation
¶
Overview ¶
Discipline that used to limits the speed of passing data elements from the input channel to the output channel.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrConvertedIntervalZero = errors.New("converted interval is zero") ErrConvertedQuantityZero = errors.New("converted quantity is zero") ErrIntervalZeroNegative = errors.New("interval is zero or negative") ErrMinimumIntervalNegative = errors.New("minimum interval is negative") ErrQuantityZero = errors.New("quantity is zero") )
var (
ErrInputEmpty = errors.New("input channel was not specified")
)
Functions ¶
This section is empty.
Types ¶
type Discipline ¶
type Discipline[Type any] struct { // contains filtered or unexported fields }
Limit discipline.
Example ¶
package main import ( "fmt" "time" "github.com/akramarenkov/cqos/v2/limit" ) func main() { quantity := 10 input := make(chan int) opts := limit.Opts[int]{ Input: input, Limit: limit.Rate{ Interval: time.Second, Quantity: 1, }, } discipline, err := limit.New(opts) if err != nil { panic(err) } outSequence := make([]int, 0, quantity) startedAt := time.Now() go func() { defer close(input) for stage := 1; stage <= quantity; stage++ { input <- stage } }() for item := range discipline.Output() { outSequence = append(outSequence, item) } duration := time.Since(startedAt) expected := (time.Duration(quantity) * opts.Limit.Interval) / time.Duration(opts.Limit.Quantity) deviation := 0.01 fmt.Println(duration <= time.Duration(float64(expected)*(1.0+deviation))) fmt.Println(duration >= time.Duration(float64(expected)*(1.0-deviation))) fmt.Println(outSequence) }
Output: true true [1 2 3 4 5 6 7 8 9 10]
func New ¶
func New[Type any](opts Opts[Type]) (*Discipline[Type], error)
Creates and runs discipline.
func (*Discipline[Type]) Output ¶
func (dsc *Discipline[Type]) Output() <-chan Type
Returns output channel.
If this channel is closed, it means that the discipline is terminated.
type Opts ¶
type Opts[Type any] struct { // Input data channel. For terminate discipline it is necessary and sufficient to // close the input channel Input <-chan Type // Rate limit Limit Rate }
Options of the created discipline.
type Rate ¶
Quantity data elements per time Interval.
func (Rate) Flatten ¶
Recalculates the units of measurement of the Interval so that the Quantity is equal to 1.
Maximizes the uniformity of the distribution of data elements over time by reducing the productivity of the discipline.
func (Rate) IsValid ¶
Validates field values.
Interval cannot be negative or equal to zero.
Quantity cannot be equal to zero.
func (Rate) Optimize ¶
Recalculates the units of measurement of the interval so that the Quantity is as small as possible but the Interval is not less than the recommended value.
Increases the uniformity of the distribution of data elements over time, almost without reducing the productivity of the discipline.