From 5743f188af805e9fdc980f219160df42ff30379a Mon Sep 17 00:00:00 2001 From: John Du Hart Date: Tue, 21 Aug 2018 18:01:02 -0400 Subject: [PATCH 1/4] Fix concurrency issue in MAuthRequestRetrier --- src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 2 +- .../MAuthRequestRetrier.cs | 54 ++++++++++++------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index d592340..527cd0b 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -62,7 +62,7 @@ public async Task AuthenticateRequest(HttpRequestMessage request) private async Task GetApplicationInfo(Guid applicationUuid) => await (await retrier.GetSuccessfulResponse(applicationUuid, CreateRequest, - remainingAttempts: (int)options.MAuthServiceRetryPolicy + 1)) + requestAttempts: (int)options.MAuthServiceRetryPolicy + 1)) .Content .FromResponse(); diff --git a/src/Medidata.MAuth.Core/MAuthRequestRetrier.cs b/src/Medidata.MAuth.Core/MAuthRequestRetrier.cs index 21c7bbd..13eb1d2 100644 --- a/src/Medidata.MAuth.Core/MAuthRequestRetrier.cs +++ b/src/Medidata.MAuth.Core/MAuthRequestRetrier.cs @@ -10,7 +10,6 @@ namespace Medidata.MAuth.Core internal class MAuthRequestRetrier { private readonly HttpClient client; - private RetriedRequestException exception; public MAuthRequestRetrier(MAuthOptionsBase options) { @@ -35,32 +34,51 @@ public MAuthRequestRetrier(MAuthOptionsBase options) } public async Task GetSuccessfulResponse(Guid applicationUuid, - Func requestFactory, int remainingAttempts) + Func requestFactory, int requestAttempts) { - var request = requestFactory?.Invoke(applicationUuid); + if (requestFactory == null) + throw new ArgumentNullException(nameof(requestFactory)); - if (request == null) - throw new ArgumentNullException( - nameof(requestFactory), - "No request function provided or the provided request function resulted null request." + if (requestAttempts <= 0) + throw new ArgumentOutOfRangeException( + nameof(requestAttempts), + requestAttempts, + "Request attempts was out of range. Must be greater than zero." ); - exception = exception ?? new RetriedRequestException( - $"Could not get a successful response from the MAuth Service after {remainingAttempts} attempts. " + - "Please see the responses for each attempt in the exception's Responses field.") + RetriedRequestException exception = null; + + int remainingAttempts = requestAttempts; + while (remainingAttempts > 0) { - Request = request - }; + var request = requestFactory?.Invoke(applicationUuid); - if (remainingAttempts == 0) - throw exception; + if (request == null) + throw new ArgumentException( + "The provided request factory function resulted null request.", + nameof(requestFactory) + ); - var result = await client.SendAsync(request).ConfigureAwait(continueOnCapturedContext: false); + var result = await client.SendAsync(request).ConfigureAwait(continueOnCapturedContext: false); - exception.Responses.Add(result); + if (result.IsSuccessStatusCode) + return result; - return result.IsSuccessStatusCode ? - result : await GetSuccessfulResponse(applicationUuid, requestFactory, remainingAttempts - 1); + if (exception == null) + { + exception = new RetriedRequestException( + $"Could not get a successful response from the MAuth Service after {requestAttempts} attempts. " + + "Please see the responses for each attempt in the exception's Responses field.") + { + Request = request + }; + } + + exception.Responses.Add(result); + remainingAttempts--; + } + + throw exception; } } } \ No newline at end of file From 28f6178061455302ec031025211869ba69c435d1 Mon Sep 17 00:00:00 2001 From: John Du Hart Date: Tue, 21 Aug 2018 20:40:05 -0400 Subject: [PATCH 2/4] Increasing version to 3.0.3 Requested by Laszlo. --- version.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.props b/version.props index 90cd1d6..152c914 100644 --- a/version.props +++ b/version.props @@ -1,6 +1,6 @@  - 3.0.2 + 3.0.3 From 6361e9bccfb90622a3b84bb52bc70ab8f7f0ab82 Mon Sep 17 00:00:00 2001 From: Laszlo Schreck Date: Mon, 27 Aug 2018 11:12:34 +0900 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d1244f..2ee7b2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changes in Medidata.MAuth +## v3.0.2 +- **[Core]** Fixed concurrency and memory issues with the `MAuthRequestRetrier` + ## v3.0.1 - **[Core]** Removed constraint for the application uuids to be only version 4. Now the MAuth header validation won't throw error if the provided uuid is not version 4. From a6403e854d25b7b32a180737b19b25b516e8110d Mon Sep 17 00:00:00 2001 From: Laszlo Schreck Date: Mon, 27 Aug 2018 11:13:44 +0900 Subject: [PATCH 4/4] Fix version numbers in the CHANGELOG --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ee7b2a..6a317e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,11 @@ # Changes in Medidata.MAuth -## v3.0.2 +## v3.0.3 - **[Core]** Fixed concurrency and memory issues with the `MAuthRequestRetrier` +## v3.0.2 +- **[Core]** Exposed the `MAuthOptionsBase.MAuthServerHandler` property as public in order to be able to inject custom handlers for the MAuth server communication. + ## v3.0.1 - **[Core]** Removed constraint for the application uuids to be only version 4. Now the MAuth header validation won't throw error if the provided uuid is not version 4.