Documentation ¶
Overview ¶
Package flow implements the flow shaping control.
flow module supports two statistic metric: QPS and Concurrency.
The TrafficShapingController consists of two part: TrafficShapingCalculator and TrafficShapingChecker
- TrafficShapingCalculator calculates the actual traffic shaping token threshold. Currently, Sentinel supports two token calculate strategy: Direct and WarmUp.
- TrafficShapingChecker performs checking logic according to current metrics and the traffic shaping strategy, then yield the token result. Currently, Sentinel supports two control behavior: Reject and Throttling.
Besides, Sentinel supports customized TrafficShapingCalculator and TrafficShapingChecker. User could call function SetTrafficShapingGenerator to register customized TrafficShapingController and call function RemoveTrafficShapingGenerator to unregister TrafficShapingController. There are a few notes users need to be aware of:
- The function both SetTrafficShapingGenerator and RemoveTrafficShapingGenerator is not thread safe.
- Users can not override the Sentinel supported TrafficShapingController.
Index ¶
- func ClearRules() error
- func IsValidRule(rule *Rule) error
- func LoadRules(rules []*Rule) (bool, error)
- func RemoveTrafficShapingGenerator(tokenCalculateStrategy TokenCalculateStrategy, controlBehavior ControlBehavior) error
- func SetTrafficShapingGenerator(tokenCalculateStrategy TokenCalculateStrategy, controlBehavior ControlBehavior, ...) error
- type ControlBehavior
- type DefaultTrafficShapingChecker
- type DirectTrafficShapingCalculator
- type MetricType
- type RelationStrategy
- type Rule
- type Slot
- type ThrottlingChecker
- type TokenCalculateStrategy
- type TrafficControllerGenFunc
- type TrafficControllerMap
- type TrafficShapingCalculator
- type TrafficShapingChecker
- type TrafficShapingController
- func (t *TrafficShapingController) FlowCalculator() TrafficShapingCalculator
- func (t *TrafficShapingController) FlowChecker() TrafficShapingChecker
- func (t *TrafficShapingController) PerformChecking(node base.StatNode, acquireCount uint32, flag int32) *base.TokenResult
- func (t *TrafficShapingController) Rule() *Rule
- type WarmUpTrafficShapingCalculator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsValidRule ¶
IsValidRule checks whether the given Rule is valid.
func LoadRules ¶
LoadRules loads the given flow rules to the rule manager, while all previous rules will be replaced.
func RemoveTrafficShapingGenerator ¶
func RemoveTrafficShapingGenerator(tokenCalculateStrategy TokenCalculateStrategy, controlBehavior ControlBehavior) error
func SetTrafficShapingGenerator ¶
func SetTrafficShapingGenerator(tokenCalculateStrategy TokenCalculateStrategy, controlBehavior ControlBehavior, generator TrafficControllerGenFunc) error
SetTrafficShapingGenerator sets the traffic controller generator for the given TokenCalculateStrategy and ControlBehavior. Note that modifying the generator of default control strategy is not allowed.
Types ¶
type ControlBehavior ¶
type ControlBehavior int32
const ( Reject ControlBehavior = iota Throttling )
func (ControlBehavior) String ¶
func (s ControlBehavior) String() string
type DefaultTrafficShapingChecker ¶
type DefaultTrafficShapingChecker struct {
// contains filtered or unexported fields
}
func NewDefaultTrafficShapingChecker ¶
func NewDefaultTrafficShapingChecker(rule *Rule) *DefaultTrafficShapingChecker
func (*DefaultTrafficShapingChecker) DoCheck ¶
func (d *DefaultTrafficShapingChecker) DoCheck(node base.StatNode, acquireCount uint32, threshold float64) *base.TokenResult
type DirectTrafficShapingCalculator ¶
type DirectTrafficShapingCalculator struct {
// contains filtered or unexported fields
}
func NewDirectTrafficShapingCalculator ¶
func NewDirectTrafficShapingCalculator(threshold float64) *DirectTrafficShapingCalculator
func (*DirectTrafficShapingCalculator) CalculateAllowedTokens ¶
type MetricType ¶
type MetricType int32
MetricType represents the target metric type.
const ( // Concurrency represents concurrency count. Concurrency MetricType = iota // QPS represents request count per second. QPS )
func (MetricType) String ¶
func (s MetricType) String() string
type RelationStrategy ¶
type RelationStrategy int32
RelationStrategy indicates the flow control strategy based on the relation of invocations.
const ( // CurrentResource means flow control by current resource directly. CurrentResource RelationStrategy = iota // AssociatedResource means flow control by the associated resource rather than current resource. AssociatedResource )
func (RelationStrategy) String ¶
func (s RelationStrategy) String() string
type Rule ¶
type Rule struct { // ID represents the unique ID of the rule (optional). ID uint64 `json:"id,omitempty"` // Resource represents the resource name. Resource string `json:"resource"` MetricType MetricType `json:"metricType"` TokenCalculateStrategy TokenCalculateStrategy `json:"tokenCalculateStrategy"` ControlBehavior ControlBehavior `json:"controlBehavior"` Count float64 `json:"count"` RelationStrategy RelationStrategy `json:"relationStrategy"` RefResource string `json:"refResource"` MaxQueueingTimeMs uint32 `json:"maxQueueingTimeMs"` WarmUpPeriodSec uint32 `json:"warmUpPeriodSec"` WarmUpColdFactor uint32 `json:"warmUpColdFactor"` }
Rule describes the strategy of flow control.
func GetResRules ¶
GetResRules returns specific resource's rules based on copy. It doesn't take effect for flow module if user changes the rule.
func GetRules ¶
func GetRules() []Rule
GetRules returns all the rules based on copy. It doesn't take effect for flow module if user changes the rule.
func (*Rule) ResourceName ¶
type Slot ¶
type Slot struct { }
func (*Slot) Check ¶
func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult
type ThrottlingChecker ¶
type ThrottlingChecker struct {
// contains filtered or unexported fields
}
ThrottlingChecker limits the time interval between two requests.
func NewThrottlingChecker ¶
func NewThrottlingChecker(timeoutMs uint32) *ThrottlingChecker
func (*ThrottlingChecker) DoCheck ¶
func (c *ThrottlingChecker) DoCheck(_ base.StatNode, acquireCount uint32, threshold float64) *base.TokenResult
type TokenCalculateStrategy ¶
type TokenCalculateStrategy int32
const ( Direct TokenCalculateStrategy = iota WarmUp )
func (TokenCalculateStrategy) String ¶
func (s TokenCalculateStrategy) String() string
type TrafficControllerGenFunc ¶
type TrafficControllerGenFunc func(*Rule) *TrafficShapingController
TrafficControllerGenFunc represents the TrafficShapingController generator function of a specific control behavior.
type TrafficControllerMap ¶
type TrafficControllerMap map[string][]*TrafficShapingController
TrafficControllerMap represents the map storage for TrafficShapingController.
type TrafficShapingCalculator ¶
type TrafficShapingCalculator interface {
CalculateAllowedTokens(node base.StatNode, acquireCount uint32, flag int32) float64
}
TrafficShapingCalculator calculates the actual traffic shaping threshold based on the threshold of rule and the traffic shaping strategy.
type TrafficShapingChecker ¶
type TrafficShapingChecker interface {
DoCheck(node base.StatNode, acquireCount uint32, threshold float64) *base.TokenResult
}
TrafficShapingChecker performs checking according to current metrics and the traffic shaping strategy, then yield the token result.
type TrafficShapingController ¶
type TrafficShapingController struct {
// contains filtered or unexported fields
}
func NewTrafficShapingController ¶
func NewTrafficShapingController(flowCalculator TrafficShapingCalculator, flowChecker TrafficShapingChecker, rule *Rule) *TrafficShapingController
NewTrafficShapingController creates a TrafficShapingController wrapped with the given checker and flow rule.
func (*TrafficShapingController) FlowCalculator ¶
func (t *TrafficShapingController) FlowCalculator() TrafficShapingCalculator
func (*TrafficShapingController) FlowChecker ¶
func (t *TrafficShapingController) FlowChecker() TrafficShapingChecker
func (*TrafficShapingController) PerformChecking ¶
func (t *TrafficShapingController) PerformChecking(node base.StatNode, acquireCount uint32, flag int32) *base.TokenResult
func (*TrafficShapingController) Rule ¶
func (t *TrafficShapingController) Rule() *Rule
type WarmUpTrafficShapingCalculator ¶
type WarmUpTrafficShapingCalculator struct {
// contains filtered or unexported fields
}
func NewWarmUpTrafficShapingCalculator ¶
func NewWarmUpTrafficShapingCalculator(rule *Rule) *WarmUpTrafficShapingCalculator