From 6d963d2f8275d527f9ef54b2a5b1fd02b7eaf3b4 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 3 Dec 2022 01:53:02 +0100 Subject: [PATCH 01/10] Retry-After from headers if StatusCode is 429 --- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 21be97d567e..164af3590b0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1439,10 +1439,20 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM // When MaximumRetryCount is not specified, the totalRequests == 1. if (totalRequests > 1 && ShouldRetry(response.StatusCode)) { + // If the status code is 429 get the retry interval from the Headers. + if ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out var retryAfter)) + { + WebSession.RetryIntervalInSeconds = Convert.ToInt32(retryAfter); + } + else + { + WebSession.RetryIntervalInSeconds = RetryIntervalSec; + } + string retryMessage = string.Format( CultureInfo.CurrentCulture, WebCmdletStrings.RetryVerboseMsg, - RetryIntervalSec, + WebSession.RetryIntervalInSeconds, response.StatusCode); WriteVerbose(retryMessage); From 9713370fb31a6089941815f8ee200a5f474baa71 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 3 Dec 2022 02:26:37 +0100 Subject: [PATCH 02/10] convert if-else to ternary operator --- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 164af3590b0..0b6a0f8f7de 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1440,14 +1440,7 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM if (totalRequests > 1 && ShouldRetry(response.StatusCode)) { // If the status code is 429 get the retry interval from the Headers. - if ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out var retryAfter)) - { - WebSession.RetryIntervalInSeconds = Convert.ToInt32(retryAfter); - } - else - { - WebSession.RetryIntervalInSeconds = RetryIntervalSec; - } + WebSession.RetryIntervalInSeconds = ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out var retryAfter)) ? Convert.ToInt32(retryAfter) : RetryIntervalSec; string retryMessage = string.Format( CultureInfo.CurrentCulture, From 1a4940a99e1187f5a05d16d20c517cc039619927 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 3 Dec 2022 15:19:10 +0100 Subject: [PATCH 03/10] use explicit type; fix Convert.ToInt32 --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 0b6a0f8f7de..4c9ca18227f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -7,6 +7,7 @@ using System.Collections.ObjectModel; using System.Globalization; using System.IO; +using System.Linq; using System.Management.Automation; using System.Net; using System.Net.Http; @@ -1440,7 +1441,7 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM if (totalRequests > 1 && ShouldRetry(response.StatusCode)) { // If the status code is 429 get the retry interval from the Headers. - WebSession.RetryIntervalInSeconds = ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out var retryAfter)) ? Convert.ToInt32(retryAfter) : RetryIntervalSec; + WebSession.RetryIntervalInSeconds = ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) ? Convert.ToInt32(retryAfter.FirstOrDefault(RetryIntervalSec.ToString())) : RetryIntervalSec; string retryMessage = string.Format( CultureInfo.CurrentCulture, From bc97fe3088c851079a2cbff4f22e0efbce3d86ae Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 3 Dec 2022 15:40:18 +0100 Subject: [PATCH 04/10] Removed redundant code --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 4c9ca18227f..60e53bdfcf6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -690,9 +690,6 @@ internal virtual void PrepareSession() if (MaximumRetryCount > 0) { WebSession.MaximumRetryCount = MaximumRetryCount; - - // only set retry interval if retry count is set. - WebSession.RetryIntervalInSeconds = RetryIntervalSec; } } @@ -1440,7 +1437,8 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM // When MaximumRetryCount is not specified, the totalRequests == 1. if (totalRequests > 1 && ShouldRetry(response.StatusCode)) { - // If the status code is 429 get the retry interval from the Headers. + // Only set retry interval if MaximumRetryCount is specified, if + // the status code is 429 get the retry interval from the Headers. WebSession.RetryIntervalInSeconds = ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) ? Convert.ToInt32(retryAfter.FirstOrDefault(RetryIntervalSec.ToString())) : RetryIntervalSec; string retryMessage = string.Format( From d2043229e60918994333b7ad45c825be2d986db7 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 5 Dec 2022 18:35:05 +0100 Subject: [PATCH 05/10] define local variable instead of WebSession.RetryIntervalInSeconds --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 60e53bdfcf6..72e9851297c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1439,18 +1439,18 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM { // Only set retry interval if MaximumRetryCount is specified, if // the status code is 429 get the retry interval from the Headers. - WebSession.RetryIntervalInSeconds = ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) ? Convert.ToInt32(retryAfter.FirstOrDefault(RetryIntervalSec.ToString())) : RetryIntervalSec; + int RetryIntervalInSeconds = ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) ? Convert.ToInt32(retryAfter.FirstOrDefault(RetryIntervalSec.ToString())) : RetryIntervalSec; string retryMessage = string.Format( CultureInfo.CurrentCulture, WebCmdletStrings.RetryVerboseMsg, - WebSession.RetryIntervalInSeconds, + RetryIntervalInSeconds, response.StatusCode); WriteVerbose(retryMessage); _cancelToken = new CancellationTokenSource(); - Task.Delay(WebSession.RetryIntervalInSeconds * 1000, _cancelToken.Token).GetAwaiter().GetResult(); + Task.Delay(RetryIntervalInSeconds * 1000, _cancelToken.Token).GetAwaiter().GetResult(); _cancelToken.Cancel(); _cancelToken = null; From 69e322d9a72384354ad1a8b67c937400e9437bf0 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 5 Dec 2022 19:35:15 +0100 Subject: [PATCH 06/10] R -> r --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 72e9851297c..8bcec010431 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1439,18 +1439,18 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM { // Only set retry interval if MaximumRetryCount is specified, if // the status code is 429 get the retry interval from the Headers. - int RetryIntervalInSeconds = ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) ? Convert.ToInt32(retryAfter.FirstOrDefault(RetryIntervalSec.ToString())) : RetryIntervalSec; + int retryIntervalInSeconds = ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) ? Convert.ToInt32(retryAfter.FirstOrDefault(RetryIntervalSec.ToString())) : RetryIntervalSec; string retryMessage = string.Format( CultureInfo.CurrentCulture, WebCmdletStrings.RetryVerboseMsg, - RetryIntervalInSeconds, + retryIntervalInSeconds, response.StatusCode); WriteVerbose(retryMessage); _cancelToken = new CancellationTokenSource(); - Task.Delay(RetryIntervalInSeconds * 1000, _cancelToken.Token).GetAwaiter().GetResult(); + Task.Delay(retryIntervalInSeconds * 1000, _cancelToken.Token).GetAwaiter().GetResult(); _cancelToken.Cancel(); _cancelToken = null; From 59df9aa99c9a88778cfbc5881090c7f2bea17e46 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 6 Dec 2022 01:54:16 +0100 Subject: [PATCH 07/10] remove linq; go back to if-else; try-catch --- .../Common/WebRequestPSCmdlet.Common.cs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 8bcec010431..121a7f32e31 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -7,7 +7,6 @@ using System.Collections.ObjectModel; using System.Globalization; using System.IO; -using System.Linq; using System.Management.Automation; using System.Net; using System.Net.Http; @@ -1439,8 +1438,25 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM { // Only set retry interval if MaximumRetryCount is specified, if // the status code is 429 get the retry interval from the Headers. - int retryIntervalInSeconds = ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) ? Convert.ToInt32(retryAfter.FirstOrDefault(RetryIntervalSec.ToString())) : RetryIntervalSec; - + int retryIntervalInSeconds; + if ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) + { + try + { + IEnumerator enumerator = retryAfter.GetEnumerator(); + enumerator.MoveNext(); + retryIntervalInSeconds = Convert.ToInt32(enumerator.Current); + } + catch + { + retryIntervalInSeconds = RetryIntervalSec; + } + } + else + { + retryIntervalInSeconds = RetryIntervalSec; + } + string retryMessage = string.Format( CultureInfo.CurrentCulture, WebCmdletStrings.RetryVerboseMsg, From 32b455ac342b771603178a60f43d6ae335f4866f Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 6 Dec 2022 10:04:54 +0100 Subject: [PATCH 08/10] revert to using websession --- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 121a7f32e31..20a3b1a61d9 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -689,6 +689,9 @@ internal virtual void PrepareSession() if (MaximumRetryCount > 0) { WebSession.MaximumRetryCount = MaximumRetryCount; + + // Only set retry interval if retry count is set. + WebSession.RetryIntervalInSeconds = RetryIntervalSec; } } @@ -1436,8 +1439,7 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM // When MaximumRetryCount is not specified, the totalRequests == 1. if (totalRequests > 1 && ShouldRetry(response.StatusCode)) { - // Only set retry interval if MaximumRetryCount is specified, if - // the status code is 429 get the retry interval from the Headers. + // If the status code is 429 get the retry interval from the Headers. int retryIntervalInSeconds; if ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) { @@ -1449,12 +1451,12 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM } catch { - retryIntervalInSeconds = RetryIntervalSec; + retryIntervalInSeconds = WebSession.RetryIntervalInSeconds; } } else { - retryIntervalInSeconds = RetryIntervalSec; + retryIntervalInSeconds = WebSession.RetryIntervalInSeconds; } string retryMessage = string.Format( From 3d5757c614d31e04fd6baa498b1e4d94aded19e5 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 6 Dec 2022 14:07:10 +0100 Subject: [PATCH 09/10] Update with suggestion --- .../Common/WebRequestPSCmdlet.Common.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 20a3b1a61d9..ff6eeaca0fe 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1439,25 +1439,25 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM // When MaximumRetryCount is not specified, the totalRequests == 1. if (totalRequests > 1 && ShouldRetry(response.StatusCode)) { + int retryIntervalInSeconds = WebSession.RetryIntervalInSeconds; + // If the status code is 429 get the retry interval from the Headers. - int retryIntervalInSeconds; - if ((int)response.StatusCode == 429 && response.Headers.TryGetValues("Retry-After", out IEnumerable retryAfter)) + // Ignore broken header and its value. + if ((int)response.StatusCode == HttpStatusCode.Conflict && response.Headers.TryGetValues(HttpKnownHeaderNames.RetryAfter, out IEnumerable retryAfter)) { try { IEnumerator enumerator = retryAfter.GetEnumerator(); - enumerator.MoveNext(); - retryIntervalInSeconds = Convert.ToInt32(enumerator.Current); + if (enumerator.MoveNext()) + { + retryIntervalInSeconds = Convert.ToInt32(enumerator.Current); + } } catch { - retryIntervalInSeconds = WebSession.RetryIntervalInSeconds; + // Ignore broken header. } } - else - { - retryIntervalInSeconds = WebSession.RetryIntervalInSeconds; - } string retryMessage = string.Format( CultureInfo.CurrentCulture, From 6da66415955b346bf180dee3c22399f636c3ef84 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 6 Dec 2022 14:45:20 +0100 Subject: [PATCH 10/10] remove (int) --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index ff6eeaca0fe..c47ee9641ca 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1443,7 +1443,7 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM // If the status code is 429 get the retry interval from the Headers. // Ignore broken header and its value. - if ((int)response.StatusCode == HttpStatusCode.Conflict && response.Headers.TryGetValues(HttpKnownHeaderNames.RetryAfter, out IEnumerable retryAfter)) + if (response.StatusCode is HttpStatusCode.Conflict && response.Headers.TryGetValues(HttpKnownHeaderNames.RetryAfter, out IEnumerable retryAfter)) { try {