mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 12:55:33 +08:00
refs #600, turned CUrlList into a plain vanilla list
* bundled all load balancing in CFailoverUrlList. "Obtain" functions are non-const, no need of mutable members
This commit is contained in:
committed by
Mathew Sutcliffe
parent
89d9b3ad39
commit
1a1e4681fe
@@ -20,22 +20,11 @@ namespace BlackMisc
|
||||
CUrlList::CUrlList() { }
|
||||
|
||||
CUrlList::CUrlList(const CUrlList &other) : CSequence<CUrl>(other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
{ }
|
||||
|
||||
CUrlList &CUrlList::operator =(const CUrlList &other)
|
||||
{
|
||||
if (this == &other) { return *this; }
|
||||
|
||||
QReadLocker readLock(&other.m_lock);
|
||||
int index = other.m_currentIndexDistributedLoad;
|
||||
readLock.unlock(); // avoid deadlock
|
||||
|
||||
QWriteLocker writeLock(&this->m_lock);
|
||||
this->m_currentIndexDistributedLoad = index;
|
||||
return *this;
|
||||
}
|
||||
CUrlList::CUrlList(const CSequence<CUrl> &other) :
|
||||
CSequence<CUrl>(other)
|
||||
{ }
|
||||
|
||||
CUrlList::CUrlList(const QStringList &listOfUrls, bool removeDuplicates)
|
||||
{
|
||||
@@ -47,10 +36,6 @@ namespace BlackMisc
|
||||
}
|
||||
}
|
||||
|
||||
CUrlList::CUrlList(const CSequence<CUrl> &other) :
|
||||
CSequence<CUrl>(other)
|
||||
{ }
|
||||
|
||||
CUrl CUrlList::getRandomUrl() const
|
||||
{
|
||||
if (this->isEmpty()) { return CUrl(); }
|
||||
@@ -83,36 +68,6 @@ namespace BlackMisc
|
||||
return copy.getRandomUrl();
|
||||
}
|
||||
|
||||
CUrl CUrlList::getNextUrl(bool randomStart) const
|
||||
{
|
||||
if (this->isEmpty()) { return CUrl(); }
|
||||
if (this->size() == 1) { return this->front();}
|
||||
if (m_currentIndexDistributedLoad < 0)
|
||||
{
|
||||
// random start point
|
||||
m_currentIndexDistributedLoad = randomStart ?
|
||||
CMathUtils::randomInteger(0, this->size() - 1) :
|
||||
0;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_currentIndexDistributedLoad++;
|
||||
if (m_currentIndexDistributedLoad >= this->size())
|
||||
{
|
||||
m_currentIndexDistributedLoad = 0;
|
||||
}
|
||||
}
|
||||
return (*this)[m_currentIndexDistributedLoad];
|
||||
}
|
||||
|
||||
CUrl CUrlList::getNextUrlWithout(const CUrlList &exclude, bool randomStart) const
|
||||
{
|
||||
CUrlList copy(*this);
|
||||
copy.removeIfIn(exclude);
|
||||
if (copy.isEmpty()) { return CUrl(); }
|
||||
return copy.getNextUrl(randomStart);
|
||||
}
|
||||
|
||||
CUrlList CUrlList::appendPath(const QString &path) const
|
||||
{
|
||||
if (path.isEmpty() || this->isEmpty()) { return (*this); }
|
||||
@@ -144,7 +99,7 @@ namespace BlackMisc
|
||||
int CUrlList::removeDuplicates()
|
||||
{
|
||||
if (this->size() < 2) { return 0; }
|
||||
CUrlList withoutDuplicates(getWithoutDuplicates());
|
||||
const CUrlList withoutDuplicates(getWithoutDuplicates());
|
||||
if (this->size() == withoutDuplicates.size()) { return 0; }
|
||||
int r = this->size() - withoutDuplicates.size();
|
||||
(*this) = withoutDuplicates;
|
||||
@@ -187,16 +142,46 @@ namespace BlackMisc
|
||||
return (m_failedUrls.size() < this->size() && m_failedUrls.size() < m_maxTrials);
|
||||
}
|
||||
|
||||
CUrl CFailoverUrlList::getNextWorkingUrl(const CUrl &failedUrl, bool random)
|
||||
CUrl CFailoverUrlList::obtainNextWorkingUrl(bool random, const CUrl &failedUrl)
|
||||
{
|
||||
if (!failedUrl.isEmpty()) { this->addFailedUrl(failedUrl); }
|
||||
if (!hasMoreUrlsToTry()) { return CUrl(); }
|
||||
CUrl url(this->getNextUrlWithout(this->m_failedUrls, random));
|
||||
const CUrl url(this->obtainNextUrlWithout(random, this->m_failedUrls));
|
||||
if (CNetworkUtils::canConnect(url)) { return url; }
|
||||
if (addFailedUrl(url)) { return getNextWorkingUrl(); }
|
||||
if (addFailedUrl(url)) { return obtainNextWorkingUrl(); }
|
||||
return CUrl();
|
||||
}
|
||||
|
||||
CUrl CFailoverUrlList::obtainNextUrl(bool randomStart)
|
||||
{
|
||||
if (this->isEmpty()) { return CUrl(); }
|
||||
if (this->size() == 1) { return this->front();}
|
||||
if (m_currentIndexDistributedLoad < 0)
|
||||
{
|
||||
// random start point
|
||||
m_currentIndexDistributedLoad = randomStart ?
|
||||
CMathUtils::randomInteger(0, this->size() - 1) :
|
||||
0;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_currentIndexDistributedLoad++;
|
||||
if (m_currentIndexDistributedLoad >= this->size())
|
||||
{
|
||||
m_currentIndexDistributedLoad = 0;
|
||||
}
|
||||
}
|
||||
return (*this)[m_currentIndexDistributedLoad];
|
||||
}
|
||||
|
||||
CUrl CFailoverUrlList::obtainNextUrlWithout(bool randomStart, const CUrlList &exclude) const
|
||||
{
|
||||
CFailoverUrlList copy(*this);
|
||||
copy.removeIfIn(exclude);
|
||||
if (copy.isEmpty()) { return CUrl(); }
|
||||
return copy.obtainNextUrl(randomStart);
|
||||
}
|
||||
|
||||
void CFailoverUrlList::reset(int maxTrials)
|
||||
{
|
||||
this->m_failedUrls.clear();
|
||||
|
||||
@@ -36,15 +36,12 @@ namespace BlackMisc
|
||||
//! Copy constructor (because of mutex)
|
||||
CUrlList(const CUrlList &other);
|
||||
|
||||
//! Copy assignment (because of mutex)
|
||||
CUrlList &operator =(const CUrlList &other);
|
||||
//! Construct from a base class object.
|
||||
CUrlList(const CSequence<CUrl> &other);
|
||||
|
||||
//! By list of URLs
|
||||
explicit CUrlList(const QStringList &listOfUrls, bool removeDuplicates = true);
|
||||
|
||||
//! Construct from a base class object.
|
||||
CUrlList(const CSequence<CUrl> &other);
|
||||
|
||||
//! Random location for distributed load
|
||||
CUrl getRandomUrl() const;
|
||||
|
||||
@@ -54,12 +51,6 @@ namespace BlackMisc
|
||||
//! Random location for distributed load
|
||||
CUrl getRandomWithout(const CUrlList &exclude) const;
|
||||
|
||||
//! Round robin with random start point
|
||||
CUrl getNextUrl(bool randomStart = true) const;
|
||||
|
||||
//! Round robin with random start point
|
||||
CUrl getNextUrlWithout(const CUrlList &exclude, bool randomStart = true) const;
|
||||
|
||||
//! Append path to all URLs
|
||||
CUrlList appendPath(const QString &path) const;
|
||||
|
||||
@@ -71,10 +62,6 @@ namespace BlackMisc
|
||||
|
||||
//! Remove duplicated URL and return number of removed elements
|
||||
int removeDuplicates();
|
||||
|
||||
private:
|
||||
mutable int m_currentIndexDistributedLoad = -1; //!< index for random access
|
||||
mutable QReadWriteLock m_lock; //!< lock (because of mutable members)
|
||||
};
|
||||
|
||||
//! URL list with fail support
|
||||
@@ -105,14 +92,21 @@ namespace BlackMisc
|
||||
//! More URLs to try
|
||||
bool hasMoreUrlsToTry() const;
|
||||
|
||||
//! Next utl from this list
|
||||
CUrl obtainNextUrl(bool randomStart = false);
|
||||
|
||||
//! Round robin with random start point
|
||||
CUrl obtainNextUrlWithout(bool randomStart = false, const CUrlList &exclude = CUrlList()) const;
|
||||
|
||||
//! Next working URL, test if it can be connected
|
||||
CUrl getNextWorkingUrl(const CUrl &failedUrl = CUrl(), bool random = true);
|
||||
CUrl obtainNextWorkingUrl( bool random = false, const CUrl &failedUrl = CUrl());
|
||||
|
||||
//! Reset failed URL, allows to set an optional new number of max.trials
|
||||
void reset(int maxTrials = -1);
|
||||
|
||||
private:
|
||||
int m_maxTrials = 2; //!< number of max trials
|
||||
int m_currentIndexDistributedLoad = -1; //!< index for random access
|
||||
int m_maxTrials = 2; //!< number of max trials
|
||||
CUrlList m_failedUrls;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user