Documentation ¶
Overview ¶
Package hotspot provides implementation of "hot-spot" (frequent) parameter flow control.
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(cb ControlBehavior) error
- func SetTrafficShapingGenerator(cb ControlBehavior, generator TrafficControllerGenFunc) error
- type ConcurrencyStatSlot
- type ControlBehavior
- type MetricType
- type ParamsMetric
- type Rule
- type Slot
- type TrafficControllerGenFunc
- type TrafficShapingController
Constants ¶
const ( ConcurrencyMaxCount = 4000 ParamsCapacityBase = 4000 ParamsMaxCapacity = 20000 )
const (
RuleCheckSlotOrder = 4000
)
const (
StatSlotOrder = 4000
)
Variables ¶
var (
DefaultConcurrencyStatSlot = &ConcurrencyStatSlot{}
)
var (
DefaultSlot = &Slot{}
)
Functions ¶
func ClearRulesOfResource ¶ added in v1.0.3
ClearRulesOfResource clears resource level hotspot param flow rules.
func IsValidRule ¶
func LoadRules ¶
LoadRules replaces all old hotspot param flow rules with the given rules. Return value:
bool: indicates whether the internal map has been changed; error: indicates whether occurs the error.
func LoadRulesOfResource ¶ added in v1.0.3
LoadRulesOfResource loads the given resource's hotspot param 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(cb ControlBehavior) error
func SetTrafficShapingGenerator ¶
func SetTrafficShapingGenerator(cb ControlBehavior, generator TrafficControllerGenFunc) error
SetTrafficShapingGenerator sets the traffic controller generator for the given control behavior. Note that modifying the generator of default control behaviors is not allowed.
Types ¶
type ConcurrencyStatSlot ¶
type ConcurrencyStatSlot struct { }
ConcurrencyStatSlot is to record the Concurrency statistic for all arguments
func (*ConcurrencyStatSlot) OnCompleted ¶
func (c *ConcurrencyStatSlot) OnCompleted(ctx *base.EntryContext)
func (*ConcurrencyStatSlot) OnEntryBlocked ¶
func (c *ConcurrencyStatSlot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError)
func (*ConcurrencyStatSlot) OnEntryPassed ¶
func (c *ConcurrencyStatSlot) OnEntryPassed(ctx *base.EntryContext)
func (*ConcurrencyStatSlot) Order ¶ added in v1.0.0
func (s *ConcurrencyStatSlot) Order() uint32
type ControlBehavior ¶
type ControlBehavior int32
ControlBehavior indicates the traffic shaping behaviour.
const ( Reject ControlBehavior = iota Throttling )
func (ControlBehavior) String ¶
func (t ControlBehavior) String() string
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 (t MetricType) String() string
type ParamsMetric ¶
type ParamsMetric struct { // RuleTimeCounter records the last added token timestamp. RuleTimeCounter cache.ConcurrentCounterCache // RuleTokenCounter records the number of tokens. RuleTokenCounter cache.ConcurrentCounterCache // ConcurrencyCounter records the real-time concurrency. ConcurrencyCounter cache.ConcurrentCounterCache }
ParamsMetric carries real-time counters for frequent ("hot spot") parameters.
For each cache map, the key is the parameter value, while the value is the counter.
type Rule ¶
type Rule struct { // ID is the unique id ID string `json:"id,omitempty"` // Resource is the resource name Resource string `json:"resource"` // MetricType indicates the metric type for checking logic. // For Concurrency metric, hotspot module will check the each hot parameter's concurrency, // if concurrency exceeds the Threshold, reject the traffic directly. // For QPS metric, hotspot module will check the each hot parameter's QPS, // the ControlBehavior decides the behavior of traffic shaping controller MetricType MetricType `json:"metricType"` // ControlBehavior indicates the traffic shaping behaviour. // ControlBehavior only takes effect when MetricType is QPS ControlBehavior ControlBehavior `json:"controlBehavior"` // ParamIndex is the index in context arguments slice. // if ParamIndex is great than or equals to zero, ParamIndex means the <ParamIndex>-th parameter // if ParamIndex is the negative, ParamIndex means the reversed <ParamIndex>-th parameter ParamIndex int `json:"paramIndex"` // ParamKey is the key in EntryContext.Input.Attachments map. // ParamKey can be used as a supplement to ParamIndex to facilitate rules to quickly obtain parameter from a large number of parameters // ParamKey is mutually exclusive with ParamIndex, ParamKey has the higher priority than ParamIndex ParamKey string `json:"paramKey"` // Threshold is the threshold to trigger rejection Threshold int64 `json:"threshold"` // MaxQueueingTimeMs only takes effect when ControlBehavior is Throttling and MetricType is QPS MaxQueueingTimeMs int64 `json:"maxQueueingTimeMs"` // BurstCount is the silent count // BurstCount only takes effect when ControlBehavior is Reject and MetricType is QPS BurstCount int64 `json:"burstCount"` // DurationInSec is the time interval in statistic // DurationInSec only takes effect when MetricType is QPS DurationInSec int64 `json:"durationInSec"` // ParamsMaxCapacity is the max capacity of cache statistic ParamsMaxCapacity int64 `json:"paramsMaxCapacity"` // SpecificItems indicates the special threshold for specific value SpecificItems map[interface{}]int64 `json:"specificItems"` }
Rule represents the hotspot(frequent) parameter flow control rule
func GetRules ¶
func GetRules() []Rule
GetRules returns all the hotspot param flow rules based on copy. It doesn't take effect for hotspot module if user changes the returned rules. GetRules need to compete hotspot module's global lock and the high performance losses of copy,
reduce or do not call GetRules if possible.
func GetRulesOfResource ¶ added in v1.0.0
GetRulesOfResource returns specific resource's hotspot parameter flow control rules based on copy. It doesn't take effect for hotspot module if user changes the returned rules. GetRulesOfResource need to compete hotspot module's global lock and the high performance losses of copy,
reduce or do not call GetRulesOfResource frequently if possible.
func (*Rule) IsStatReusable ¶
IsStatReusable checks whether current rule is "statistically" equal to the given rule.
func (*Rule) ResourceName ¶
type Slot ¶ added in v0.5.0
type Slot struct { }
func (*Slot) Check ¶ added in v0.5.0
func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult
type TrafficControllerGenFunc ¶
type TrafficControllerGenFunc func(r *Rule, reuseMetric *ParamsMetric) TrafficShapingController
TrafficControllerGenFunc represents the TrafficShapingController generator function of a specific control behavior.
type TrafficShapingController ¶
type TrafficShapingController interface { PerformChecking(arg interface{}, batchCount int64) *base.TokenResult BoundParamIndex() int ExtractArgs(ctx *base.EntryContext) interface{} BoundMetric() *ParamsMetric BoundRule() *Rule }