hotspot

package
v0.0.3-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package hotspot provides implementation of "hot-spot" (frequent) parameter flow control.

Index

Constants

View Source
const (
	ConcurrencyMaxCount = 4000
	ParamsCapacityBase  = 4000
	ParamsMaxCapacity   = 20000
)
View Source
const (
	RuleCheckSlotOrder = 4000
)
View Source
const (
	StatSlotOrder = 4000
)

Variables

View Source
var (
	DefaultConcurrencyStatSlot = &ConcurrencyStatSlot{}
)
View Source
var (
	DefaultSlot = &Slot{}
)

Functions

func ClearRules

func ClearRules() error

ClearRules clears all hotspot param flow rules.

func ClearRulesOfResource

func ClearRulesOfResource(res string) error

ClearRulesOfResource clears resource level hotspot param flow rules.

func IsValidRule

func IsValidRule(rule *Rule) error

func LoadRules

func LoadRules(rules []*Rule) (bool, error)

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

func LoadRulesOfResource(res string, rules []*Rule) (bool, error)

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

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

func GetRulesOfResource(res string) []Rule

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) Equals

func (r *Rule) Equals(newRule *Rule) bool

Equals checks whether current rule is consistent with the given rule.

func (*Rule) IsStatReusable

func (r *Rule) IsStatReusable(newRule *Rule) bool

IsStatReusable checks whether current rule is "statistically" equal to the given rule.

func (*Rule) ResourceName

func (r *Rule) ResourceName() string

func (*Rule) String

func (r *Rule) String() string

type Slot

type Slot struct {
}

func (*Slot) Check

func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult

func (*Slot) Order

func (s *Slot) Order() uint32

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
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL