mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-08 16:46:37 +08:00
fix(lint): 消除 linter 告警
This commit is contained in:
@@ -153,13 +153,16 @@ function appendAutomaticPresetRule(
|
|||||||
? `${config.lookback_minutes}m`
|
? `${config.lookback_minutes}m`
|
||||||
: '1h';
|
: '1h';
|
||||||
// strip legacy field so saved JSON only keeps lookback duration string
|
// strip legacy field so saved JSON only keeps lookback duration string
|
||||||
const { lookback_minutes: _legacyLookbackMinutes, ...rest } = config;
|
const {
|
||||||
|
lookback_minutes: _legacyLookbackMinutes,
|
||||||
|
lookback: _existingLookback,
|
||||||
|
...rest
|
||||||
|
} = config;
|
||||||
return JSON.stringify(
|
return JSON.stringify(
|
||||||
{
|
{
|
||||||
lookback,
|
|
||||||
ttl: typeof rest.ttl === 'number' ? rest.ttl : -1,
|
|
||||||
...rest,
|
...rest,
|
||||||
lookback,
|
lookback,
|
||||||
|
ttl: typeof rest.ttl === 'number' ? rest.ttl : -1,
|
||||||
rules: nextRules,
|
rules: nextRules,
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ func (env ipGroupAutoRuleEnv) StatusRatio(code any) float64 {
|
|||||||
return float64(countStatusMatches(env.statusCounts, code)) / float64(env.RequestCount)
|
return float64(countStatusMatches(env.statusCounts, code)) / float64(env.RequestCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const maxHTTPStatusCodeDigits = 999
|
||||||
|
|
||||||
// countStatusMatches sums status counts for an exact code or class token.
|
// countStatusMatches sums status counts for an exact code or class token.
|
||||||
// Accepted forms:
|
// Accepted forms:
|
||||||
// - int / int64 / float64: exact status code (e.g. 404)
|
// - int / int64 / float64: exact status code (e.g. 404)
|
||||||
@@ -66,31 +68,13 @@ func countStatusMatches(statusCounts map[int]int, code any) int {
|
|||||||
switch v := code.(type) {
|
switch v := code.(type) {
|
||||||
case int:
|
case int:
|
||||||
return statusCounts[v]
|
return statusCounts[v]
|
||||||
case int8:
|
|
||||||
return statusCounts[int(v)]
|
|
||||||
case int16:
|
|
||||||
return statusCounts[int(v)]
|
|
||||||
case int32:
|
|
||||||
return statusCounts[int(v)]
|
|
||||||
case int64:
|
case int64:
|
||||||
return statusCounts[int(v)]
|
if v < 0 || v > int64(maxHTTPStatusCodeDigits) {
|
||||||
case uint:
|
|
||||||
return statusCounts[int(v)]
|
|
||||||
case uint8:
|
|
||||||
return statusCounts[int(v)]
|
|
||||||
case uint16:
|
|
||||||
return statusCounts[int(v)]
|
|
||||||
case uint32:
|
|
||||||
return statusCounts[int(v)]
|
|
||||||
case uint64:
|
|
||||||
return statusCounts[int(v)]
|
|
||||||
case float32:
|
|
||||||
if v != float32(int(v)) {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return statusCounts[int(v)]
|
return statusCounts[int(v)]
|
||||||
case float64:
|
case float64:
|
||||||
if v != float64(int(v)) {
|
if v != float64(int64(v)) || v < 0 || v > float64(maxHTTPStatusCodeDigits) {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return statusCounts[int(v)]
|
return statusCounts[int(v)]
|
||||||
@@ -127,14 +111,10 @@ func countStatusMatchesString(statusCounts map[int]int, raw string) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
code = code*10 + int(ch-'0')
|
code = code*10 + int(ch-'0')
|
||||||
if code > 999 {
|
if code > maxHTTPStatusCodeDigits {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if code < 100 || code > 599 {
|
|
||||||
// still allow lookup for non-standard codes if present
|
|
||||||
return statusCounts[code]
|
|
||||||
}
|
|
||||||
return statusCounts[code]
|
return statusCounts[code]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -911,6 +911,12 @@ func buildPathExactMatchPattern(rules []string) string {
|
|||||||
return fmt.Sprintf("^(?:%s)$", strings.Join(parts, "|"))
|
return fmt.Sprintf("^(?:%s)$", strings.Join(parts, "|"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
limitReqDefaultBurst = 5
|
||||||
|
limitReqPerSecondBurstMul = 2
|
||||||
|
limitReqPerMinuteBurstDiv = 5
|
||||||
|
)
|
||||||
|
|
||||||
func calculateBurst(rateStr string) int {
|
func calculateBurst(rateStr string) int {
|
||||||
rateStr = strings.ToLower(strings.TrimSpace(rateStr))
|
rateStr = strings.ToLower(strings.TrimSpace(rateStr))
|
||||||
if rateStr == "" {
|
if rateStr == "" {
|
||||||
@@ -919,20 +925,19 @@ func calculateBurst(rateStr string) int {
|
|||||||
var val int
|
var val int
|
||||||
var unit string
|
var unit string
|
||||||
_, err := fmt.Sscanf(rateStr, "%dr/%s", &val, &unit)
|
_, err := fmt.Sscanf(rateStr, "%dr/%s", &val, &unit)
|
||||||
if err != nil {
|
if err != nil || val <= 0 {
|
||||||
return 5
|
return limitReqDefaultBurst
|
||||||
}
|
}
|
||||||
if val <= 0 {
|
switch unit {
|
||||||
return 5
|
case "s":
|
||||||
}
|
return val * limitReqPerSecondBurstMul
|
||||||
if unit == "s" {
|
case "m":
|
||||||
return val * 2
|
b := val / limitReqPerMinuteBurstDiv
|
||||||
} else if unit == "m" {
|
if b < limitReqDefaultBurst {
|
||||||
b := val / 5
|
return limitReqDefaultBurst
|
||||||
if b < 5 {
|
|
||||||
b = 5
|
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
|
default:
|
||||||
|
return limitReqDefaultBurst
|
||||||
}
|
}
|
||||||
return 5
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user