Skip to content

Commit 6ddbd37

Browse files
committed
chore: consistent reset of pagedquery counters
chore: null guard ConcurrentHashSet clear chore: use idisposable to reset exclusion list in tests
1 parent 94f4597 commit 6ddbd37

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

src/CommonLib/ConcurrentHashSet.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ public IEnumerable<string> Values() {
5454
}
5555

5656
public void Clear() {
57-
_backingDictionary.Clear();
57+
_backingDictionary?.Clear();
5858
}
5959

6060
public void Dispose() {
6161
_backingDictionary = null;
6262
GC.SuppressFinalize(this);
6363
}
64-
65-
}
64+
}

src/CommonLib/LdapConnectionPool.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,6 @@ public async IAsyncEnumerable<LdapResult<IDirectoryObject>> PagedQuery(LdapQuery
286286
if (response != null) {
287287
pageResponse = (PageResultResponseControl)response.Controls
288288
.Where(x => x is PageResultResponseControl).DefaultIfEmpty(null).FirstOrDefault();
289-
// Reset retry counter on a successful response so the next page gets a fresh budget
290-
queryRetryCount = 0;
291289
} else if (queryRetryCount == MaxRetries) {
292290
_metric.Observe(LdapMetricDefinitions.FailedRequests, 1,
293291
new LabelValues([nameof(LdapConnectionPool), _poolIdentifier]));
@@ -413,8 +411,9 @@ public async IAsyncEnumerable<LdapResult<IDirectoryObject>> PagedQuery(LdapQuery
413411
continue;
414412
}
415413

416-
// Reset busy retry count after a successfully delivered page so each page starts with a fresh budget
414+
// Reset busy and query retry count after a successfully delivered page so each page starts with a fresh budget
417415
busyRetryCount = 0;
416+
queryRetryCount = 0;
418417

419418
foreach (SearchResultEntry entry in response.Entries) {
420419
if (cancellationToken.IsCancellationRequested) {
@@ -581,6 +580,8 @@ public async IAsyncEnumerable<Result<string>> RangedRetrieval(string distinguish
581580
* If we get a busy error, we want to do an exponential backoff, but maintain the current connection.
582581
* The expectation is that given enough time, the server should stop being busy and service our query appropriately.
583582
*/
583+
_metric.Observe(LdapMetricDefinitions.FailedRequests, 1,
584+
new LabelValues([nameof(LdapConnectionPool), _poolIdentifier]));
584585
busyRetryCount++;
585586
_log.LogDebug("RangedRetrieval - Executing busy backoff for query {Info} (Attempt {Count})",
586587
queryParameters.GetQueryInfo(), busyRetryCount);
@@ -591,6 +592,8 @@ public async IAsyncEnumerable<Result<string>> RangedRetrieval(string distinguish
591592
/*
592593
* Treat a timeout as a busy error
593594
*/
595+
_metric.Observe(LdapMetricDefinitions.FailedRequests, 1,
596+
new LabelValues([nameof(LdapConnectionPool), _poolIdentifier]));
594597
busyRetryCount++;
595598
_log.LogDebug("RangedRetrieval - Timeout: Executing busy backoff for query {Info} (Attempt {Count})",
596599
queryParameters.GetQueryInfo(), busyRetryCount);

test/unit/LdapConnectionPoolTest.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Concurrent;
23
using System.DirectoryServices.Protocols;
34
using System.Reflection;
@@ -7,8 +8,12 @@
78
using SharpHoundCommonLib;
89
using Xunit;
910

10-
public class LdapConnectionPoolTest
11+
public class LdapConnectionPoolTest : IDisposable
1112
{
13+
public void Dispose() {
14+
ResetExclusionDomain();
15+
}
16+
1217
private static void AddExclusionDomain(string identifier) {
1318
var excludedDomainsField = typeof(LdapConnectionPool)
1419
.GetField("ExcludedDomains", BindingFlags.Static | BindingFlags.NonPublic);
@@ -163,4 +168,4 @@ public void LdapConnectionPool_ReleaseConnection_NonGlobalCatalog_RoutesToConnec
163168

164169
pool.Dispose();
165170
}
166-
}
171+
}

0 commit comments

Comments
 (0)