@@ -16,9 +16,7 @@ package llmtokenratelimit
16
16
17
17
// ================================= Config ====================================
18
18
const (
19
- DefaultResource string = "default-resource"
20
19
DefaultResourcePattern string = ".*"
21
- DefaultRuleName string = "overall-rule"
22
20
DefaultIdentifierValuePattern string = ".*"
23
21
DefaultKeyPattern string = ".*"
24
22
@@ -48,13 +46,13 @@ const (
48
46
KeyRequestInfos string = "SentinelLLMTokenRatelimitReqInfos"
49
47
KeyUsedTokenInfos string = "SentinelLLMTokenRatelimitUsedTokenInfos"
50
48
KeyMatchedRules string = "SentinelLLMTokenRatelimitMatchedRules"
51
- KeyLLMPrompts string = "SentinelLLMTokenRatelimitLLMPrompts"
52
49
KeyResponseHeaders string = "SentinelLLMTokenRatelimitResponseHeaders"
50
+ KeyRequestID string = "SentinelLLMTokenRatelimitRequestID"
53
51
)
54
52
55
53
// ================================= RedisRatelimitKeyFormat ==================
56
54
const (
57
- RedisRatelimitKeyFormat string = "sentinel-go:llm-token-ratelimit:%s:%s:%s:%d:%s" // ruleName , strategy, identifierType, timeWindow, tokenCountStrategy
55
+ RedisRatelimitKeyFormat string = "sentinel-go:llm-token-ratelimit:resource- %s:%s:%s:%d:%s" // hashedResource , strategy, identifierType, timeWindow, tokenCountStrategy
58
56
)
59
57
60
58
// ================================= ResponseHeader ==================
@@ -67,11 +65,13 @@ const (
67
65
68
66
// ================================= PETAStrategy =============================
69
67
const (
70
- PETANoWaiting int64 = 0
71
- PETACorrectOK int64 = 1
72
- PETASlidingWindowKeyFormat string = "{shard-%s}:sliding-window:%s" // hashTag, redisRatelimitKey
73
- PETATokenBucketKeyFormat string = "{shard-%s}:token-bucket:%s" // hashTag, redisRatelimitKey
74
- PETARandomStringLength int = 16
68
+ PETANoWaiting int64 = 0
69
+ PETACorrectOK int64 = 0
70
+ PETACorrectUnderestimateError int64 = 1
71
+ PETACorrectOverestimateError int64 = 2
72
+ PETASlidingWindowKeyFormat string = "{shard-%s}:sliding-window:%s" // hashTag, redisRatelimitKey
73
+ PETATokenBucketKeyFormat string = "{shard-%s}:token-bucket:%s" // hashTag, redisRatelimitKey
74
+ PETARandomStringLength int = 16
75
75
)
76
76
77
77
// ================================= Generate Random String ===================
@@ -86,108 +86,3 @@ const (
86
86
const (
87
87
TokenEncoderKeyFormat string = "%s:token-encoder:%s:%s" // redisRatelimitKey, provider, model
88
88
)
89
-
90
- // ================================= RedisKeyForbiddenChars ===================
91
- var RedisKeyForbiddenChars = map [string ]string {
92
- // Control characters
93
- " " : "space" ,
94
- "\n " : "newline" ,
95
- "\r " : "carriage return" ,
96
- "\t " : "tab" ,
97
- "\x00 " : "null byte" ,
98
- "\x01 " : "start of heading" ,
99
- "\x02 " : "start of text" ,
100
- "\x03 " : "end of text" ,
101
- "\x04 " : "end of transmission" ,
102
- "\x05 " : "enquiry" ,
103
- "\x06 " : "acknowledge" ,
104
- "\x07 " : "bell" ,
105
- "\x08 " : "backspace" ,
106
- "\x0B " : "vertical tab" ,
107
- "\x0C " : "form feed" ,
108
- "\x0E " : "shift out" ,
109
- "\x0F " : "shift in" ,
110
- "\x10 " : "data link escape" ,
111
- "\x11 " : "device control 1" ,
112
- "\x12 " : "device control 2" ,
113
- "\x13 " : "device control 3" ,
114
- "\x14 " : "device control 4" ,
115
- "\x15 " : "negative acknowledge" ,
116
- "\x16 " : "synchronous idle" ,
117
- "\x17 " : "end of transmission block" ,
118
- "\x18 " : "cancel" ,
119
- "\x19 " : "end of medium" ,
120
- "\x1A " : "substitute" ,
121
- "\x1B " : "escape" ,
122
- "\x1C " : "file separator" ,
123
- "\x1D " : "group separator" ,
124
- "\x1E " : "record separator" ,
125
- "\x1F " : "unit separator" ,
126
- "\x7F " : "delete" ,
127
-
128
- // Special characters that may cause issues
129
- "*" : "asterisk (reserved for wildcard)" ,
130
- "?" : "question mark (reserved for wildcard)" ,
131
- "[" : "left bracket (reserved for character class)" ,
132
- "]" : "right bracket (reserved for character class)" ,
133
- "{" : "left brace (reserved for quantifier)" ,
134
- "}" : "right brace (reserved for quantifier)" ,
135
- "(" : "left parenthesis (reserved for grouping)" ,
136
- ")" : "right parenthesis (reserved for grouping)" ,
137
- "|" : "pipe (reserved for alternation)" ,
138
- "^" : "caret (reserved for start anchor)" ,
139
- "$" : "dollar (reserved for end anchor)" ,
140
- "+" : "plus (reserved for quantifier)" ,
141
- "." : "dot (reserved for any character)" ,
142
- "\\ " : "backslash (reserved for escape)" ,
143
- "/" : "forward slash (potential path separator)" ,
144
-
145
- // Redis protocol related characters
146
- "\r \n " : "CRLF sequence" ,
147
-
148
- // Unicode control characters (partial)
149
- "\u0080 " : "padding character" ,
150
- "\u0081 " : "high octet preset" ,
151
- "\u0082 " : "break permitted here" ,
152
- "\u0083 " : "no break here" ,
153
- "\u0084 " : "index" ,
154
- "\u0085 " : "next line" ,
155
- "\u0086 " : "start of selected area" ,
156
- "\u0087 " : "end of selected area" ,
157
- "\u0088 " : "character tabulation set" ,
158
- "\u0089 " : "character tabulation with justification" ,
159
- "\u008A " : "line tabulation set" ,
160
- "\u008B " : "partial line forward" ,
161
- "\u008C " : "partial line backward" ,
162
- "\u008D " : "reverse line feed" ,
163
- "\u008E " : "single shift two" ,
164
- "\u008F " : "single shift three" ,
165
- "\u0090 " : "device control string" ,
166
- "\u0091 " : "private use one" ,
167
- "\u0092 " : "private use two" ,
168
- "\u0093 " : "set transmit state" ,
169
- "\u0094 " : "cancel character" ,
170
- "\u0095 " : "message waiting" ,
171
- "\u0096 " : "start of guarded area" ,
172
- "\u0097 " : "end of guarded area" ,
173
- "\u0098 " : "start of string" ,
174
- "\u0099 " : "single graphic character introducer" ,
175
- "\u009A " : "single character introducer" ,
176
- "\u009B " : "control sequence introducer" ,
177
- "\u009C " : "string terminator" ,
178
- "\u009D " : "operating system command" ,
179
- "\u009E " : "privacy message" ,
180
- "\u009F " : "application program command" ,
181
-
182
- // Other potentially problematic characters
183
- "\" " : "double quote (potential JSON escape)" ,
184
- "'" : "single quote (potential SQL injection)" ,
185
- "`" : "backtick (potential command injection)" ,
186
- "<" : "less than (potential XSS)" ,
187
- ">" : "greater than (potential XSS)" ,
188
- "&" : "ampersand (potential HTML entity)" ,
189
- ";" : "semicolon (potential command separator)" ,
190
- ":" : "colon (potential protocol separator)" ,
191
- "=" : "equals (potential assignment)" ,
192
- "," : "comma (potential CSV separator)" ,
193
- }
0 commit comments