Documentation
¶
Index ¶
- Constants
- func ClassificationToContext(ctx context.Context, class Classification) context.Context
- type Capacity
- type CapacityConcurrency
- type CapacityErrorRate
- type CapacityLandingRate
- type CapacityLatency
- type CapacityThrottle
- type CapacityWaitGroup
- type Classification
- type Classifier
- type ClassifierFN
- type Curve
- type CurveFN
- type CurveLinear
- type ErrRejection
- type FailureProbability
- type FailureProbabilityCurve
- type Fn
- type LatencyReduction
- type OptionConcurrency
- type OptionErrorRate
- func OptionErrorRateBucketDuration(d time.Duration) OptionErrorRate
- func OptionErrorRateBucketSizeHint(size int) OptionErrorRate
- func OptionErrorRateMinimumPoints(min int) OptionErrorRate
- func OptionErrorRateName(name string) OptionErrorRate
- func OptionErrorRateWindowBuckets(count int) OptionErrorRate
- type OptionLandingRate
- type OptionLatency
- func OptionLatencyBucketDuration(d time.Duration) OptionLatency
- func OptionLatencyBucketSizeHint(size int) OptionLatency
- func OptionLatencyMeasurePanics(v bool) OptionLatency
- func OptionLatencyMinimumPoints(min int) OptionLatency
- func OptionLatencyName(name string) OptionLatency
- func OptionLatencyReduction(r LatencyReduction) OptionLatency
- func OptionLatencyWindowBuckets(count int) OptionLatency
- type OptionShedder
- type RejectionRate
- type RejectionRateCurve
- func NewRejectionRateCurveByClassification(probability FailureProbability, defaultCurve Curve, ...) *RejectionRateCurve
- func NewRejectionRateCurveIdentity(probability FailureProbability) *RejectionRateCurve
- func NewRejectionRateCurveLinear(probability FailureProbability, lower float32, upper float32, exponent float32) *RejectionRateCurve
- type Rule
- type Shedder
- type Wrapper
Constants ¶
const RuleProbabilistic string = "PROBABILISTIC"
RuleProbabilistic is the name of the rejection rule that covers all cases of using a rejection rate rather than a deterministic rule.
Variables ¶
This section is empty.
Functions ¶
func ClassificationToContext ¶
func ClassificationToContext(ctx context.Context, class Classification) context.Context
Types ¶
type Capacity ¶
type Capacity interface { // Name of the capacity or metric being tracked. Name(ctx context.Context) string // Usage as a percent value. This should report value between 0 and 1 but // some implementations may intentionally report negative or greater 100% // values if needed. Usage(ctx context.Context) float32 }
Capacity represents usage of some finite resource.
type CapacityConcurrency ¶
type CapacityConcurrency struct {
// contains filtered or unexported fields
}
CapacityConcurrency tracks the number of concurrent calls to a method.
func NewCapacityConcurrency ¶
func NewCapacityConcurrency(limit int32, options ...OptionConcurrency) *CapacityConcurrency
func (*CapacityConcurrency) Add ¶
func (self *CapacityConcurrency) Add(count int32)
func (*CapacityConcurrency) Done ¶
func (self *CapacityConcurrency) Done(count int32)
func (*CapacityConcurrency) Name ¶
func (self *CapacityConcurrency) Name(ctx context.Context) string
func (*CapacityConcurrency) Usage ¶
func (self *CapacityConcurrency) Usage(ctx context.Context) float32
Aggregate returns the current concurrency value.
func (*CapacityConcurrency) Wrap ¶
func (self *CapacityConcurrency) Wrap(fn Fn) Fn
Wrap a function in concurrency tracking.
type CapacityErrorRate ¶
type CapacityErrorRate struct {
// contains filtered or unexported fields
}
CapacityErrorRate calculates the percent error of invocations within a winow of time. Returned errors and panics are both considered in the rate calculation.
Attempts and errors are always recorded within the same bucket of the window. The rate is then calculated as (errors / attempts) within the window. The current rate is given as the current capacity usage value.
The rate calculation is based on a rolling window. The default size of the window is 1s with each bucket representing 10ms. Both of these values can be modified using constructor options.
func NewCapacityErrorRate ¶
func NewCapacityErrorRate(options ...OptionErrorRate) *CapacityErrorRate
func (*CapacityErrorRate) Wrap ¶
func (self *CapacityErrorRate) Wrap(fn Fn) Fn
type CapacityLandingRate ¶
type CapacityLandingRate struct {
// contains filtered or unexported fields
}
CapacityLandingRate considers the number of method invocations within a window of time that have begun. Note that this counts all attempts to invoke a method and does not distinguish success or failure.
The rate calculation is based on a rolling window. The default size of the window is 1s with each bucket representing 10ms. Both of these values can be modified using constructor options.
func NewCapacityLandingRate ¶
func NewCapacityLandingRate(limit int, options ...OptionLandingRate) *CapacityLandingRate
func (*CapacityLandingRate) Usage ¶
func (self *CapacityLandingRate) Usage(ctx context.Context) float32
func (*CapacityLandingRate) Wrap ¶
func (self *CapacityLandingRate) Wrap(fn Fn) Fn
type CapacityLatency ¶
type CapacityLatency struct {
// contains filtered or unexported fields
}
CapacityLatency calculates the execution time of method invocations within a winow of time.
By default, latency is calculated by taking an average of method invocation time within the window. You can provide an alternative calculation using the OptionCapacityLatencyReduction option.
The latency calculation is based on a rolling window. The default size of the window is 1s with each bucket representing 10ms. Both of these values can be modified using constructor options.
func NewCapacityLatency ¶
func NewCapacityLatency(limit time.Duration, options ...OptionLatency) *CapacityLatency
NewCapacityLatency defaults to installing an average function for the window reduction. Use the AggregatorLatencyOptionReduction modifier to set a different reduction.
func (*CapacityLatency) Append ¶
func (self *CapacityLatency) Append(ctx context.Context, v time.Duration)
Append adds a latency measure to the underlying window.
func (*CapacityLatency) Wrap ¶
func (self *CapacityLatency) Wrap(fn Fn) Fn
type CapacityThrottle ¶
type CapacityThrottle struct { Capacity // contains filtered or unexported fields }
CapacityThrottle is a wrapper for other Capacity implementations that limits the number of times the underlying usage is calculated within a period of time. This exists to help amortize the cost of expensive capacity calculations when it is safe or desireable to do so.
func NewCapacityThrottle ¶
func NewCapacityThrottle(wrapped Capacity, duration time.Duration) *CapacityThrottle
func (*CapacityThrottle) Usage ¶
func (self *CapacityThrottle) Usage(ctx context.Context) float32
Usage returns from an internal cache until a duration has expired at which point it calls the wrapped Capacity to get a new value.
func (*CapacityThrottle) Wrap ¶
func (self *CapacityThrottle) Wrap(fn Fn) Fn
type CapacityWaitGroup ¶
type CapacityWaitGroup struct { *CapacityConcurrency // contains filtered or unexported fields }
CapacityWaitGroup combines a CapacityConcurrency and a sync.WaitGroup. This type may be used in place of a wait group and can satisfy an interface matching the wait group's methods. Each delta given to Add() increases the reported concurrency count and each call to Done() decreases the count.
func NewCapacityWaitGroup ¶
func NewCapacityWaitGroup(limit int32, options ...OptionConcurrency) *CapacityWaitGroup
func (*CapacityWaitGroup) Add ¶
func (self *CapacityWaitGroup) Add(delta int)
func (*CapacityWaitGroup) Done ¶
func (self *CapacityWaitGroup) Done()
func (*CapacityWaitGroup) Wait ¶
func (self *CapacityWaitGroup) Wait()
type Classification ¶
type Classification string
Classification is an arbitrary class or category assigned to method invocations. The most common use of this type is to define priorities that can then be mapped to specific load shedding policies. For example, you might define LOW, NORMAL, HIGH, and CRITICAL as classifications and have LOW shed at a higher rate than NORMAL, etc.
func ClassificationFromContext ¶
func ClassificationFromContext(ctx context.Context) Classification
type Classifier ¶
type Classifier interface {
Classify(ctx context.Context) Classification
}
Classifier determines the classification of a method invocation.
type ClassifierFN ¶
type ClassifierFN func(ctx context.Context) Classification
ClassifierFN is an adapter for simple classification functions. For example:
ClassifierFN(func(ctx context.Context) Classification { requestPath := PathFromContext(ctx) if requestPath == "/important/api" { return Classification("HIGH") } return Classification("Normal") })
func (ClassifierFN) Classify ¶
func (self ClassifierFN) Classify(ctx context.Context) Classification
type Curve ¶
Curve is a function used to scale or plot a value. The primary use cases for a curving function is to either translate a capacity usage to a failure probability or translate a failure probability to a rejection rate. The general expectation is that most inputs and outputes are between the values 0 and 1, though specialized use cases may handle arbitrary values.
type CurveFN ¶
CurveFN is an adapter for simple curving functions. For example:
CurveFN(func(ctx context.Context, value float32) float32 { return 0.0 })
type CurveLinear ¶
CurveLinear calculates shifts the input based on the following formula:
f(x) = x < LOWER ? 0 : x > UPPER ? 1 : ((x - LOWER) / (UPPER - LOWER) )^EXPONENT
The result is a linear interpolation of the usage value between the upper and lower limits, optionally modified by some exponent.
type ErrRejection ¶
type ErrRejection struct { // Rule matches the name of any deterministic rule or the value of // RuleProbabilistic if a rejection rate was used. Rule string // Classification optionally contains the invocations classification value // if one is set. It is otherwise empty. Classification Classification // Name is present when Rule matches RuleProbabilistic and contains the // name of the RejectionRate/FailureProbability/Capacity used to make the // decision Name string // Usage is present when Rule matches RuleProbabilistic and contains the // current capacity utilization. Usage float32 // Likelihood is present when Rule matches RuleProbabilistic and contains // the current likelihood of failure due to the capacity utilization. Likelihood float32 // Rate is present when Rule matches RuleProbabilistic and contains the // current rejection rate as derived from the probability of failure. Rate float32 }
ErrRejection provides the details on why an invocation was rejected.
func (ErrRejection) Error ¶
func (self ErrRejection) Error() string
type FailureProbability ¶
type FailureProbability interface { Capacity // Likelihood computes a chance of either system or action failure based on // the current capacity usage. Values a percentage and should be bounded // between 0 and 1. Greater than 100% probability of failure is not // particularly meaningful but may have use in some specific scenarios. Likelihood(ctx context.Context) float32 }
FailureProbability represents the chance of failure based on capacity usage.
Implementations of FailureProbability that wrap or otherwise do not directly implement Capacity must account for the wrapped Capacity's optional Wrapper interface.
type FailureProbabilityCurve ¶
func NewFailureProbabilityCurveIdentity ¶
func NewFailureProbabilityCurveIdentity(cap Capacity) *FailureProbabilityCurve
NewFailureProbabilityCurveIdentity generates a probability that returns the underlying capacity value without modifying it.
func NewFailureProbabilityCurveLinear ¶
func NewFailureProbabilityCurveLinear(cap Capacity, lower float32, upper float32, exponent float32) *FailureProbabilityCurve
NewFailureProbabilityCurveLinear generates a curving probability that uses the linear interpolation curve from CurveLinear.
func (*FailureProbabilityCurve) Likelihood ¶
func (self *FailureProbabilityCurve) Likelihood(ctx context.Context) float32
func (*FailureProbabilityCurve) Wrap ¶
func (self *FailureProbabilityCurve) Wrap(fn Fn) Fn
type LatencyReduction ¶
type OptionConcurrency ¶
type OptionConcurrency func(*CapacityConcurrency)
func OptionConcurrencyName ¶
func OptionConcurrencyName(name string) OptionConcurrency
type OptionErrorRate ¶
type OptionErrorRate func(*CapacityErrorRate)
func OptionErrorRateBucketDuration ¶
func OptionErrorRateBucketDuration(d time.Duration) OptionErrorRate
func OptionErrorRateBucketSizeHint ¶
func OptionErrorRateBucketSizeHint(size int) OptionErrorRate
func OptionErrorRateMinimumPoints ¶
func OptionErrorRateMinimumPoints(min int) OptionErrorRate
func OptionErrorRateName ¶
func OptionErrorRateName(name string) OptionErrorRate
func OptionErrorRateWindowBuckets ¶
func OptionErrorRateWindowBuckets(count int) OptionErrorRate
type OptionLandingRate ¶
type OptionLandingRate func(*CapacityLandingRate)
func OptionLandingRateBucketDuration ¶
func OptionLandingRateBucketDuration(d time.Duration) OptionLandingRate
func OptionLandingRateBucketSizeHint ¶
func OptionLandingRateBucketSizeHint(size int) OptionLandingRate
func OptionLandingRateWindowBuckets ¶
func OptionLandingRateWindowBuckets(count int) OptionLandingRate
func OptionLandingrateName ¶
func OptionLandingrateName(name string) OptionLandingRate
type OptionLatency ¶
type OptionLatency func(*CapacityLatency)
func OptionLatencyBucketDuration ¶
func OptionLatencyBucketDuration(d time.Duration) OptionLatency
func OptionLatencyBucketSizeHint ¶
func OptionLatencyBucketSizeHint(size int) OptionLatency
func OptionLatencyMeasurePanics ¶
func OptionLatencyMeasurePanics(v bool) OptionLatency
OptionLatencyMeasurePanics modifies the capacity to capture latency for executions that resulted in a panic in addition to executions that exit normally. The default value is false.
func OptionLatencyMinimumPoints ¶
func OptionLatencyMinimumPoints(min int) OptionLatency
func OptionLatencyName ¶
func OptionLatencyName(name string) OptionLatency
func OptionLatencyReduction ¶
func OptionLatencyReduction(r LatencyReduction) OptionLatency
OptionLatencyReduction sets the reduction method used when calculating usage value of the window of data. The default value is an average function.
func OptionLatencyWindowBuckets ¶
func OptionLatencyWindowBuckets(count int) OptionLatency
type OptionShedder ¶
type OptionShedder func(*Shedder)
func OptionShedderClassifier ¶
func OptionShedderClassifier(c Classifier) OptionShedder
func OptionShedderRandom ¶
func OptionShedderRandom(r func() float32) OptionShedder
func OptionShedderRejectionRate ¶
func OptionShedderRejectionRate(r RejectionRate) OptionShedder
func OptionShedderRule ¶
func OptionShedderRule(r Rule) OptionShedder
type RejectionRate ¶
type RejectionRate interface { FailureProbability // Rate compute the percentage of load to shed based on the current failure // probability. Outputs are expected to be percentage values between 0 and // 1. Values outside of this range may result in unexpected behavior. Rate(ctx context.Context) float32 }
RejectionRate represents the amount of load that should be shed based on the current failure probability.
Implementations of RejectionRate that wrap or otherwise do not directly implement FailureProbability must account for the wrapped FailureProbability's optional Wrapper interface.
type RejectionRateCurve ¶
type RejectionRateCurve struct { FailureProbability // contains filtered or unexported fields }
func NewRejectionRateCurveByClassification ¶
func NewRejectionRateCurveByClassification(probability FailureProbability, defaultCurve Curve, classes map[Classification]Curve) *RejectionRateCurve
NewRejectionRateCurveByClassification allows for the failure probability to be translated to a rejection rate based on the classification of an invocation. For example, LOW priority requests can have a higher rejection rate for the same failure probability compared to HIGH priority.
func NewRejectionRateCurveIdentity ¶
func NewRejectionRateCurveIdentity(probability FailureProbability) *RejectionRateCurve
NewRejectionRateCurveIdentity generates a rejection rate that returns the underlying failure probability value without modifying it.
func NewRejectionRateCurveLinear ¶
func NewRejectionRateCurveLinear(probability FailureProbability, lower float32, upper float32, exponent float32) *RejectionRateCurve
NewRejectionRateCurveLinear generates a curving rejection rate that uses the linear interpolation curve from CurveLinear.
func (*RejectionRateCurve) Wrap ¶
func (self *RejectionRateCurve) Wrap(fn Fn) Fn
type Rule ¶
Rule represents a deterministic load shedding decision. Unlike RejectionRate, a Rule does not incorporate randomness or probability.
Rules can represent virtually any kind of deterministic behavior. For example, rules may be used to integrate rate limiting or quota management policies into the load shedding framework. Rules also do not have to be static. They reference dynamic variables and consult external systems.
type Shedder ¶
type Shedder struct {
// contains filtered or unexported fields
}
Shedder encapsulates a series of load shedding policies and applies them to method invocations.
The primary usage of the Shedder is intended to be the Do method which applies all load shedding rules and rejection rates.
func NewShedder ¶
func NewShedder(options ...OptionShedder) *Shedder
func (*Shedder) Do ¶
Do optionally runs the function based on the current state of the load shedding policy configured for the Shedder. In the event that the function is not executed the Shedder will return an ErrRejection.
All invocation monitoring wrappers are applied for the Fn is executed. If a Classifier is provided then the current classification is added to the context before any other action.
func (*Shedder) Select ¶
Select performs the decision making process for the load shedder and optionally returns an error indicating that an action should be rejected. The returned error, if not nil, is always an ErrRejected instance. This may be used in custom load shedding integrations.
Note that the context given must be the same context that would otherwise be given to Do. Also note that Select does not apply any Fn wrapping or classification so any invocation monitoring, metrics management, and classification must be performed externally.
func (*Shedder) WrapSelect ¶
WrapSelect returns a wrapped version of Fn that both applies any invocation monitoring required by rejection rate calculators and applies load shedding rules.
This differs from the Do method by returning a re-usable Fn. Most usage of the shedder should be through the Do method but this method is provided for specialized cases where the input parameters for the Fn do not change with each invocation. This allows Fn to be called repeatedly without needing to be re-wrapped on each invocation.
type Wrapper ¶
Wrapper is an optional interface that any Capcity and Probability may implement if they need to collect data from functions being executed. For example, if a Capcity needs to record the execution duration of all function executed within a load shedding policy then it can implement this interface by returning a wrapped copy of the passed in function that tracks the start and end times.
Implementing this behaviour is optional and this interface is only exposed for documentation purposes.