flow

package
v1.0.0-M2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2020 License: Apache-2.0 Imports: 14 Imported by: 86

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 (
	RuleCheckSlotName = "sentinel-core-flow-rule-check-slot"
)
View Source
const (
	StatSlotName = "sentinel-core-flow-standalone-stat-slot"
)

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 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.

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 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 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"`
}

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

func (s *Slot) Name() string

type StandaloneStatSlot added in v1.0.0

type StandaloneStatSlot struct {
}

func (*StandaloneStatSlot) Name added in v1.0.0

func (s *StandaloneStatSlot) Name() string

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)

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
)

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