Review fixes

This commit is contained in:
r.kuznetsov 2018-03-05 16:24:48 +03:00 committed by Daria Volvenkova
parent 4481ab409c
commit adcd71ac6f
3 changed files with 17 additions and 10 deletions

View file

@ -367,7 +367,7 @@ void Statistics::SendToServer()
auto const connectionStatus = GetPlatform().ConnectionStatus();
if (connectionStatus == Platform::EConnectionType::CONNECTION_WIFI)
{
for (auto it = m_metadataCache.begin(); it != m_metadataCache.end();)
for (auto it = m_metadataCache.begin(); it != m_metadataCache.end(); ++it)
{
auto metadataKey = it->first;
auto metadata = it->second;

View file

@ -279,13 +279,8 @@ std::vector<std::string> LocalAdsManager::GetRequestedCampaigns(std::vector<MwmS
// Skip downloading request if maximum attempts count has reached or
// we are waiting for new attempt.
auto const failedDownloadsIt = m_failedDownloads.find(mwm);
if (failedDownloadsIt != m_failedDownloads.cend() &&
(failedDownloadsIt->second.m_attemptsCount >= kMaxDownloadingAttempts ||
std::chrono::steady_clock::now() <= failedDownloadsIt->second.m_lastDownloading +
failedDownloadsIt->second.m_currentTimeout))
{
if (failedDownloadsIt != m_failedDownloads.cend() && !failedDownloadsIt->second.CanRetry())
continue;
}
std::string const & mwmName = info->GetCountryName();
auto campaignIt = m_campaigns.find(mwmName);
@ -332,18 +327,20 @@ bool LocalAdsManager::DownloadCampaign(MwmSet::MwmId const & mwmId, std::vector<
std::lock_guard<std::mutex> lock(m_campaignsMutex);
if (!success)
{
bool const isAbsent = request.ErrorCode() == 404;
auto const it = m_failedDownloads.find(mwmId);
if (it == m_failedDownloads.cend())
{
m_failedDownloads.insert(std::make_pair(mwmId, BackoffStats(std::chrono::steady_clock::now(),
kFailedDownloadingTimeout,
1 /* m_attemptsCount */)));
1 /* m_attemptsCount */, isAbsent)));
}
else
{
// Here we increase timeout multiplying by 2.
it->second.m_currentTimeout = std::chrono::seconds(it->second.m_currentTimeout.count() * 2);
it->second.m_attemptsCount++;
it->second.m_fileIsAbsent = isAbsent;
}
return false;
}
@ -367,7 +364,7 @@ void LocalAdsManager::ProcessRequests(std::set<Request> && requests)
if (!mwm.IsAlive())
continue;
std::string const countryName = mwm.GetInfo()->GetCountryName();
std::string const & countryName = mwm.GetInfo()->GetCountryName();
if (type == RequestType::Download)
{
// Download campaign data from server.
@ -540,3 +537,9 @@ std::string LocalAdsManager::GetCompanyUrl(FeatureID const & featureId) const
{
return MakeCampaignPageURL(featureId);
}
bool LocalAdsManager::BackoffStats::CanRetry() const
{
return !m_fileIsAbsent && m_attemptsCount < kMaxDownloadingAttempts &&
std::chrono::steady_clock::now() > (m_lastDownloading + m_currentTimeout);
}

View file

@ -113,15 +113,19 @@ private:
BackoffStats() = default;
BackoffStats(std::chrono::steady_clock::time_point lastDownloading,
std::chrono::seconds currentTimeout,
uint8_t attemptsCount)
uint8_t attemptsCount, bool fileIsAbsent)
: m_lastDownloading(lastDownloading)
, m_currentTimeout(currentTimeout)
, m_attemptsCount(attemptsCount)
, m_fileIsAbsent(fileIsAbsent)
{}
std::chrono::steady_clock::time_point m_lastDownloading = {};
std::chrono::seconds m_currentTimeout = std::chrono::seconds(0);
uint8_t m_attemptsCount = 0;
bool m_fileIsAbsent = false;
bool CanRetry() const;
};
std::map<MwmSet::MwmId, BackoffStats> m_failedDownloads;