MCPcopy Index your code
hub / github.com/coder/coder / MarkKeyOnStatus

Function MarkKeyOnStatus

aibridge/keypool/keymark.go:14–53  ·  view source on GitHub ↗

MarkKeyOnStatus marks key based on a key-specific HTTP status code from resp (429 for temporary, 401 or 403 for permanent). Returns true if the status was a key-specific failover trigger so callers can retry with the next key.

(
	ctx context.Context,
	key *Key,
	resp *http.Response,
	logger slog.Logger,
	providerName string,
)

Source from the content-addressed store, hash-verified

12// permanent). Returns true if the status was a key-specific
13// failover trigger so callers can retry with the next key.
14func MarkKeyOnStatus(
15 ctx context.Context,
16 key *Key,
17 resp *http.Response,
18 logger slog.Logger,
19 providerName string,
20) bool {
21 if resp == nil {
22 return false
23 }
24 statusCode := resp.StatusCode
25 switch statusCode {
26 case http.StatusTooManyRequests:
27 cooldown := ParseRetryAfter(resp)
28 if cooldown <= 0 {
29 cooldown = defaultCooldown
30 }
31 if key.MarkTemporary(cooldown) {
32 logger.Info(ctx, "key marked temporary",
33 slog.F("provider", providerName),
34 slog.F("api_key_hint", key.Hint()),
35 slog.F("status", statusCode),
36 slog.F("cooldown", cooldown))
37 }
38 return true
39 case http.StatusUnauthorized, http.StatusForbidden:
40 if key.MarkPermanent() {
41 logger.Warn(ctx, "key marked permanent",
42 slog.F("provider", providerName),
43 slog.F("api_key_hint", key.Hint()),
44 slog.F("status", statusCode))
45 }
46 return true
47 default:
48 logger.Debug(ctx, "status is not a key failover trigger",
49 slog.F("provider", providerName),
50 slog.F("status", statusCode))
51 return false
52 }
53}

Callers 5

markKeyOnErrorMethod · 0.92
markKeyOnErrorMethod · 0.92
markKeyOnErrorMethod · 0.92
TestMarkKeyOnStatusFunction · 0.92
RoundTripMethod · 0.85

Calls 5

ParseRetryAfterFunction · 0.85
MarkTemporaryMethod · 0.80
HintMethod · 0.80
MarkPermanentMethod · 0.80
InfoMethod · 0.45

Tested by 1

TestMarkKeyOnStatusFunction · 0.74