I wrote a custom throttler to handle scenarion when some client (that is making outside calls) is getting backoff exception and needs to sleep before making another API Call.
I have a memory dump with a strange scenario: stuck in an infinte loop, although I see no reason for it. This application has many threads, and all client calls are being throttled using the following code. Call to some API is done like this:
bool res = DoSomeAction([&]{ /* call client m_client.callAPI()*/ return true}
The memory dump shows only 1 thread is working (usually there are 10), and the m_isThrottling is set to true, so the entire application is running forever.
How can this scenario happen? Any suggestion for better implementation (variables with m_ prefix mean class variables, m_throttlingTime and m_isThrottling are static class variables)?
template<typename T>
bool ThrottlerClass::DoSomeAction(T && lambda)
{
for (int retriesCount = 3; retriesCount > 0; --retriesCount)
{
while (m_isThrottling) //dump is stuck here
{
Sleep(10000);
}
try
{
return lambda();
}
catch (const std::exception& ex)
{
int time = m_client->GetThrottlingTimeMS(); //the client got exception making API call and saves it
if (time > 0)
{
ExclusiveLock lock(&m_throttlingMutex); //custom scope mutex
m_isThrottling = true;
if (time > m_throttlingTime)
m_throttlingTime = time;
}
if (m_throttlingTime > 0)
{
Sleep(m_throttlingTime);
{
ExclusiveLock lock(&m_throttlingMutex);
m_isThrottling = false;
m_throttlingTime = 0;
}
}
continue;
}
}
return false;
}