Version: v1.0.4 Latest Latest

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

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




View Source
const (
	TotalInBoundResourceName = "__total_inbound_traffic__"

	DefaultMaxResourceAmount uint32 = 10000

	DefaultSampleCount uint32 = 2
	DefaultIntervalMs  uint32 = 1000

	// default 10*1000/500 = 20
	DefaultSampleCountTotal uint32 = 20
	// default 10s (total length)
	DefaultIntervalMsTotal uint32 = 10000

	DefaultStatisticMaxRt = int64(60000)

global variable


View Source
var (
	IllegalGlobalStatisticParamsError = errors.New("Invalid parameters, sampleCount or interval, for resource's global statistic")
	IllegalStatisticParamsError       = errors.New("Invalid parameters, sampleCount or interval, for metric statistic")
	GlobalStatisticNonReusableError   = errors.New("The parameters, sampleCount and interval, mismatch for reusing between resource's global statistic and readonly metric statistic.")


func CheckValidityForReuseStatistic added in v1.0.0

func CheckValidityForReuseStatistic(sampleCount, intervalInMs uint32, parentSampleCount, parentIntervalInMs uint32) error

CheckValidityForReuseStatistic check the compliance whether readonly metric statistic can be built based on resource's global statistic The parameters, sampleCount and intervalInMs, are the parameters of the metric statistic you want to build The parameters, parentSampleCount and parentIntervalInMs, are the parameters of the resource's global statistic If compliance passes, return nil, if not returns specific error

func CheckValidityForStatistic added in v1.0.0

func CheckValidityForStatistic(sampleCount, intervalInMs uint32) error

func NopReadStat added in v1.0.0

func NopReadStat() *nopReadStat

func NopWriteStat added in v1.0.0

func NopWriteStat() *nopWriteStat

func RegistryBlockType added in v1.0.3

func RegistryBlockType(blockType BlockType, desc string) error

RegistryBlockType adds block type and corresponding description in order.


type BaseSlot added in v1.0.0

type BaseSlot interface {
	// Order returns the sort value of the slot.
	// SlotChain will sort all it's slots by ascending sort value in each bucket
	// (StatPrepareSlot bucket、RuleCheckSlot bucket and StatSlot bucket)
	Order() uint32

type BlockError

type BlockError struct {
	// contains filtered or unexported fields

BlockError indicates the request was blocked by Sentinel.

func NewBlockError

func NewBlockError(opts ...BlockErrorOption) *BlockError

func NewBlockErrorFromDeepCopy added in v0.3.0

func NewBlockErrorFromDeepCopy(from *BlockError) *BlockError

func NewBlockErrorWithCause

func NewBlockErrorWithCause(blockType BlockType, blockMsg string, rule SentinelRule, snapshot interface{}) *BlockError

func NewBlockErrorWithMessage added in v0.5.0

func NewBlockErrorWithMessage(blockType BlockType, message string) *BlockError

func (*BlockError) BlockMsg

func (e *BlockError) BlockMsg() string

func (*BlockError) BlockType

func (e *BlockError) BlockType() BlockType

func (*BlockError) Error

func (e *BlockError) Error() string

func (*BlockError) ResetBlockError added in v1.0.3

func (e *BlockError) ResetBlockError(opts ...BlockErrorOption)

func (*BlockError) TriggeredRule

func (e *BlockError) TriggeredRule() SentinelRule

func (*BlockError) TriggeredValue

func (e *BlockError) TriggeredValue() interface{}

type BlockErrorOption added in v1.0.3

type BlockErrorOption func(*BlockError)

func WithBlockMsg added in v1.0.3

func WithBlockMsg(blockMsg string) BlockErrorOption

func WithBlockType added in v1.0.3

func WithBlockType(blockType BlockType) BlockErrorOption

func WithRule added in v1.0.3

func WithRule(rule SentinelRule) BlockErrorOption

func WithSnapshotValue added in v1.0.3

func WithSnapshotValue(snapshotValue interface{}) BlockErrorOption

type BlockType

type BlockType uint8
const (
	BlockTypeUnknown BlockType = iota

func (BlockType) String

func (t BlockType) String() string

type ConcurrencyStat added in v1.0.0

type ConcurrencyStat interface {
	CurrentConcurrency() int32

type EntryContext

type EntryContext struct {
	Resource *ResourceWrapper
	StatNode StatNode

	Input *SentinelInput
	// the result of rule slots check
	RuleCheckResult *TokenResult
	// reserve for storing some intermediate data from the Entry execution process
	Data map[interface{}]interface{}
	// contains filtered or unexported fields

func NewEmptyEntryContext

func NewEmptyEntryContext() *EntryContext

func (*EntryContext) Entry added in v0.6.0

func (ctx *EntryContext) Entry() *SentinelEntry

func (*EntryContext) Err added in v0.3.0

func (ctx *EntryContext) Err() error

func (*EntryContext) IsBlocked

func (ctx *EntryContext) IsBlocked() bool

func (*EntryContext) PutRt added in v0.3.0

func (ctx *EntryContext) PutRt(rt uint64)

func (*EntryContext) Reset

func (ctx *EntryContext) Reset()

Reset init EntryContext,

func (*EntryContext) Rt added in v0.3.0

func (ctx *EntryContext) Rt() uint64

func (*EntryContext) SetEntry added in v0.6.0

func (ctx *EntryContext) SetEntry(entry *SentinelEntry)

func (*EntryContext) SetError added in v0.3.0

func (ctx *EntryContext) SetError(err error)

func (*EntryContext) StartTime

func (ctx *EntryContext) StartTime() uint64

type ExitHandler added in v0.6.0

type ExitHandler func(entry *SentinelEntry, ctx *EntryContext) error

type ExitOption added in v0.3.0

type ExitOption func(*ExitOptions)

func WithError added in v0.3.0

func WithError(err error) ExitOption

type ExitOptions added in v0.3.0

type ExitOptions struct {
	// contains filtered or unexported fields

type MetricEvent

type MetricEvent int8
const (
	// sentinel rules check pass
	MetricEventPass MetricEvent = iota
	// sentinel rules check block

	// Biz error, used for circuit breaker
	// request execute rt, unit is millisecond
	// hack for the number of event

There are five events to record pass + block == Total

type MetricItem

type MetricItem struct {
	Resource       string
	Classification int32
	Timestamp      uint64

	PassQps         uint64
	BlockQps        uint64
	CompleteQps     uint64
	ErrorQps        uint64
	AvgRt           uint64
	OccupiedPassQps uint64
	Concurrency     uint32

MetricItem represents the data of metric log per line.

func MetricItemFromFatString

func MetricItemFromFatString(line string) (*MetricItem, error)

func (*MetricItem) ToFatString

func (m *MetricItem) ToFatString() (string, error)

func (*MetricItem) ToThinString

func (m *MetricItem) ToThinString() (string, error)

type MetricItemRetriever

type MetricItemRetriever interface {
	MetricsOnCondition(predicate TimePredicate) []*MetricItem

type ReadStat

type ReadStat interface {
	GetQPS(event MetricEvent) float64
	GetPreviousQPS(event MetricEvent) float64
	GetSum(event MetricEvent) int64

	MinRT() float64
	AvgRT() float64

type ResourceType

type ResourceType int32

ResourceType represents classification of the resources

const (
	ResTypeCommon ResourceType = iota

type ResourceWrapper

type ResourceWrapper struct {
	// contains filtered or unexported fields

ResourceWrapper represents the invocation

func NewResourceWrapper

func NewResourceWrapper(name string, classification ResourceType, flowType TrafficType) *ResourceWrapper

func (*ResourceWrapper) Classification

func (r *ResourceWrapper) Classification() ResourceType

func (*ResourceWrapper) FlowType

func (r *ResourceWrapper) FlowType() TrafficType

func (*ResourceWrapper) Name

func (r *ResourceWrapper) Name() string

func (*ResourceWrapper) String

func (r *ResourceWrapper) String() string

type RuleCheckSlot

type RuleCheckSlot interface {
	// Check function do some validation
	// It can break off the slot pipeline
	// Each TokenResult will return check result
	// The upper logic will control pipeline according to SlotResult.
	Check(ctx *EntryContext) *TokenResult

RuleCheckSlot is rule based checking strategy All checking rule must implement this interface.

type SentinelEntry

type SentinelEntry struct {
	// contains filtered or unexported fields

func NewSentinelEntry

func NewSentinelEntry(ctx *EntryContext, rw *ResourceWrapper, sc *SlotChain) *SentinelEntry

func (*SentinelEntry) Context

func (e *SentinelEntry) Context() *EntryContext

func (*SentinelEntry) Exit

func (e *SentinelEntry) Exit(exitOps ...ExitOption)

func (*SentinelEntry) Resource

func (e *SentinelEntry) Resource() *ResourceWrapper

func (*SentinelEntry) SetError added in v0.3.0

func (e *SentinelEntry) SetError(err error)

func (*SentinelEntry) WhenExit added in v0.6.0

func (e *SentinelEntry) WhenExit(exitHandler ExitHandler)

type SentinelInput

type SentinelInput struct {
	BatchCount uint32
	Flag       int32
	Args       []interface{}
	// store some values in this context when calling context in slot.
	Attachments map[interface{}]interface{}

The input data of sentinel

type SentinelRule

type SentinelRule interface {

	ResourceName() string

type SlotChain

type SlotChain struct {
	// contains filtered or unexported fields

SlotChain hold all system slots and customized slot. SlotChain support plug-in slots developed by developer.

func NewSlotChain

func NewSlotChain() *SlotChain

func (*SlotChain) AddRuleCheckSlot added in v1.0.0

func (sc *SlotChain) AddRuleCheckSlot(s RuleCheckSlot)

AddRuleCheckSlot adds the RuleCheckSlot to the RuleCheckSlot list of the SlotChain. All RuleCheckSlot in the list will be sorted according to RuleCheckSlot.Order() in ascending order. AddRuleCheckSlot is non-thread safe, In concurrency scenario, AddRuleCheckSlot must be guarded by SlotChain.RWMutex#Lock

func (*SlotChain) AddStatPrepareSlot added in v1.0.0

func (sc *SlotChain) AddStatPrepareSlot(s StatPrepareSlot)

AddStatPrepareSlot adds the StatPrepareSlot slot to the StatPrepareSlot list of the SlotChain. All StatPrepareSlot in the list will be sorted according to StatPrepareSlot.Order() in ascending order. AddStatPrepareSlot is non-thread safe, In concurrency scenario, AddStatPrepareSlot must be guarded by SlotChain.RWMutex#Lock

func (*SlotChain) AddStatSlot added in v1.0.0

func (sc *SlotChain) AddStatSlot(s StatSlot)

AddStatSlot adds the StatSlot to the StatSlot list of the SlotChain. All StatSlot in the list will be sorted according to StatSlot.Order() in ascending order. AddStatSlot is non-thread safe, In concurrency scenario, AddStatSlot must be guarded by SlotChain.RWMutex#Lock

func (*SlotChain) Entry

func (sc *SlotChain) Entry(ctx *EntryContext) *TokenResult

The entrance of slot chain Return the TokenResult and nil if internal panic.

func (*SlotChain) GetPooledContext

func (sc *SlotChain) GetPooledContext() *EntryContext

Get a EntryContext from EntryContext ctxPool, if ctxPool doesn't have enough EntryContext then new one.

func (*SlotChain) RefurbishContext

func (sc *SlotChain) RefurbishContext(c *EntryContext)

type StatNode

type StatNode interface {


	// GenerateReadStat generates the readonly metric statistic based on resource level global statistic
	// If parameters, sampleCount and intervalInMs, are not suitable for resource level global statistic, return (nil, error)
	GenerateReadStat(sampleCount uint32, intervalInMs uint32) (ReadStat, error)

StatNode holds real-time statistics for resources.

type StatPrepareSlot

type StatPrepareSlot interface {
	// Prepare function do some initialization
	// Such as: init statistic structure、node and etc
	// The result of preparing would store in EntryContext
	// All StatPrepareSlots execute in sequence
	// Prepare function should not throw panic.
	Prepare(ctx *EntryContext)

StatPrepareSlot is responsible for some preparation before statistic For example: init structure and so on

type StatSlot

type StatSlot interface {
	// OnEntryPass function will be invoked when StatPrepareSlots and RuleCheckSlots execute pass
	// StatSlots will do some statistic logic, such as QPS、log、etc
	OnEntryPassed(ctx *EntryContext)
	// OnEntryBlocked function will be invoked when StatPrepareSlots and RuleCheckSlots fail to execute
	// It may be inbound flow control or outbound cir
	// StatSlots will do some statistic logic, such as QPS、log、etc
	// blockError introduce the block detail
	OnEntryBlocked(ctx *EntryContext, blockError *BlockError)
	// OnCompleted function will be invoked when chain exits.
	// The semantics of OnCompleted is the entry passed and completed
	// Note: blocked entry will not call this function
	OnCompleted(ctx *EntryContext)

StatSlot is responsible for counting all custom biz metrics. StatSlot would not handle any panic, and pass up all panic to slot chain

type TimePredicate

type TimePredicate func(uint64) bool

type TokenResult

type TokenResult struct {
	// contains filtered or unexported fields

func NewTokenResult added in v1.0.3

func NewTokenResult(status TokenResultStatus, blockErrOpts ...BlockErrorOption) *TokenResult

func NewTokenResultBlocked

func NewTokenResultBlocked(blockType BlockType) *TokenResult

func NewTokenResultBlockedWithCause

func NewTokenResultBlockedWithCause(blockType BlockType, blockMsg string, rule SentinelRule, snapshot interface{}) *TokenResult

func NewTokenResultBlockedWithMessage added in v0.5.0

func NewTokenResultBlockedWithMessage(blockType BlockType, blockMsg string) *TokenResult

func NewTokenResultPass

func NewTokenResultPass() *TokenResult

func NewTokenResultShouldWait

func NewTokenResultShouldWait(waitNs time.Duration) *TokenResult

func (*TokenResult) BlockError

func (r *TokenResult) BlockError() *BlockError

func (*TokenResult) DeepCopyFrom added in v0.3.0

func (r *TokenResult) DeepCopyFrom(newResult *TokenResult)

func (*TokenResult) IsBlocked

func (r *TokenResult) IsBlocked() bool

func (*TokenResult) IsPass

func (r *TokenResult) IsPass() bool

func (*TokenResult) NanosToWait added in v1.0.0

func (r *TokenResult) NanosToWait() time.Duration

func (*TokenResult) ResetToBlocked added in v0.5.0

func (r *TokenResult) ResetToBlocked(blockType BlockType)

func (*TokenResult) ResetToBlockedWith added in v1.0.3

func (r *TokenResult) ResetToBlockedWith(opts ...BlockErrorOption)

func (*TokenResult) ResetToBlockedWithCause added in v0.5.0

func (r *TokenResult) ResetToBlockedWithCause(blockType BlockType, blockMsg string, rule SentinelRule, snapshot interface{})

func (*TokenResult) ResetToBlockedWithMessage added in v0.5.0

func (r *TokenResult) ResetToBlockedWithMessage(blockType BlockType, blockMsg string)

func (*TokenResult) ResetToPass added in v0.3.0

func (r *TokenResult) ResetToPass()

func (*TokenResult) Status

func (r *TokenResult) Status() TokenResultStatus

func (*TokenResult) String

func (r *TokenResult) String() string

type TokenResultStatus

type TokenResultStatus uint8
const (
	ResultStatusPass TokenResultStatus = iota

func (TokenResultStatus) String added in v0.3.0

func (s TokenResultStatus) String() string

type TrafficType

type TrafficType int32

TrafficType describes the traffic type: Inbound or Outbound

const (
	// Inbound represents the inbound traffic (e.g. provider)
	Inbound TrafficType = iota
	// Outbound represents the outbound traffic (e.g. consumer)

func (TrafficType) String

func (t TrafficType) String() string

type WriteStat

type WriteStat interface {
	AddCount(event MetricEvent, count int64)

Jump to

Keyboard shortcuts

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