diff --git a/api/api.go b/api/api.go index 9ea60997c..9976d0027 100644 --- a/api/api.go +++ b/api/api.go @@ -12,6 +12,7 @@ type EntryOptions struct { flag int32 slotChain *base.SlotChain args []interface{} + attachments map[interface{}]interface{} } type EntryOption func(*EntryOptions) @@ -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{ @@ -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) @@ -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) diff --git a/core/base/context.go b/core/base/context.go index a5af3e538..73588cd35 100644 --- a/core/base/context.go +++ b/core/base/context.go @@ -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 { @@ -47,7 +47,7 @@ type SentinelOutput struct { LastResult *TokenResult // store output data. - data map[interface{}]interface{} + Attachments map[interface{}]interface{} } func newEmptyOutput() *SentinelOutput { diff --git a/core/base/slot_chain_test.go b/core/base/slot_chain_test.go index eaa9f8505..ec0e971e2 100644 --- a/core/base/slot_chain_test.go +++ b/core/base/slot_chain_test.go @@ -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{} @@ -266,7 +266,7 @@ func TestSlotChain_Entry_Block(t *testing.T) { AcquireCount: 1, Flag: 0, Args: nil, - data: nil, + Attachments: nil, } rbs := &prepareSlotMock{} @@ -324,7 +324,7 @@ func TestSlotChain_Entry_With_Panic(t *testing.T) { AcquireCount: 1, Flag: 0, Args: nil, - data: nil, + Attachments: nil, } rbs := &badPrepareSlotMock{}