From aa06367b32a9694fac9a8dea461f0b1d28a476b5 Mon Sep 17 00:00:00 2001 From: Ken Christensen Date: Sat, 10 Apr 2021 15:34:03 +0200 Subject: [PATCH] Stop using a single variable for nonces When sending multiple requests in parallel, there is a chance they will be using the same nonce, as it's stored as a single string. Instead, convert to using a concurrenct queue and enqueue/dequeue as requests are going out and responses coming in. --- src/Libraries/ACMELib/ACMERestClient.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Libraries/ACMELib/ACMERestClient.cs b/src/Libraries/ACMELib/ACMERestClient.cs index 8dfc88a..405da5e 100644 --- a/src/Libraries/ACMELib/ACMERestClient.cs +++ b/src/Libraries/ACMELib/ACMERestClient.cs @@ -1,6 +1,7 @@ namespace Kenc.ACMELib { using System; + using System.Collections.Concurrent; using System.Diagnostics; using System.IO; using System.Linq; @@ -32,7 +33,8 @@ public class ACMERestClient : IRestClient private readonly Jws jws; private readonly string UserAgent; - private string nonce = string.Empty; + + private readonly ConcurrentQueue nonces = new ConcurrentQueue(); /// /// Initializes a new instance of the class. @@ -92,7 +94,7 @@ public ACMERestClient(Jws jws) if (message != null) { - if (string.IsNullOrEmpty(nonce)) + if (!nonces.TryDequeue(out var nonce)) { throw new NoNonceException(); } @@ -125,7 +127,11 @@ public ACMERestClient(Jws jws) if (response.Headers.AllKeys.Contains("Replay-Nonce")) { - nonce = response.Headers.GetValues("Replay-Nonce").First(); + var nonce = response.Headers.GetValues("Replay-Nonce").First(); + if (!string.IsNullOrWhiteSpace(nonce)) + { + nonces.Enqueue(nonce); + } } var responseBody = string.Empty;