Tweaked peer list logic

This commit is contained in:
SChernykh 2021-09-02 19:21:38 +02:00
parent e736a73117
commit 70ff4ba859
2 changed files with 28 additions and 26 deletions

View file

@ -214,8 +214,10 @@ void P2PServer::update_peer_list()
continue; continue;
} }
if (cur_time >= client->m_lastPeerListRequest + 60) { if (cur_time >= client->m_nextPeerListRequest) {
client->m_lastPeerListRequest = cur_time; // Send peer list requests at random intervals (60-120 seconds)
client->m_nextPeerListRequest = cur_time + 60 + (get_random64() % 61);
send(client, send(client,
[](void* buf) [](void* buf)
{ {
@ -272,12 +274,13 @@ void P2PServer::save_peer_list()
return; return;
} }
size_t num_peers; std::vector<Peer> peer_list;
{ {
MutexLock lock(m_peerListLock); MutexLock lock(m_peerListLock);
peer_list = m_peerList;
}
num_peers = m_peerList.size(); for (const Peer& p : peer_list) {
for (const Peer& p : m_peerList) {
const char* addr_str; const char* addr_str;
char addr_str_buf[64]; char addr_str_buf[64];
@ -298,11 +301,10 @@ void P2PServer::save_peer_list()
} }
} }
} }
}
f.close(); f.close();
LOGINFO(5, "peer list saved (" << num_peers << " peers)"); LOGINFO(5, "peer list saved (" << peer_list.size() << " peers)");
m_peerListLastSaved = time(nullptr); m_peerListLastSaved = time(nullptr);
} }
@ -579,9 +581,9 @@ void P2PServer::on_timer()
{ {
flush_cache(); flush_cache();
download_missing_blocks(); download_missing_blocks();
update_peer_connections();
update_peer_list(); update_peer_list();
save_peer_list_async(); save_peer_list_async();
update_peer_connections();
} }
void P2PServer::flush_cache() void P2PServer::flush_cache()
@ -689,7 +691,7 @@ P2PServer::P2PClient::P2PClient()
, m_handshakeComplete(false) , m_handshakeComplete(false)
, m_handshakeInvalid(false) , m_handshakeInvalid(false)
, m_listenPort(-1) , m_listenPort(-1)
, m_lastPeerListRequest(0) , m_nextPeerListRequest(0)
, m_lastAlive(0) , m_lastAlive(0)
{ {
uv_rwlock_init_checked(&m_broadcastedHashesLock); uv_rwlock_init_checked(&m_broadcastedHashesLock);
@ -711,7 +713,7 @@ void P2PServer::P2PClient::reset()
m_handshakeComplete = false; m_handshakeComplete = false;
m_handshakeInvalid = false; m_handshakeInvalid = false;
m_listenPort = -1; m_listenPort = -1;
m_lastPeerListRequest = 0; m_nextPeerListRequest = 0;
m_lastAlive = 0; m_lastAlive = 0;
WriteLock lock(m_broadcastedHashesLock); WriteLock lock(m_broadcastedHashesLock);

View file

@ -105,7 +105,7 @@ public:
bool m_handshakeComplete; bool m_handshakeComplete;
bool m_handshakeInvalid; bool m_handshakeInvalid;
int m_listenPort; int m_listenPort;
time_t m_lastPeerListRequest; time_t m_nextPeerListRequest;
time_t m_lastAlive; time_t m_lastAlive;
uv_rwlock_t m_broadcastedHashesLock; uv_rwlock_t m_broadcastedHashesLock;