Skip to content
Open
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
13 changes: 13 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package api

import (
"context"
"sync"

"github.com/alibaba/sentinel-golang/core/base"
Expand All @@ -36,6 +37,7 @@ var entryOptsPool = sync.Pool{

// EntryOptions represents the options of a Sentinel resource entry.
type EntryOptions struct {
ctx context.Context
resourceType base.ResourceType
entryType base.TrafficType
batchCount uint32
Expand All @@ -53,6 +55,7 @@ func (o *EntryOptions) Reset() {
o.slotChain = nil
o.args = o.args[:0]
o.attachments = nil
o.ctx = nil
}

type EntryOption func(*EntryOptions)
Expand Down Expand Up @@ -129,6 +132,13 @@ func WithAttachments(data map[interface{}]interface{}) EntryOption {
}
}

// WithContext sets the resource entry with the given context.
func WithContext(ctx context.Context) EntryOption {
return func(opts *EntryOptions) {
opts.ctx = ctx
}
}

// Entry is the basic API of Sentinel.
func Entry(resource string, opts ...EntryOption) (*base.SentinelEntry, *base.BlockError) {
options := entryOptsPool.Get().(*EntryOptions)
Expand Down Expand Up @@ -164,6 +174,9 @@ func entry(resource string, options *EntryOptions) (*base.SentinelEntry, *base.B
if len(options.attachments) != 0 {
ctx.Input.Attachments = options.attachments
}
if options.ctx != nil {
ctx.Input.Context = options.ctx
}
e := base.NewSentinelEntry(ctx, rw, sc)
ctx.SetEntry(e)
r := sc.Entry(ctx)
Expand Down
9 changes: 9 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package api

import (
"context"
"testing"

"github.com/alibaba/sentinel-golang/core/base"
Expand Down Expand Up @@ -157,3 +158,11 @@ func Test_entryWithArgsAndChainBlock(t *testing.T) {
ssm.AssertNumberOfCalls(t, "OnEntryBlocked", 1)
ssm.AssertNumberOfCalls(t, "OnCompleted", 0)
}

func TestWithContext(t *testing.T) {
entry, _ := Entry("testing", WithContext(context.Background()))
assert.NotNil(t, entry.Context().Input.Context)

entry.Exit()
assert.Nil(t, entry.Context().Input.Context)
}
9 changes: 8 additions & 1 deletion core/base/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package base

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

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

type EntryContext struct {
entry *SentinelEntry
Expand Down Expand Up @@ -86,11 +90,14 @@ type SentinelInput struct {
Args []interface{}
// store some values in this context when calling context in slot.
Attachments map[interface{}]interface{}
// store the request context
Context context.Context
}

func (i *SentinelInput) reset() {
i.BatchCount = 1
i.Flag = 0
i.Context = nil
i.Args = i.Args[:0]
if len(i.Attachments) != 0 {
i.Attachments = make(map[interface{}]interface{})
Expand Down