Skip to content

Commit 94f4597

Browse files
committed
chore: some small fixes
1 parent 2553fd3 commit 94f4597

4 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/CommonLib/ConcurrentHashSet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ public IEnumerable<string> Values() {
5353
return _backingDictionary.Keys;
5454
}
5555

56+
public void Clear() {
57+
_backingDictionary.Clear();
58+
}
59+
5660
public void Dispose() {
5761
_backingDictionary = null;
5862
GC.SuppressFinalize(this);
5963
}
64+
6065
}

src/CommonLib/ConnectionPoolManager.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,8 @@ public void ReleaseConnection(LdapConnectionWrapper connectionWrapper, bool conn
8383
}
8484

8585
var resolved = ResolveIdentifier(identifier);
86-
if (!_pools.TryGetValue(resolved, out var pool)) {
87-
pool = new LdapConnectionPool(identifier, resolved, _ldapConfig, scanner: _portScanner);
88-
pool = _pools.GetOrAdd(resolved, pool);
89-
}
90-
86+
var pool = _pools.GetOrAdd(resolved, _ => new LdapConnectionPool(identifier, resolved, _ldapConfig, scanner: _portScanner));
87+
9188
return (true, pool);
9289
}
9390

src/CommonLib/LdapConnectionPool.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public async IAsyncEnumerable<LdapResult<IDirectoryObject>> Query(LdapQueryParam
203203
new LabelValues([nameof(LdapConnectionPool), _poolIdentifier]));
204204
errorResult = LdapResult<IDirectoryObject>.Fail(
205205
$"Query - Caught unrecoverable ldap exception: {le.Message} (ServerMessage: {le.ServerErrorMessage}) (ErrorCode: {le.ErrorCode})",
206-
queryParameters);
206+
queryParameters, le.ErrorCode);
207207
}
208208
catch (Exception e) {
209209
/*
@@ -791,7 +791,6 @@ private bool CallDsGetDcName(string domainName, out NetAPIStructs.DomainControll
791791
if (!_globalCatalogConnection.TryTake(out var connectionWrapper)) {
792792
var (success, connection, message) = await CreateNewConnection(true);
793793
if (!success) {
794-
//If we didn't get a connection, immediately release the semaphore so we don't have hanging ones
795794
return (false, null, message);
796795
}
797796

test/unit/LdapConnectionPoolTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ private static void AddExclusionDomain(string identifier) {
1818
excludedDomains.Add(identifier);
1919
}
2020

21+
private static void ResetExclusionDomain() {
22+
var excludedDomainsField = typeof(LdapConnectionPool)
23+
.GetField("ExcludedDomains", BindingFlags.Static | BindingFlags.NonPublic);
24+
25+
var excludedDomains = (ConcurrentHashSet)excludedDomainsField.GetValue(null);
26+
27+
excludedDomains.Clear();
28+
}
29+
2130
private static ConcurrentBag<LdapConnectionWrapper> GetConnectionsBag(LdapConnectionPool pool) {
2231
var field = typeof(LdapConnectionPool)
2332
.GetField("_connections", BindingFlags.Instance | BindingFlags.NonPublic);
@@ -33,6 +42,7 @@ private static ConcurrentBag<LdapConnectionWrapper> GetGlobalCatalogConnectionsB
3342
[Fact]
3443
public async Task LdapConnectionPool_ExcludedDomains_ShouldExitEarly()
3544
{
45+
ResetExclusionDomain();
3646
var mockLogger = new Mock<ILogger>();
3747
var ldapConfig = new LdapConfig();
3848
var connectionPool = new ConnectionPoolManager(ldapConfig, mockLogger.Object);
@@ -47,6 +57,7 @@ public async Task LdapConnectionPool_ExcludedDomains_ShouldExitEarly()
4757
[Fact]
4858
public async Task LdapConnectionPool_ExcludedDomains_NonExcludedShouldntExit()
4959
{
60+
ResetExclusionDomain();
5061
var mockLogger = new Mock<ILogger>();
5162
var ldapConfig = new LdapConfig();
5263
var connectionPool = new ConnectionPoolManager(ldapConfig, mockLogger.Object);
@@ -64,6 +75,7 @@ public async Task LdapConnectionPool_ExcludedDomains_NonExcludedShouldntExit()
6475
[Fact]
6576
public async Task LdapConnectionPool_ExcludedDomains_GlobalCatalog_ShouldExitEarly()
6677
{
78+
ResetExclusionDomain();
6779
var mockLogger = new Mock<ILogger>();
6880
var ldapConfig = new LdapConfig();
6981
var connectionPool = new ConnectionPoolManager(ldapConfig, mockLogger.Object);

0 commit comments

Comments
 (0)