-
Notifications
You must be signed in to change notification settings - Fork 0
Fix Loop on 429 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,21 @@ package lib | |
| import ( | ||
| "context" | ||
| "errors" | ||
| lru "github.com/hashicorp/golang-lru" | ||
| "github.com/hashicorp/memberlist" | ||
| "github.com/sirupsen/logrus" | ||
| "net/http" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| lru "github.com/hashicorp/golang-lru" | ||
| "github.com/hashicorp/memberlist" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| const ( | ||
| nirnRetryCountHeader = "X-Nirn-Retry-Count" | ||
| maxRetries = 2 | ||
| ) | ||
|
|
||
| type QueueType int64 | ||
|
|
@@ -169,14 +175,26 @@ func (m *QueueManager) routeRequest(addr string, req *http.Request) (*http.Respo | |
| nodeReq, err := http.NewRequestWithContext(req.Context(), req.Method, "http://"+addr+req.URL.Path+"?"+req.URL.RawQuery, req.Body) | ||
| nodeReq.Header = req.Header.Clone() | ||
| nodeReq.Header.Set("nirn-routed-to", addr) | ||
|
|
||
| // Increment retry count header when routing | ||
| retryCount := 0 | ||
| if s := req.Header.Get(nirnRetryCountHeader); s != "" { | ||
| if v, err := strconv.Atoi(s); err == nil { | ||
| retryCount = v | ||
| } | ||
| } | ||
| retryCount++ | ||
| nodeReq.Header.Set(nirnRetryCountHeader, strconv.Itoa(retryCount)) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
Comment on lines
189
to
191
|
||
|
|
||
| logger.WithFields(logrus.Fields{ | ||
| "to": addr, | ||
| "path": req.URL.Path, | ||
| "method": req.Method, | ||
| "to": addr, | ||
| "path": req.URL.Path, | ||
| "method": req.Method, | ||
| "retryCount": retryCount, | ||
| }).Trace("Routing request to node in cluster") | ||
| resp, err := client.Do(nodeReq) | ||
| logger.WithFields(logrus.Fields{ | ||
|
|
@@ -261,11 +279,25 @@ func (m *QueueManager) getOrCreateBearerQueue(token string) (*RequestQueue, erro | |
| } | ||
|
|
||
| func (m *QueueManager) DiscordRequestHandler(resp http.ResponseWriter, req *http.Request) { | ||
| // Check retry count early and fail fast to prevent infinite loops | ||
| if s := req.Header.Get(nirnRetryCountHeader); s != "" { | ||
| if v, err := strconv.Atoi(s); err == nil && v > maxRetries { | ||
| logger.WithFields(logrus.Fields{ | ||
| "retryCount": v, | ||
| "maxRetries": maxRetries, | ||
| "path": req.URL.Path, | ||
| "method": req.Method, | ||
| }).Warn("Request exceeded max retry count, rejecting") | ||
| Generate429(&resp) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| reqStart := time.Now() | ||
| metricsPath := GetMetricsPath(req.URL.Path) | ||
|
|
||
| token := req.Header.Get("Authorization") | ||
| clientId := GetBotId(token) | ||
| clientId := GetBotId(token) | ||
|
|
||
| ConnectionsOpen.With(map[string]string{"route": metricsPath, "method": req.Method, "clientId": clientId}).Inc() | ||
| defer ConnectionsOpen.With(map[string]string{"route": metricsPath, "method": req.Method, "clientId": clientId}).Dec() | ||
|
Comment on lines
302
to
303
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new retry count logic introduced in this PR (tracking requests via the X-Nirn-Retry-Count header and enforcing maxRetries) lacks test coverage. Consider adding tests that verify: 1) the header is properly incremented when routing requests, 2) requests are rejected when exceeding maxRetries, and 3) the early rejection at the DiscordRequestHandler entry point works correctly.