aboutsummaryrefslogtreecommitdiff
path: root/pkg/api/api0
diff options
context:
space:
mode:
authorpg9182 <96569817+pg9182@users.noreply.github.com>2023-03-04 00:41:31 -0500
committerpg9182 <96569817+pg9182@users.noreply.github.com>2023-03-04 00:49:32 -0500
commit586b75749f297c84df3a004f183de49cbdc44013 (patch)
treebbf01b4b2da8e4b3d4a3a17ff81ab27c88a39fcc /pkg/api/api0
parent386bef353984f435927461e6110ada87678369e3 (diff)
downloadAtlas-586b75749f297c84df3a004f183de49cbdc44013.tar.gz
Atlas-586b75749f297c84df3a004f183de49cbdc44013.zip
pkg/api/api0: Terminate client request handler early if request was canceled
This prevents unnecessary backend requests, while also reducing noise in the metrics.
Diffstat (limited to 'pkg/api/api0')
-rw-r--r--pkg/api/api0/client.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/pkg/api/api0/client.go b/pkg/api/api0/client.go
index 45f006b..6e02603 100644
--- a/pkg/api/api0/client.go
+++ b/pkg/api/api0/client.go
@@ -143,6 +143,12 @@ func (h *Handler) handleClientOriginAuth(w http.ResponseWriter, r *http.Request)
return
}
+ select {
+ case <-r.Context().Done(): // check if the request was canceled to avoid making unnecessary requests
+ return
+ default:
+ }
+
if !h.InsecureDevNoCheckPlayerAuth {
token := r.URL.Query().Get("token")
if token == "" {
@@ -211,8 +217,20 @@ func (h *Handler) handleClientOriginAuth(w http.ResponseWriter, r *http.Request)
}
}
+ select {
+ case <-r.Context().Done(): // check if the request was canceled to avoid making unnecessary requests
+ return
+ default:
+ }
+
username := h.lookupUsername(r, uid)
+ select {
+ case <-r.Context().Done(): // check if the request was canceled to avoid making unnecessary requests
+ return
+ default:
+ }
+
// note: there's small chance of race conditions here if there are multiple
// concurrent origin_auth calls, but since we only ever support one session
// at a time per uid, it's not a big deal which token gets saved (if it is
@@ -340,6 +358,11 @@ func (h *Handler) lookupUsername(r *http.Request, uid uint64) (username string)
// lookupUsernameOrigin gets the username for uid from the Origin API, returning
// an empty string if a username does not exist for the uid, and false on error.
func (h *Handler) lookupUsernameOrigin(r *http.Request, uid uint64) (username string, ok bool) {
+ select {
+ case <-r.Context().Done(): // check if the request was canceled to avoid polluting the metrics
+ return
+ default:
+ }
if h.OriginAuthMgr == nil {
hlog.FromRequest(r).Error().
Str("username_source", "origin").
@@ -402,6 +425,11 @@ func (h *Handler) lookupUsernameOrigin(r *http.Request, uid uint64) (username st
// lookupUsernameEAX gets the username for uid from the EAX API, returning an
// empty string if a username does not exist for the uid, and false on error.
func (h *Handler) lookupUsernameEAX(r *http.Request, uid uint64) (username string, ok bool) {
+ select {
+ case <-r.Context().Done(): // check if the request was canceled to avoid polluting the metrics
+ return
+ default:
+ }
if h.EAXClient == nil {
hlog.FromRequest(r).Error().
Str("username_source", "eax").