Skip to content

Commit

Permalink
fix: fix retry logic again
Browse files Browse the repository at this point in the history
  • Loading branch information
rvazarkar committed Feb 6, 2024
1 parent 7d7e669 commit efa6a28
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/CommonLib/LDAPUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private static readonly ConcurrentDictionary<string, ResolvedWellKnownPrincipal>
private readonly PortScanner _portScanner;
private LDAPConfig _ldapConfig = new();
private readonly ManualResetEvent _connectionResetEvent = new(false);
private readonly object _lockObj = new();


/// <summary>
Expand Down Expand Up @@ -884,12 +885,15 @@ public IEnumerable<ISearchResultEntry> QueryLDAP(string ldapFilter, SearchScope

//Always increment retry count
retryCount++;

//If we are the holders of the reset event, we need to do logic to reset the connection
if (_connectionResetEvent.Reset())
//Attempt to acquire a lock
if (Monitor.TryEnter(_lockObj))
{
//If we've acquired the lock, we want to immediately signal our reset event so everyone else waits
_connectionResetEvent.Reset();
try
{
//Sleep for our backoff
Thread.Sleep(backoffDelay);
//Explicitly skip the cache so we don't get the same connection back
conn = CreateNewConnection(domainName, globalCatalog, true);
Expand All @@ -904,12 +908,15 @@ public IEnumerable<ISearchResultEntry> QueryLDAP(string ldapFilter, SearchScope
}
finally
{
//Reset our event + release the lock
_connectionResetEvent.Set();
Monitor.Exit(_lockObj);
}
}
else
{
//If someone else is holding the reset event, we want to just wait and then pull the newly created connection out of the cache
//This event will be released after the first entrant thread is done making a new connection
_connectionResetEvent.WaitOne();
conn = CreateNewConnection(domainName, globalCatalog);
}
Expand Down

0 comments on commit efa6a28

Please sign in to comment.