flow

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2021 License: Apache-2.0 Imports: 16 Imported by: 79

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

  1. TrafficShapingCalculator calculates the actual traffic shaping token threshold. Currently, Sentinel supports two token calculate strategy: Direct and WarmUp.
  2. 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:

  1. The function both SetTrafficShapingGenerator and RemoveTrafficShapingGenerator is not thread safe.
  2. Users can not override the Sentinel supported TrafficShapingController.

Index

Constants

View Source
const (
	BlockMsgQueueing = "flow throttling check blocked, estimated queueing time exceeds max queueing time"

	MillisToNanosOffset = int64(time.Millisecond / time.Nanosecond)
)
View Source
const (
	RuleCheckSlotOrder = 2000
)
View Source
const (
	StatSlotOrder = 3000
)

Variables

View Source
var (
	DefaultSlot = &Slot{}
)
View Source
var (
	DefaultStandaloneStatSlot = &StandaloneStatSlot{}
)

Functions

func ClearRules

func ClearRules() error

ClearRules clears all the rules in flow module.

func ClearRulesOfResource added in v1.0.3

func ClearRulesOfResource(res string) error

ClearRulesOfResource clears resource level rules in flow module.

func IsValidRule added in v0.6.1

func IsValidRule(rule *Rule) error

IsValidRule checks whether the given Rule is valid.

func LoadRules

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

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 added in v1.0.3

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

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 added in v0.6.1

func (s ControlBehavior) String() string

type DirectTrafficShapingCalculator added in v0.6.1

type DirectTrafficShapingCalculator struct {
	// contains filtered or unexported fields
}

func NewDirectTrafficShapingCalculator added in v0.6.1

func NewDirectTrafficShapingCalculator(owner *TrafficShapingController, threshold float64) *DirectTrafficShapingCalculator

func (*DirectTrafficShapingCalculator) BoundOwner added in v1.0.0

func (*DirectTrafficShapingCalculator) CalculateAllowedTokens added in v0.6.1

func (d *DirectTrafficShapingCalculator) CalculateAllowedTokens(uint32, int32) float64

type MemoryAdaptiveTrafficShapingCalculator added in v1.0.3

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 added in v1.0.3

func NewMemoryAdaptiveTrafficShapingCalculator(owner *TrafficShapingController, r *Rule) *MemoryAdaptiveTrafficShapingCalculator

func (*MemoryAdaptiveTrafficShapingCalculator) BoundOwner added in v1.0.3

func (*MemoryAdaptiveTrafficShapingCalculator) CalculateAllowedTokens added in v1.0.3

func (m *MemoryAdaptiveTrafficShapingCalculator) CalculateAllowedTokens(_ uint32, _ int32) float64

type RejectTrafficShapingChecker added in v1.0.0

type RejectTrafficShapingChecker struct {
	// contains filtered or unexported fields
}

func NewRejectTrafficShapingChecker added in v1.0.0

func NewRejectTrafficShapingChecker(owner *TrafficShapingController, rule *Rule) *RejectTrafficShapingChecker

func (*RejectTrafficShapingChecker) BoundOwner added in v1.0.0

func (*RejectTrafficShapingChecker) DoCheck added in v1.0.0

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 added in v0.6.1

func (s RelationStrategy) String() string

type Rule added in v0.6.1

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 added in v1.0.0

func GetRulesOfResource(res string) []Rule

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 added in v0.6.1

func (r *Rule) ResourceName() string

func (*Rule) String added in v0.6.1

func (r *Rule) String() string

type Slot added in v0.6.1

type Slot struct {
}

func (*Slot) Check added in v0.6.1

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

func (*Slot) Order added in v1.0.0

func (s *Slot) Order() uint32

type StandaloneStatSlot added in v1.0.0

type StandaloneStatSlot struct {
}

func (StandaloneStatSlot) OnCompleted added in v1.0.0

func (s StandaloneStatSlot) OnCompleted(ctx *base.EntryContext)

func (StandaloneStatSlot) OnEntryBlocked added in v1.0.0

func (s StandaloneStatSlot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError)

func (StandaloneStatSlot) OnEntryPassed added in v1.0.0

func (s StandaloneStatSlot) OnEntryPassed(ctx *base.EntryContext)

func (*StandaloneStatSlot) Order added in v1.0.0

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 added in v1.0.0

func (*ThrottlingChecker) DoCheck

func (c *ThrottlingChecker) DoCheck(_ base.StatNode, batchCount uint32, threshold float64) *base.TokenResult

type TokenCalculateStrategy added in v0.6.1

type TokenCalculateStrategy int32
const (
	Direct TokenCalculateStrategy = iota
	WarmUp
	MemoryAdaptive
)

func (TokenCalculateStrategy) String added in v0.6.1

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 added in v0.6.0

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 added in v1.0.0

func (t *TrafficShapingController) BoundRule() *Rule

func (*TrafficShapingController) FlowCalculator

func (*TrafficShapingController) FlowChecker

func (*TrafficShapingController) PerformChecking

func (t *TrafficShapingController) PerformChecking(resStat base.StatNode, batchCount uint32, flag int32) *base.TokenResult

type WarmUpTrafficShapingCalculator added in v0.6.0

type WarmUpTrafficShapingCalculator struct {
	// contains filtered or unexported fields
}

func (*WarmUpTrafficShapingCalculator) BoundOwner added in v1.0.0

func (*WarmUpTrafficShapingCalculator) CalculateAllowedTokens added in v0.6.0

func (c *WarmUpTrafficShapingCalculator) CalculateAllowedTokens(_ uint32, _ int32) float64

Jump to

Keyboard shortcuts

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