-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaccount_availability.go
More file actions
96 lines (82 loc) · 1.74 KB
/
account_availability.go
File metadata and controls
96 lines (82 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"log"
"time"
)
const (
claudePrimaryCooldownThreshold = 1.0
primaryHardExcludeThreshold = 0.95
secondaryHardExcludeThreshold = 0.99
)
func accountPrimaryUsageLocked(a *Account) float64 {
if a == nil {
return 0
}
used := a.Usage.PrimaryUsedPercent
if used == 0 {
used = a.Usage.PrimaryUsed
}
return used
}
func accountSecondaryUsageLocked(a *Account) float64 {
if a == nil {
return 0
}
used := a.Usage.SecondaryUsedPercent
if used == 0 {
used = a.Usage.SecondaryUsed
}
return used
}
func accountCoolingDownLocked(a *Account, now time.Time) bool {
if a == nil {
return false
}
return !a.RateLimitUntil.IsZero() && a.RateLimitUntil.After(now)
}
func accountUsageExhaustedLocked(a *Account) bool {
if a == nil {
return false
}
return accountPrimaryUsageLocked(a) >= primaryHardExcludeThreshold ||
accountSecondaryUsageLocked(a) >= secondaryHardExcludeThreshold
}
func accountAvailableForRoutingLocked(a *Account, now time.Time) bool {
if a == nil {
return false
}
if a.Dead || a.Disabled {
return false
}
if accountCoolingDownLocked(a, now) {
return false
}
return !accountUsageExhaustedLocked(a)
}
func syncUsageCooldown(a *Account) {
if a == nil {
return
}
now := time.Now()
a.mu.Lock()
defer a.mu.Unlock()
if a.Type != AccountTypeClaude {
return
}
primaryUsed := accountPrimaryUsageLocked(a)
resetAt := a.Usage.PrimaryResetAt
if primaryUsed < claudePrimaryCooldownThreshold || resetAt.IsZero() || !resetAt.After(now) {
return
}
if !a.RateLimitUntil.Before(resetAt) {
return
}
a.RateLimitUntil = resetAt
if a.ID != "" {
log.Printf("cooling down claude account %s until %s (5hr usage %.1f%%)",
a.ID,
resetAt.Format(time.RFC3339),
primaryUsed*100,
)
}
}