@@ -40,9 +40,23 @@ func (c *BaseRuleCollector) Collect(ctx *Context, rule *Rule) []*MatchedRule {
40
40
41
41
reqInfos := extractRequestInfos (ctx ) // allow nil for global rate limit
42
42
43
- ruleMap := make (map [string ]* MatchedRule )
43
+ resourceHash := generateHash (rule .Resource )
44
+ ruleStrategy := rule .Strategy .String ()
45
+
46
+ estimatedSize := 0
47
+ for _ , item := range rule .SpecificItems {
48
+ if item .KeyItems != nil {
49
+ estimatedSize += len (item .KeyItems )
50
+ }
51
+ }
52
+
53
+ ruleMap := make (map [string ]* MatchedRule , estimatedSize )
44
54
45
55
for _ , specificItem := range rule .SpecificItems {
56
+ if specificItem .KeyItems == nil {
57
+ continue
58
+ }
59
+
46
60
identifierChecker := globalRuleMatcher .getIdentifierChecker (specificItem .Identifier .Type )
47
61
if identifierChecker == nil {
48
62
logging .Error (errors .New ("unknown identifier.type" ),
@@ -52,9 +66,9 @@ func (c *BaseRuleCollector) Collect(ctx *Context, rule *Rule) []*MatchedRule {
52
66
)
53
67
continue
54
68
}
55
- if specificItem . KeyItems == nil {
56
- continue
57
- }
69
+
70
+ identifierType := specificItem . Identifier . Type . String ()
71
+
58
72
for _ , keyItem := range specificItem .KeyItems {
59
73
if ! identifierChecker .Check (ctx , reqInfos , specificItem .Identifier , keyItem .Key ) {
60
74
continue
@@ -69,17 +83,22 @@ func (c *BaseRuleCollector) Collect(ctx *Context, rule *Rule) []*MatchedRule {
69
83
continue
70
84
}
71
85
72
- params := & BaseLimitKeyParams {
73
- Resource : generateHash (rule .Resource ),
74
- Strategy : rule .Strategy ,
75
- IdentifierType : specificItem .Identifier .Type ,
76
- TimeWindow : timeWindow ,
77
- TokenSize : keyItem .Token .Number ,
78
- CountStrategy : keyItem .Token .CountStrategy ,
86
+ limitKey := fmt .Sprintf (RedisRatelimitKeyFormat ,
87
+ resourceHash ,
88
+ ruleStrategy ,
89
+ identifierType ,
90
+ timeWindow ,
91
+ keyItem .Token .CountStrategy .String (),
92
+ )
93
+ ruleMap [limitKey ] = & MatchedRule {
94
+ Strategy : rule .Strategy ,
95
+ LimitKey : limitKey ,
96
+ TimeWindow : timeWindow ,
97
+ TokenSize : keyItem .Token .Number ,
98
+ CountStrategy : keyItem .Token .CountStrategy ,
79
99
// PETA
80
100
Encoding : rule .Encoding ,
81
101
}
82
- c .addMatchedRule (ctx , params , ruleMap )
83
102
}
84
103
}
85
104
@@ -89,59 +108,3 @@ func (c *BaseRuleCollector) Collect(ctx *Context, rule *Rule) []*MatchedRule {
89
108
}
90
109
return rules
91
110
}
92
-
93
- func (c * BaseRuleCollector ) addMatchedRule (ctx * Context , params * BaseLimitKeyParams , ruleMap map [string ]* MatchedRule ) {
94
- if c == nil {
95
- return
96
- }
97
- if params .CountStrategy != TotalTokens {
98
- limitKey , err := c .generateLimitKey (params )
99
- if err != nil {
100
- logging .Error (err , "failed to generate LimitKey in llm_token_ratelimit.BaseRuleCollector.addMatchedRule()" ,
101
- "params" , params ,
102
- "requestID" , ctx .Get (KeyRequestID ),
103
- )
104
- return
105
- }
106
- ruleMap [limitKey ] = & MatchedRule {
107
- Strategy : params .Strategy ,
108
- LimitKey : limitKey ,
109
- TimeWindow : params .TimeWindow ,
110
- TokenSize : params .TokenSize ,
111
- CountStrategy : params .CountStrategy ,
112
- // PETA
113
- Encoding : params .Encoding ,
114
- }
115
- }
116
- params .CountStrategy = TotalTokens
117
- limitKey , err := c .generateLimitKey (params )
118
- if err != nil {
119
- logging .Error (err , "failed to generate LimitKey in llm_token_ratelimit.BaseRuleCollector.addMatchedRule()" ,
120
- "params" , params ,
121
- "requestID" , ctx .Get (KeyRequestID ),
122
- )
123
- return
124
- }
125
- ruleMap [limitKey ] = & MatchedRule {
126
- Strategy : params .Strategy ,
127
- LimitKey : limitKey ,
128
- TimeWindow : params .TimeWindow ,
129
- TokenSize : params .TokenSize ,
130
- CountStrategy : params .CountStrategy ,
131
- // PETA
132
- Encoding : params .Encoding ,
133
- }
134
- }
135
-
136
- func (c * BaseRuleCollector ) generateLimitKey (params * BaseLimitKeyParams ) (string , error ) {
137
- if c == nil {
138
- return "" , fmt .Errorf ("BaseRuleCollector is nil" )
139
- }
140
- return fmt .Sprintf (RedisRatelimitKeyFormat ,
141
- params .Resource ,
142
- params .Strategy .String (),
143
- params .IdentifierType .String (),
144
- params .TimeWindow ,
145
- params .CountStrategy .String (),
146
- ), nil
147
- }
0 commit comments