From ea7b944114628715c8e7c68d4812701e410740b2 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Thu, 27 Oct 2022 10:46:00 +0200 Subject: [PATCH] Include http errors in GetInventoryAsync() And stop actively matching on first 429 --- ArchiSteamFarm/Core/RemoteCommunication.cs | 13 ++++++++++++- ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs | 8 ++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ArchiSteamFarm/Core/RemoteCommunication.cs b/ArchiSteamFarm/Core/RemoteCommunication.cs index 0dde46a1a..788dc84ef 100644 --- a/ArchiSteamFarm/Core/RemoteCommunication.cs +++ b/ArchiSteamFarm/Core/RemoteCommunication.cs @@ -407,6 +407,7 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { byte maxTradeHoldDuration = ASF.GlobalConfig?.MaxTradeHoldDuration ?? GlobalConfig.DefaultMaxTradeHoldDuration; byte totalMatches = 0; + bool rateLimited = false; HashSet<(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity)> skippedSetsThisRound = new(); @@ -443,6 +444,16 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { } catch (HttpRequestException e) { Bot.ArchiLogger.LogGenericWarningException(e); +#if NETFRAMEWORK + if (e.StatusCode == (HttpStatusCode) 429) { +#else + if (e.StatusCode == HttpStatusCode.TooManyRequests) { +#endif + rateLimited = true; + + break; + } + continue; } catch (Exception e) { Bot.ArchiLogger.LogGenericException(e); @@ -702,6 +713,6 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable { Bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.ActivelyMatchingItemsRound, skippedSetsThisRound.Count)); // Keep matching when we either traded something this round (so it makes sense for a refresh) or if we didn't try all available bots yet (so it makes sense to keep going) - return ((totalMatches > 0) && ((skippedSetsThisRound.Count > 0) || triedSteamIDs.Values.All(static data => data.Tries < 2)), skippedSetsThisRound.Count > 0); + return (!rateLimited && (totalMatches > 0) && ((skippedSetsThisRound.Count > 0) || triedSteamIDs.Values.All(static data => data.Tries < 2)), skippedSetsThisRound.Count > 0); } } diff --git a/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs b/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs index b1472c989..516397ac5 100644 --- a/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs +++ b/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs @@ -173,12 +173,16 @@ public sealed class ArchiWebHandler : IDisposable { await Task.Delay(rateLimitingDelay).ConfigureAwait(false); } - response = await UrlGetToJsonObjectWithSession(request, requestOptions: WebBrowser.ERequestOptions.ReturnServerErrors | WebBrowser.ERequestOptions.AllowInvalidBodyOnErrors, rateLimitingDelay: rateLimitingDelay).ConfigureAwait(false); + response = await UrlGetToJsonObjectWithSession(request, requestOptions: WebBrowser.ERequestOptions.ReturnClientErrors | WebBrowser.ERequestOptions.ReturnServerErrors | WebBrowser.ERequestOptions.AllowInvalidBodyOnErrors, rateLimitingDelay: rateLimitingDelay).ConfigureAwait(false); if (response == null) { throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(response))); } + if (response.StatusCode.IsClientErrorCode()) { + throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, response.StatusCode), null, response.StatusCode); + } + if (response.StatusCode.IsServerErrorCode()) { if (string.IsNullOrEmpty(response.Content?.Error)) { // This is a generic server error without a reason, try again @@ -189,7 +193,7 @@ public sealed class ArchiWebHandler : IDisposable { // This is actually client error with a reason, so it doesn't make sense to retry // ReSharper disable once RedundantSuppressNullableWarningExpression - required for .NET Framework - throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, response.Content!.Error)); + throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, response.Content!.Error), null, response.StatusCode); } } } finally {