Improve peer iteration in overlay

This commit is contained in:
Nik Bougalis
2016-10-20 10:44:33 -07:00
parent 810a6b0f30
commit d572de769b
2 changed files with 16 additions and 7 deletions

View File

@@ -235,10 +235,19 @@ public:
for_each (UnaryFunc&& f)
{
std::lock_guard <decltype(mutex_)> lock (mutex_);
for (auto const& e : ids_)
// Iterate over a copy of the peer list because peer
// destruction can invalidate iterators.
std::vector<std::weak_ptr<PeerImp>> wp;
wp.reserve(ids_.size());
for (auto& x : ids_)
wp.push_back(x.second);
for (auto& w : wp)
{
if (auto sp = e.second.lock())
f(std::move(sp));
if (auto p = w.lock())
f(std::move(p));
}
}

View File

@@ -1958,14 +1958,14 @@ getPeerWithTree (OverlayImpl& ov,
std::shared_ptr<PeerImp> ret;
int retScore = 0;
ov.for_each([&](std::shared_ptr<PeerImp> const& p)
ov.for_each([&](std::shared_ptr<PeerImp>&& p)
{
if (p->hasTxSet(rootHash) && p.get() != skip)
{
auto score = p->getScore (true);
if (! ret || (score > retScore))
{
ret = p;
ret = std::move(p);
retScore = score;
}
}
@@ -1986,7 +1986,7 @@ getPeerWithLedger (OverlayImpl& ov,
std::shared_ptr<PeerImp> ret;
int retScore = 0;
ov.for_each([&](std::shared_ptr<PeerImp> const& p)
ov.for_each([&](std::shared_ptr<PeerImp>&& p)
{
if (p->hasLedger(ledgerHash, ledger) &&
p.get() != skip)
@@ -1994,7 +1994,7 @@ getPeerWithLedger (OverlayImpl& ov,
auto score = p->getScore (true);
if (! ret || (score > retScore))
{
ret = p;
ret = std::move(p);
retScore = score;
}
}