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
19 changes: 19 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type EntryOptions struct {
flag int32
slotChain *base.SlotChain
args []interface{}
attachments map[interface{}]interface{}
}

type EntryOption func(*EntryOptions)
Expand Down Expand Up @@ -51,6 +52,22 @@ func WithArgs(args ...interface{}) EntryOption {
}
}

// WithAttachment set the resource entry with the given k-v pair
func WithAttachment(key interface{}, value interface{}) EntryOption {
return func(opts *EntryOptions) {
opts.attachments[key] = value
}
}

// WithAttachment set the resource entry with the given k-v pairs
func WithAttachments(data map[interface{}]interface{}) EntryOption {
return func(opts *EntryOptions) {
for key, value := range data {
opts.attachments[key] = value
}
}
}

// Entry is the basic API of Sentinel.
func Entry(resource string, opts ...EntryOption) (*base.SentinelEntry, *base.BlockError) {
var options = EntryOptions{
Expand All @@ -60,6 +77,7 @@ func Entry(resource string, opts ...EntryOption) (*base.SentinelEntry, *base.Blo
flag: 0,
slotChain: globalSlotChain,
args: []interface{}{},
attachments: make(map[interface{}]interface{}),
}
for _, opt := range opts {
opt(&options)
Expand All @@ -82,6 +100,7 @@ func entry(resource string, options *EntryOptions) (*base.SentinelEntry, *base.B
AcquireCount: options.acquireCount,
Flag: options.flag,
Args: options.args,
Attachments: options.attachments,
}

e := base.NewSentinelEntry(ctx, rw, sc)
Expand Down
4 changes: 2 additions & 2 deletions core/base/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type SentinelInput struct {
Args []interface{}

// store some values in this context when calling context in slot.
data map[interface{}]interface{}
Attachments map[interface{}]interface{}
}

func newEmptyInput() *SentinelInput {
Expand All @@ -47,7 +47,7 @@ type SentinelOutput struct {
LastResult *TokenResult

// store output data.
data map[interface{}]interface{}
Attachments map[interface{}]interface{}
}

func newEmptyOutput() *SentinelOutput {
Expand Down
6 changes: 3 additions & 3 deletions core/base/slot_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestSlotChain_Entry_Pass_And_Exit(t *testing.T) {
AcquireCount: 1,
Flag: 0,
Args: nil,
data: nil,
Attachments: nil,
}

ps1 := &prepareSlotMock{}
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestSlotChain_Entry_Block(t *testing.T) {
AcquireCount: 1,
Flag: 0,
Args: nil,
data: nil,
Attachments: nil,
}

rbs := &prepareSlotMock{}
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestSlotChain_Entry_With_Panic(t *testing.T) {
AcquireCount: 1,
Flag: 0,
Args: nil,
data: nil,
Attachments: nil,
}

rbs := &badPrepareSlotMock{}
Expand Down