@@ -33,6 +33,8 @@ type TrafficShapingController interface {
33
33
34
34
BoundParamIndex () int
35
35
36
+ ExtractArgs (ctx * base.EntryContext ) interface {}
37
+
36
38
BoundMetric () * ParamsMetric
37
39
38
40
BoundRule () * Rule
@@ -44,6 +46,7 @@ type baseTrafficShapingController struct {
44
46
res string
45
47
metricType MetricType
46
48
paramIndex int
49
+ paramKey string
47
50
threshold int64
48
51
specificItems map [interface {}]int64
49
52
durationInSec int64
@@ -60,6 +63,7 @@ func newBaseTrafficShapingControllerWithMetric(r *Rule, metric *ParamsMetric) *b
60
63
res : r .Resource ,
61
64
metricType : r .MetricType ,
62
65
paramIndex : r .ParamIndex ,
66
+ paramKey : r .ParamKey ,
63
67
threshold : r .Threshold ,
64
68
specificItems : r .SpecificItems ,
65
69
durationInSec : r .DurationInSec ,
@@ -155,6 +159,72 @@ func (c *baseTrafficShapingController) BoundParamIndex() int {
155
159
return c .paramIndex
156
160
}
157
161
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 .extractAttachmentArgs (ctx )
169
+ if value != nil {
170
+ return
171
+ }
172
+ value = c .extractArgs (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
+
158
228
func (c * rejectTrafficShapingController ) PerformChecking (arg interface {}, batchCount int64 ) * base.TokenResult {
159
229
metric := c .metric
160
230
if metric == nil {
0 commit comments