Documentation ¶
Overview ¶
Package flow implements the flow shaping control.
flow module is based on QPS statistic metric
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 ¶
- Constants
- Variables
- func ClearRules() error
- func ClearRulesOfResource(res string) error
- func IsValidRule(rule *Rule) error
- func LoadRules(rules []*Rule) (bool, error)
- func LoadRulesOfResource(res string, rules []*Rule) (bool, error)
- func RemoveTrafficShapingGenerator(tokenCalculateStrategy TokenCalculateStrategy, controlBehavior ControlBehavior) error
- func SetTrafficShapingGenerator(tokenCalculateStrategy TokenCalculateStrategy, controlBehavior ControlBehavior, ...) error
- type ControlBehavior
- type DirectTrafficShapingCalculator
- type MemoryAdaptiveTrafficShapingCalculator
- type RejectTrafficShapingChecker
- type RelationStrategy
- type Rule
- type Slot
- type StandaloneStatSlot
- type ThrottlingChecker
- type TokenCalculateStrategy
- type TrafficControllerGenFunc
- type TrafficControllerMap
- type TrafficShapingCalculator
- type TrafficShapingChecker
- type TrafficShapingController
- func (t *TrafficShapingController) BoundRule() *Rule
- func (t *TrafficShapingController) FlowCalculator() TrafficShapingCalculator
- func (t *TrafficShapingController) FlowChecker() TrafficShapingChecker
- func (t *TrafficShapingController) PerformChecking(resStat base.StatNode, batchCount uint32, flag int32) *base.TokenResult
- type WarmUpTrafficShapingCalculator
Constants ¶
const ( BlockMsgQueueing = "flow throttling check blocked, estimated queueing time exceeds max queueing time" MillisToNanosOffset = int64(time.Millisecond / time.Nanosecond) )
const (
RuleCheckSlotOrder = 2000
)
const (
StatSlotOrder = 3000
)
Variables ¶
var (
DefaultSlot = &Slot{}
)
var (
DefaultStandaloneStatSlot = &StandaloneStatSlot{}
)
Functions ¶
func ClearRulesOfResource ¶
ClearRulesOfResource clears resource level rules in flow module.
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. the first returned value indicates whether do real load operation, if the rules is the same with previous rules, return false
func LoadRulesOfResource ¶
LoadRulesOfResource loads the given resource's flow rules to the rule manager, while all previous resource's rules will be replaced. the first returned value indicates whether do real load operation, if the rules is the same with previous resource's rules, return false
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
ControlBehavior defines the behavior when requests have reached the capacity of the resource.
const ( Reject ControlBehavior = iota // Throttling indicates that pending requests will be throttled, wait in queue (until free capacity is available) Throttling )
func (ControlBehavior) String ¶
func (s ControlBehavior) String() string
type DirectTrafficShapingCalculator ¶
type DirectTrafficShapingCalculator struct {
// contains filtered or unexported fields
}
func NewDirectTrafficShapingCalculator ¶
func NewDirectTrafficShapingCalculator(owner *TrafficShapingController, threshold float64) *DirectTrafficShapingCalculator
func (*DirectTrafficShapingCalculator) BoundOwner ¶
func (d *DirectTrafficShapingCalculator) BoundOwner() *TrafficShapingController
func (*DirectTrafficShapingCalculator) CalculateAllowedTokens ¶
func (d *DirectTrafficShapingCalculator) CalculateAllowedTokens(uint32, int32) float64
type MemoryAdaptiveTrafficShapingCalculator ¶
type MemoryAdaptiveTrafficShapingCalculator struct {
// contains filtered or unexported fields
}
MemoryAdaptiveTrafficShapingCalculator is a memory adaptive traffic shaping calculator
adaptive flow control algorithm If the watermark is less than Rule.MemLowWaterMarkBytes, the threshold is Rule.LowMemUsageThreshold. If the watermark is greater than Rule.MemHighWaterMarkBytes, the threshold is Rule.HighMemUsageThreshold. Otherwise, the threshold is ((watermark - MemLowWaterMarkBytes)/(MemHighWaterMarkBytes - MemLowWaterMarkBytes)) *
(HighMemUsageThreshold - LowMemUsageThreshold) + LowMemUsageThreshold.
func NewMemoryAdaptiveTrafficShapingCalculator ¶
func NewMemoryAdaptiveTrafficShapingCalculator(owner *TrafficShapingController, r *Rule) *MemoryAdaptiveTrafficShapingCalculator
func (*MemoryAdaptiveTrafficShapingCalculator) BoundOwner ¶
func (m *MemoryAdaptiveTrafficShapingCalculator) BoundOwner() *TrafficShapingController
func (*MemoryAdaptiveTrafficShapingCalculator) CalculateAllowedTokens ¶
func (m *MemoryAdaptiveTrafficShapingCalculator) CalculateAllowedTokens(_ uint32, _ int32) float64
type RejectTrafficShapingChecker ¶
type RejectTrafficShapingChecker struct {
// contains filtered or unexported fields
}
func NewRejectTrafficShapingChecker ¶
func NewRejectTrafficShapingChecker(owner *TrafficShapingController, rule *Rule) *RejectTrafficShapingChecker
func (*RejectTrafficShapingChecker) BoundOwner ¶
func (d *RejectTrafficShapingChecker) BoundOwner() *TrafficShapingController
func (*RejectTrafficShapingChecker) DoCheck ¶
func (d *RejectTrafficShapingChecker) DoCheck(resStat base.StatNode, batchCount uint32, threshold float64) *base.TokenResult
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 string `json:"id,omitempty"` // Resource represents the resource name. Resource string `json:"resource"` TokenCalculateStrategy TokenCalculateStrategy `json:"tokenCalculateStrategy"` ControlBehavior ControlBehavior `json:"controlBehavior"` // Threshold means the threshold during StatIntervalInMs // If StatIntervalInMs is 1000(1 second), Threshold means QPS Threshold float64 `json:"threshold"` RelationStrategy RelationStrategy `json:"relationStrategy"` RefResource string `json:"refResource"` // MaxQueueingTimeMs only takes effect when ControlBehavior is Throttling. // When MaxQueueingTimeMs is 0, it means Throttling only controls interval of requests, // and requests exceeding the threshold will be rejected directly. MaxQueueingTimeMs uint32 `json:"maxQueueingTimeMs"` WarmUpPeriodSec uint32 `json:"warmUpPeriodSec"` WarmUpColdFactor uint32 `json:"warmUpColdFactor"` // StatIntervalInMs indicates the statistic interval and it's the optional setting for flow Rule. // If user doesn't set StatIntervalInMs, that means using default metric statistic of resource. // If the StatIntervalInMs user specifies can not reuse the global statistic of resource, // sentinel will generate independent statistic structure for this rule. StatIntervalInMs uint32 `json:"statIntervalInMs"` // adaptive flow control algorithm related parameters // limitation: LowMemUsageThreshold > HighMemUsageThreshold && MemHighWaterMarkBytes > MemLowWaterMarkBytes // if the current memory usage is less than or equals to MemLowWaterMarkBytes, threshold == LowMemUsageThreshold // if the current memory usage is more than or equals to MemHighWaterMarkBytes, threshold == HighMemUsageThreshold // if the current memory usage is in (MemLowWaterMarkBytes, MemHighWaterMarkBytes), threshold is in (HighMemUsageThreshold, LowMemUsageThreshold) LowMemUsageThreshold int64 `json:"lowMemUsageThreshold"` HighMemUsageThreshold int64 `json:"highMemUsageThreshold"` MemLowWaterMarkBytes int64 `json:"memLowWaterMarkBytes"` MemHighWaterMarkBytes int64 `json:"memHighWaterMarkBytes"` }
Rule describes the strategy of flow control, the flow control strategy is based on QPS statistic metric
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 GetRulesOfResource ¶
GetRulesOfResource returns specific resource's 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 StandaloneStatSlot ¶
type StandaloneStatSlot struct { }
func (StandaloneStatSlot) OnCompleted ¶
func (s StandaloneStatSlot) OnCompleted(ctx *base.EntryContext)
func (StandaloneStatSlot) OnEntryBlocked ¶
func (s StandaloneStatSlot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError)
func (StandaloneStatSlot) OnEntryPassed ¶
func (s StandaloneStatSlot) OnEntryPassed(ctx *base.EntryContext)
func (*StandaloneStatSlot) Order ¶
func (s *StandaloneStatSlot) Order() uint32
type ThrottlingChecker ¶
type ThrottlingChecker struct {
// contains filtered or unexported fields
}
ThrottlingChecker limits the time interval between two requests.
func NewThrottlingChecker ¶
func NewThrottlingChecker(owner *TrafficShapingController, timeoutMs uint32, statIntervalMs uint32) *ThrottlingChecker
func (*ThrottlingChecker) BoundOwner ¶
func (c *ThrottlingChecker) BoundOwner() *TrafficShapingController
func (*ThrottlingChecker) DoCheck ¶
func (c *ThrottlingChecker) DoCheck(_ base.StatNode, batchCount uint32, threshold float64) *base.TokenResult
type TokenCalculateStrategy ¶
type TokenCalculateStrategy int32
const ( Direct TokenCalculateStrategy = iota WarmUp MemoryAdaptive )
func (TokenCalculateStrategy) String ¶
func (s TokenCalculateStrategy) String() string
type TrafficControllerGenFunc ¶
type TrafficControllerGenFunc func(*Rule, *standaloneStatistic) (*TrafficShapingController, error)
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 { BoundOwner() *TrafficShapingController CalculateAllowedTokens(batchCount uint32, flag int32) float64 }
TrafficShapingCalculator calculates the actual traffic shaping threshold based on the threshold of rule and the traffic shaping strategy.
func NewWarmUpTrafficShapingCalculator ¶
func NewWarmUpTrafficShapingCalculator(owner *TrafficShapingController, rule *Rule) TrafficShapingCalculator
type TrafficShapingChecker ¶
type TrafficShapingChecker interface { BoundOwner() *TrafficShapingController DoCheck(resStat base.StatNode, batchCount 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(rule *Rule, boundStat *standaloneStatistic) (*TrafficShapingController, error)
func (*TrafficShapingController) BoundRule ¶
func (t *TrafficShapingController) BoundRule() *Rule
func (*TrafficShapingController) FlowCalculator ¶
func (t *TrafficShapingController) FlowCalculator() TrafficShapingCalculator
func (*TrafficShapingController) FlowChecker ¶
func (t *TrafficShapingController) FlowChecker() TrafficShapingChecker
func (*TrafficShapingController) PerformChecking ¶
func (t *TrafficShapingController) PerformChecking(resStat base.StatNode, batchCount uint32, flag int32) *base.TokenResult
type WarmUpTrafficShapingCalculator ¶
type WarmUpTrafficShapingCalculator struct {
// contains filtered or unexported fields
}
func (*WarmUpTrafficShapingCalculator) BoundOwner ¶
func (c *WarmUpTrafficShapingCalculator) BoundOwner() *TrafficShapingController
func (*WarmUpTrafficShapingCalculator) CalculateAllowedTokens ¶
func (c *WarmUpTrafficShapingCalculator) CalculateAllowedTokens(_ uint32, _ int32) float64