flow

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package flow implements the flow shaping control.

flow module supports two statistic metric: QPS and Concurrency.

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

This section is empty.

Variables

This section is empty.

Functions

func ClearRules

func ClearRules() error

ClearRules clears all the rules in flow module.

func IsValidRule

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

func (s ControlBehavior) String() string

type DefaultTrafficShapingChecker

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

func NewDefaultTrafficShapingChecker

func NewDefaultTrafficShapingChecker(rule *Rule) *DefaultTrafficShapingChecker

func (*DefaultTrafficShapingChecker) DoCheck

func (d *DefaultTrafficShapingChecker) DoCheck(node base.StatNode, acquireCount uint32, threshold float64) *base.TokenResult

type DirectTrafficShapingCalculator

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

func NewDirectTrafficShapingCalculator

func NewDirectTrafficShapingCalculator(threshold float64) *DirectTrafficShapingCalculator

func (*DirectTrafficShapingCalculator) CalculateAllowedTokens

func (d *DirectTrafficShapingCalculator) CalculateAllowedTokens(base.StatNode, uint32, int32) float64

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 (s MetricType) String() string

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 uint64 `json:"id,omitempty"`

	// Resource represents the resource name.
	Resource               string                 `json:"resource"`
	MetricType             MetricType             `json:"metricType"`
	TokenCalculateStrategy TokenCalculateStrategy `json:"tokenCalculateStrategy"`
	ControlBehavior        ControlBehavior        `json:"controlBehavior"`
	Count                  float64                `json:"count"`
	RelationStrategy       RelationStrategy       `json:"relationStrategy"`
	RefResource            string                 `json:"refResource"`
	MaxQueueingTimeMs      uint32                 `json:"maxQueueingTimeMs"`
	WarmUpPeriodSec        uint32                 `json:"warmUpPeriodSec"`
	WarmUpColdFactor       uint32                 `json:"warmUpColdFactor"`
}

Rule describes the strategy of flow control.

func GetResRules

func GetResRules(res string) []Rule

GetResRules returns specific resource's rules based on copy. It doesn't take effect for flow module if user changes the rule.

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 (*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

type ThrottlingChecker

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

ThrottlingChecker limits the time interval between two requests.

func NewThrottlingChecker

func NewThrottlingChecker(timeoutMs uint32) *ThrottlingChecker

func (*ThrottlingChecker) DoCheck

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

type TokenCalculateStrategy

type TokenCalculateStrategy int32
const (
	Direct TokenCalculateStrategy = iota
	WarmUp
)

func (TokenCalculateStrategy) String

func (s TokenCalculateStrategy) String() string

type TrafficControllerGenFunc

type TrafficControllerGenFunc func(*Rule) *TrafficShapingController

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 {
	CalculateAllowedTokens(node base.StatNode, acquireCount uint32, flag int32) float64
}

TrafficShapingCalculator calculates the actual traffic shaping threshold based on the threshold of rule and the traffic shaping strategy.

type TrafficShapingChecker

type TrafficShapingChecker interface {
	DoCheck(node base.StatNode, acquireCount 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(flowCalculator TrafficShapingCalculator, flowChecker TrafficShapingChecker, rule *Rule) *TrafficShapingController

NewTrafficShapingController creates a TrafficShapingController wrapped with the given checker and flow rule.

func (*TrafficShapingController) FlowCalculator

func (*TrafficShapingController) FlowChecker

func (*TrafficShapingController) PerformChecking

func (t *TrafficShapingController) PerformChecking(node base.StatNode, acquireCount uint32, flag int32) *base.TokenResult

func (*TrafficShapingController) Rule

func (t *TrafficShapingController) Rule() *Rule

type WarmUpTrafficShapingCalculator

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

func NewWarmUpTrafficShapingCalculator

func NewWarmUpTrafficShapingCalculator(rule *Rule) *WarmUpTrafficShapingCalculator

func (*WarmUpTrafficShapingCalculator) CalculateAllowedTokens

func (c *WarmUpTrafficShapingCalculator) CalculateAllowedTokens(node base.StatNode, acquireCount uint32, flag int32) float64

Jump to

Keyboard shortcuts

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