Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/slot_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/core/circuitbreaker"
"github.com/alibaba/sentinel-golang/core/flow"
"github.com/alibaba/sentinel-golang/core/log"
"github.com/alibaba/sentinel-golang/core/stat"
Expand All @@ -28,7 +29,9 @@ func BuildDefaultSlotChain() *base.SlotChain {
sc.AddStatPrepareSlotLast(&stat.StatNodePrepareSlot{})
sc.AddRuleCheckSlotLast(&system.SystemAdaptiveSlot{})
sc.AddRuleCheckSlotLast(&flow.FlowSlot{})
sc.AddRuleCheckSlotLast(&circuitbreaker.CircuitBreakerSlot{})
sc.AddStatSlotLast(&stat.StatisticSlot{})
sc.AddStatSlotLast(&log.LogSlot{})
sc.AddStatSlotLast(&circuitbreaker.MetricStatSlot{})
return sc
}
29 changes: 29 additions & 0 deletions core/base/context.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package base

import "github.com/alibaba/sentinel-golang/util"

type EntryContext struct {
// internal error when sentinel entry or
// biz error of downstream
err error
// Use to calculate RT
startTime uint64
// the rt of this transaction
rt uint64

Resource *ResourceWrapper
StatNode StatNode
Expand All @@ -14,6 +21,14 @@ type EntryContext struct {
Data map[interface{}]interface{}
}

func (ctx *EntryContext) Err() error {
return ctx.err
}

func (ctx *EntryContext) SetError(err error) {
ctx.err = err
}

func (ctx *EntryContext) StartTime() uint64 {
return ctx.startTime
}
Expand All @@ -25,6 +40,18 @@ func (ctx *EntryContext) IsBlocked() bool {
return ctx.RuleCheckResult.IsBlocked()
}

func (ctx *EntryContext) PutRt(rt uint64) {
ctx.rt = rt
}

func (ctx *EntryContext) Rt() uint64 {
if ctx.rt == 0 {
rt := util.CurrentTimeMillis() - ctx.StartTime()
return rt
}
return ctx.rt
}

func NewEmptyEntryContext() *EntryContext {
return &EntryContext{}
}
Expand All @@ -50,7 +77,9 @@ func newEmptyInput() *SentinelInput {
// Reset init EntryContext,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reset method should be updated for new fields.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right

func (ctx *EntryContext) Reset() {
// reset all fields of ctx
ctx.err = nil
ctx.startTime = 0
ctx.rt = 0
ctx.Resource = nil
ctx.StatNode = nil
ctx.Input = nil
Expand Down
36 changes: 33 additions & 3 deletions core/base/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ type SentinelEntry struct {
exitCtl sync.Once
}

func NewSentinelEntry(ctx *EntryContext, rw *ResourceWrapper, sc *SlotChain) *SentinelEntry {
return &SentinelEntry{
res: rw,
ctx: ctx,
sc: sc,
}
}

func (e *SentinelEntry) SetError(err error) {
if e.ctx != nil {
e.ctx.SetError(err)
}
}

func (e *SentinelEntry) Context() *EntryContext {
return e.ctx
}
Expand All @@ -23,12 +37,28 @@ func (e *SentinelEntry) Resource() *ResourceWrapper {
return e.res
}

func NewSentinelEntry(ctx *EntryContext, rw *ResourceWrapper, sc *SlotChain) *SentinelEntry {
return &SentinelEntry{res: rw, ctx: ctx, sc: sc}
type ExitOptions struct {
err error
}
type ExitOption func(*ExitOptions)

func WithError(err error) ExitOption {
return func(opts *ExitOptions) {
opts.err = err
}
}

func (e *SentinelEntry) Exit() {
func (e *SentinelEntry) Exit(exitOps ...ExitOption) {
var options = ExitOptions{
err: nil,
}
for _, opt := range exitOps {
opt(&options)
}
ctx := e.ctx
if options.err != nil {
ctx.SetError(options.err)
}
e.exitCtl.Do(func() {
if e.sc != nil {
e.sc.exit(ctx)
Expand Down
2 changes: 2 additions & 0 deletions core/base/slot_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/alibaba/sentinel-golang/logging"
"github.com/alibaba/sentinel-golang/util"
"github.com/pkg/errors"
)

var logger = logging.GetDefaultLogger()
Expand Down Expand Up @@ -124,6 +125,7 @@ func (sc *SlotChain) Entry(ctx *EntryContext) *TokenResult {
defer func() {
if err := recover(); err != nil {
logger.Panicf("Sentinel internal panic in SlotChain, err: %+v", err)
ctx.SetError(errors.Errorf("%+v", err))
return
}
}()
Expand Down
Loading