base

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2020 License: Apache-2.0 Imports: 8 Imported by: 122

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockError

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

BlockError indicates the request was blocked by Sentinel.

func NewBlockError

func NewBlockError(blockType BlockType) *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) TriggeredRule

func (e *BlockError) TriggeredRule() SentinelRule

func (*BlockError) TriggeredValue

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

type BlockType

type BlockType uint8
const (
	BlockTypeUnknown BlockType = iota
	BlockTypeFlow
	BlockTypeCircuitBreaking
	BlockTypeSystemFlow
	BlockTypeHotSpotParamFlow
)

func (BlockType) String

func (t BlockType) String() string

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
	MetricEventBlock

	MetricEventComplete
	// Biz error, used for circuit breaker
	MetricEventError
	// request execute rt, unit is millisecond
	MetricEventRt
	// hack for the number of event
	MetricEventTotal
)

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
	ResTypeWeb
	ResTypeRPC
	ResTypeAPIGateway
	ResTypeDBSQL
	ResTypeCache
	ResTypeMQ
)

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 {
	AcquireCount 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 {
	fmt.Stringer

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

func (sc *SlotChain) AddRuleCheckSlotFirst(s RuleCheckSlot)

func (*SlotChain) AddRuleCheckSlotLast

func (sc *SlotChain) AddRuleCheckSlotLast(s RuleCheckSlot)

func (*SlotChain) AddStatPrepareSlotFirst

func (sc *SlotChain) AddStatPrepareSlotFirst(s StatPrepareSlot)

func (*SlotChain) AddStatPrepareSlotLast

func (sc *SlotChain) AddStatPrepareSlotLast(s StatPrepareSlot)

func (*SlotChain) AddStatSlotFirst

func (sc *SlotChain) AddStatSlotFirst(s StatSlot)

func (*SlotChain) AddStatSlotLast

func (sc *SlotChain) AddStatSlotLast(s StatSlot)

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 {
	MetricItemRetriever

	ReadStat
	WriteStat

	CurrentGoroutineNum() int32
	IncreaseGoroutineNum()
	DecreaseGoroutineNum()

	Reset()
}

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 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(waitMs uint64) *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) ResetToBlocked added in v0.5.0

func (r *TokenResult) ResetToBlocked(blockType BlockType)

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

func (*TokenResult) WaitMs

func (r *TokenResult) WaitMs() uint64

type TokenResultStatus

type TokenResultStatus uint8
const (
	ResultStatusPass TokenResultStatus = iota
	ResultStatusBlocked
	ResultStatusShouldWait
)

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

func (TrafficType) String

func (t TrafficType) String() string

type WriteStat

type WriteStat interface {
	AddMetric(event MetricEvent, count uint64)
}

Jump to

Keyboard shortcuts

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