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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
package providerhealth
import (
"sort"
"sync"
"time"
)
const recentWindow = 100
type RouteKey struct {
ModelID string
ProviderID string
WireAPI string
}
type Observation struct {
StatusCode int
Latency time.Duration
Failed bool
ObservedAt time.Time
}
type Status struct {
ModelID string `json:"model_id"`
ProviderID string `json:"provider_id"`
WireAPI string `json:"wire_api"`
State string `json:"state"`
Attempts uint64 `json:"attempts"`
RecentSamples int `json:"recent_samples"`
AvailabilityPercent float64 `json:"availability_percent"`
HeaderLatencyEWMA int64 `json:"header_latency_ewma_ms"`
ConsecutiveFailures uint64 `json:"consecutive_failures"`
LastStatusCode int `json:"last_status_code,omitempty"`
LastObservedAt *time.Time `json:"last_observed_at,omitempty"`
LastHealthyAt *time.Time `json:"last_healthy_at,omitempty"`
CircuitOpenUntil *time.Time `json:"circuit_open_until,omitempty"`
}
type Options struct {
FailureThreshold uint64
OpenDuration time.Duration
Now func() time.Time
}
type Tracker struct {
states sync.Map
failureThreshold uint64
openDuration time.Duration
now func() time.Time
}
type routeState struct {
mu sync.RWMutex
attempts uint64
consecutiveFailures uint64
lastStatusCode int
lastObservedAt time.Time
lastHealthyAt time.Time
openUntil time.Time
headerLatencyEWMA float64
recent [recentWindow]bool
recentCount int
recentPosition int
recentHealthy int
}
func New(options Options) *Tracker {
if options.FailureThreshold == 0 {
options.FailureThreshold = 3
}
if options.OpenDuration <= 0 {
options.OpenDuration = 30 * time.Second
}
if options.Now == nil {
options.Now = time.Now
}
return &Tracker{failureThreshold: options.FailureThreshold, openDuration: options.OpenDuration, now: options.Now}
}
func (t *Tracker) Observe(key RouteKey, observation Observation) {
if t == nil || key.ProviderID == "" {
return
}
if observation.ObservedAt.IsZero() {
observation.ObservedAt = t.now()
}
value, _ := t.states.LoadOrStore(key, &routeState{})
state := value.(*routeState)
state.mu.Lock()
defer state.mu.Unlock()
state.attempts++
state.lastStatusCode = observation.StatusCode
state.lastObservedAt = observation.ObservedAt
if observation.Latency > 0 {
latency := float64(observation.Latency.Milliseconds())
if latency < 1 {
latency = 1
}
if state.headerLatencyEWMA == 0 {
state.headerLatencyEWMA = latency
} else {
state.headerLatencyEWMA = state.headerLatencyEWMA*0.8 + latency*0.2
}
}
state.addRecent(!observation.Failed)
if observation.Failed {
state.consecutiveFailures++
if state.consecutiveFailures >= t.failureThreshold {
state.openUntil = observation.ObservedAt.Add(t.openDuration)
}
return
}
state.consecutiveFailures = 0
state.openUntil = time.Time{}
state.lastHealthyAt = observation.ObservedAt
}
func (s *routeState) addRecent(healthy bool) {
if s.recentCount == recentWindow {
if s.recent[s.recentPosition] {
s.recentHealthy--
}
} else {
s.recentCount++
}
s.recent[s.recentPosition] = healthy
if healthy {
s.recentHealthy++
}
s.recentPosition = (s.recentPosition + 1) % recentWindow
}
func (t *Tracker) CircuitOpen(key RouteKey) bool {
if t == nil {
return false
}
value, ok := t.states.Load(key)
if !ok {
return false
}
state := value.(*routeState)
state.mu.RLock()
defer state.mu.RUnlock()
return state.openUntil.After(t.now())
}
func (t *Tracker) Snapshot() []Status {
if t == nil {
return []Status{}
}
now := t.now()
result := make([]Status, 0)
t.states.Range(func(rawKey, rawState any) bool {
key := rawKey.(RouteKey)
state := rawState.(*routeState)
state.mu.RLock()
item := statusFromState(key, state, now)
state.mu.RUnlock()
result = append(result, item)
return true
})
sort.Slice(result, func(i, j int) bool {
if result[i].ModelID != result[j].ModelID {
return result[i].ModelID < result[j].ModelID
}
return result[i].ProviderID < result[j].ProviderID
})
return result
}
func statusFromState(key RouteKey, state *routeState, now time.Time) Status {
item := Status{ModelID: key.ModelID, ProviderID: key.ProviderID, WireAPI: key.WireAPI,
Attempts: state.attempts, RecentSamples: state.recentCount, HeaderLatencyEWMA: int64(state.headerLatencyEWMA + 0.5),
ConsecutiveFailures: state.consecutiveFailures, LastStatusCode: state.lastStatusCode}
if state.recentCount > 0 {
item.AvailabilityPercent = float64(state.recentHealthy) / float64(state.recentCount) * 100
}
if !state.lastObservedAt.IsZero() {
value := state.lastObservedAt
item.LastObservedAt = &value
}
if !state.lastHealthyAt.IsZero() {
value := state.lastHealthyAt
item.LastHealthyAt = &value
}
if state.openUntil.After(now) {
value := state.openUntil
item.CircuitOpenUntil = &value
item.State = "open"
} else if state.attempts == 0 {
item.State = "unknown"
} else if state.consecutiveFailures > 0 || (state.recentCount >= 5 && item.AvailabilityPercent < 95) {
item.State = "degraded"
} else {
item.State = "healthy"
}
return item
}
|