From 586b75749f297c84df3a004f183de49cbdc44013 Mon Sep 17 00:00:00 2001 From: pg9182 <96569817+pg9182@users.noreply.github.com> Date: Sat, 4 Mar 2023 00:41:31 -0500 Subject: pkg/api/api0: Terminate client request handler early if request was canceled This prevents unnecessary backend requests, while also reducing noise in the metrics. --- pkg/api/api0/client.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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"). -- cgit v1.2.3