@@ -33,6 +33,8 @@ type TrafficShapingController interface {
3333
3434 BoundParamIndex () int
3535
36+ ExtractArgs (ctx * base.EntryContext ) interface {}
37+
3638 BoundMetric () * ParamsMetric
3739
3840 BoundRule () * Rule
@@ -44,6 +46,7 @@ type baseTrafficShapingController struct {
4446 res string
4547 metricType MetricType
4648 paramIndex int
49+ paramKey string
4750 threshold int64
4851 specificItems map [interface {}]int64
4952 durationInSec int64
@@ -60,6 +63,7 @@ func newBaseTrafficShapingControllerWithMetric(r *Rule, metric *ParamsMetric) *b
6063 res : r .Resource ,
6164 metricType : r .MetricType ,
6265 paramIndex : r .ParamIndex ,
66+ paramKey : r .ParamKey ,
6367 threshold : r .Threshold ,
6468 specificItems : r .SpecificItems ,
6569 durationInSec : r .DurationInSec ,
@@ -155,6 +159,72 @@ func (c *baseTrafficShapingController) BoundParamIndex() int {
155159 return c .paramIndex
156160}
157161
162+ // ExtractArgs matches the arg from ctx based on TrafficShapingController
163+ // return nil if match failed.
164+ func (c * baseTrafficShapingController ) ExtractArgs (ctx * base.EntryContext ) (value interface {}) {
165+ if c == nil {
166+ return nil
167+ }
168+ value = c .extractArgs (ctx )
169+ if value != nil {
170+ return
171+ }
172+ value = c .extractAttachmentArgs (ctx )
173+ if value != nil {
174+ return
175+ }
176+ return
177+ }
178+ func (c * baseTrafficShapingController ) extractArgs (ctx * base.EntryContext ) interface {} {
179+ args := ctx .Input .Args
180+ idx := c .BoundParamIndex ()
181+ if idx < 0 {
182+ idx = len (args ) + idx
183+ }
184+ if idx < 0 {
185+ if logging .DebugEnabled () {
186+ logging .Debug ("[extractArgs] The param index of hotspot traffic shaping controller is invalid" ,
187+ "args" , args , "paramIndex" , c .BoundParamIndex ())
188+ }
189+ return nil
190+ }
191+ if idx >= len (args ) {
192+ if logging .DebugEnabled () {
193+ logging .Debug ("[extractArgs] The argument in index doesn't exist" ,
194+ "args" , args , "paramIndex" , c .BoundParamIndex ())
195+ }
196+ return nil
197+ }
198+ return args [idx ]
199+ }
200+ func (c * baseTrafficShapingController ) extractAttachmentArgs (ctx * base.EntryContext ) interface {} {
201+ attachments := ctx .Input .Attachments
202+
203+ if attachments == nil {
204+ if logging .DebugEnabled () {
205+ logging .Debug ("[paramKey] The attachments of ctx is nil" ,
206+ "args" , attachments , "paramKey" , c .paramKey )
207+ }
208+ return nil
209+ }
210+ if c .paramKey == "" {
211+ if logging .DebugEnabled () {
212+ logging .Debug ("[paramKey] The param key is nil" ,
213+ "args" , attachments , "paramKey" , c .paramKey )
214+ }
215+ return nil
216+ }
217+ arg , ok := attachments [c .paramKey ]
218+ if ! ok {
219+ if logging .DebugEnabled () {
220+ logging .Debug ("[paramKey] extracted data does not exist" ,
221+ "args" , attachments , "paramKey" , c .paramKey )
222+ }
223+ }
224+
225+ return arg
226+ }
227+
158228func (c * rejectTrafficShapingController ) PerformChecking (arg interface {}, batchCount int64 ) * base.TokenResult {
159229 metric := c .metric
160230 if metric == nil {
0 commit comments