From bb32a47b18f08b2c055d4a82f9f466100df34984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Sun, 13 Aug 2023 18:56:58 +0200 Subject: [PATCH] Abort account loop on context cancellation as well --- faceit.go | 129 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 61 deletions(-) diff --git a/faceit.go b/faceit.go index 77f0fc9..2c8e189 100644 --- a/faceit.go +++ b/faceit.go @@ -445,75 +445,82 @@ func (fc *faceitChecker) requestHandler(w http.ResponseWriter, r *http.Request) length := len(accountList) for _, account := range accountList { - player := &FaceitPlayer{} - playerList = append(playerList, player) - - // parsed[0] is ID64 , parsed[1] is nickname - parsed := strings.Split(account, "_") - if len(parsed) != 2 { - logger.Error("Failed to parse account", - slog.String("account", account)) - continue - } + select { + case <-r.Context().Done(): + logger.Debug("Cancelled", + slog.String("error", r.Context().Err().Error())) + return + default: + player := &FaceitPlayer{} + playerList = append(playerList, player) + + // parsed[0] is ID64 , parsed[1] is nickname + parsed := strings.Split(account, "_") + if len(parsed) != 2 { + logger.Error("Failed to parse account", + slog.String("account", account)) + continue + } - // Information is only needed if multiple accounts are displayed - if length > 1 { - player.SteamLink = "https://steamcommunity.com/profiles/" + parsed[0] - player.SteamName = parsed[1] - } + // Information is only needed if multiple accounts are displayed + if length > 1 { + player.SteamLink = "https://steamcommunity.com/profiles/" + parsed[0] + player.SteamName = parsed[1] + } - playerLogger := logger.With(slog.String("ID", parsed[0]), - slog.String("name", parsed[1])) - playerLogger.Debug("Checking player") + playerLogger := logger.With(slog.String("ID", parsed[0]), + slog.String("name", parsed[1])) + playerLogger.Debug("Checking player") - data, err := fc.getFaceitPlayer(r.Context(), parsed[0]) - if err != nil { - playerLogger.Error("Failed to get Faceit player", - slog.String("error", err.Error())) - player.Error = err.Error() - continue - } + data, err := fc.getFaceitPlayer(r.Context(), parsed[0]) + if err != nil { + playerLogger.Error("Failed to get Faceit player", + slog.String("error", err.Error())) + player.Error = err.Error() + continue + } - var data2 faceitCSGO - err = fc.checkStats(r.Context(), data.PlayerID, &data2) - if err != nil { - playerLogger.Error("Failed to check stats", - slog.String("error", err.Error())) - player.Error = err.Error() - continue - } + var data2 faceitCSGO + err = fc.checkStats(r.Context(), data.PlayerID, &data2) + if err != nil { + playerLogger.Error("Failed to check stats", + slog.String("error", err.Error())) + player.Error = err.Error() + continue + } - banned, reason, err := fc.checkBan(r.Context(), data.PlayerID) - if err != nil { - playerLogger.Error("Failed to check ban status", - slog.String("error", err.Error())) - player.Error = err.Error() - continue - } + banned, reason, err := fc.checkBan(r.Context(), data.PlayerID) + if err != nil { + playerLogger.Error("Failed to check ban status", + slog.String("error", err.Error())) + player.Error = err.Error() + continue + } - var status string - if banned { - status = fmt.Sprintf("Banned for %s on Faceit.", reason) - } else { - status = "" - } + var status string + if banned { + status = fmt.Sprintf("Banned for %s on Faceit.", reason) + } else { + status = "" + } - lastMatch, err := fc.lastMatch(r.Context(), data.PlayerID) - if err != nil { - playerLogger.Error("Failed to retrieve last match", - slog.String("error", err.Error())) - player.Error = err.Error() - continue - } + lastMatch, err := fc.lastMatch(r.Context(), data.PlayerID) + if err != nil { + playerLogger.Error("Failed to retrieve last match", + slog.String("error", err.Error())) + player.Error = err.Error() + continue + } - player.Elo = data.Games.CSGO.Elo - player.Kd = data2.Stats.KD - player.Matches = data2.Stats.Matches - player.Winrate = data2.Stats.Winrate - player.Link = data.FaceitURL - player.Status = status - player.Country = data.Country - player.LastMatch = lastMatch + player.Elo = data.Games.CSGO.Elo + player.Kd = data2.Stats.KD + player.Matches = data2.Stats.Matches + player.Winrate = data2.Stats.Winrate + player.Link = data.FaceitURL + player.Status = status + player.Country = data.Country + player.LastMatch = lastMatch + } } if err := fc.templates.ExecuteTemplate(w, "players.html", playerList); err != nil { -- 2.44.0