TCPServer: use steady_clock for bans

This commit is contained in:
SChernykh 2022-02-21 18:41:36 +01:00
parent c56773d4d6
commit f082488e4b
4 changed files with 16 additions and 11 deletions

View file

@ -52,6 +52,7 @@ P2PServer::P2PServer(p2pool* pool)
, m_block(new PoolBlock())
, m_timer{}
, m_timerCounter(0)
, m_timerInterval(2)
, m_peerId(m_rng())
, m_peerListLastSaved(0)
{
@ -86,7 +87,7 @@ P2PServer::P2PServer(p2pool* pool)
}
m_timer.data = this;
err = uv_timer_start(&m_timer, on_timer, 1000, 2000);
err = uv_timer_start(&m_timer, on_timer, 1000, m_timerInterval * 1000);
if (err) {
LOGERR(1, "failed to start timer, error " << uv_err_name(err));
panic();
@ -281,7 +282,6 @@ void P2PServer::update_peer_connections()
void P2PServer::update_peer_list()
{
const time_t cur_time = time(nullptr);
{
MutexLock lock(m_clientsListLock);
@ -290,9 +290,9 @@ void P2PServer::update_peer_list()
continue;
}
if (cur_time >= client->m_nextOutgoingPeerListRequest) {
if (m_timerCounter >= client->m_nextOutgoingPeerListRequest) {
// Send peer list requests at random intervals (60-120 seconds)
client->m_nextOutgoingPeerListRequest = cur_time + 60 + (get_random64() % 61);
client->m_nextOutgoingPeerListRequest = m_timerCounter + (60 + (get_random64() % 61)) / m_timerInterval;
const bool result = send(client,
[](void* buf)

View file

@ -111,7 +111,7 @@ public:
int m_listenPort;
time_t m_prevIncomingPeerListRequest;
time_t m_nextOutgoingPeerListRequest;
uint64_t m_nextOutgoingPeerListRequest;
std::chrono::system_clock::time_point m_lastPeerListRequestTime;
int m_peerListPendingRequests;
int64_t m_pingTime;
@ -176,7 +176,8 @@ private:
PoolBlock* m_block;
uv_timer_t m_timer;
uint32_t m_timerCounter;
uint64_t m_timerCounter;
uint64_t m_timerInterval;
uint64_t m_peerId;

View file

@ -160,7 +160,7 @@ protected:
uint32_t m_numIncomingConnections;
uv_mutex_t m_bansLock;
unordered_map<raw_ip, time_t> m_bans;
unordered_map<raw_ip, std::chrono::steady_clock::time_point> m_bans;
bool is_banned(const raw_ip& ip);

View file

@ -322,11 +322,13 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_connect_failed(bool, const raw
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::is_banned(const raw_ip& ip)
{
const auto cur_time = std::chrono::steady_clock::now();
MutexLock lock(m_bansLock);
auto it = m_bans.find(ip);
if (it != m_bans.end()) {
const bool banned = (time(nullptr) < it->second);
const bool banned = (cur_time < it->second);
if (!banned) {
m_bans.erase(it);
}
@ -435,14 +437,14 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::shutdown_tcp()
using namespace std::chrono;
const system_clock::time_point start_time = system_clock::now();
const auto start_time = steady_clock::now();
int64_t counter = 0;
uv_async_t asy;
constexpr uint32_t timeout_seconds = 30;
while (!m_loopStopped) {
const int64_t elapsed_time = duration_cast<milliseconds>(system_clock::now() - start_time).count();
const int64_t elapsed_time = duration_cast<milliseconds>(steady_clock::now() - start_time).count();
if (elapsed_time >= (counter + 1) * 1000) {
++counter;
@ -493,8 +495,10 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::print_status()
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::ban(const raw_ip& ip, uint64_t seconds)
{
const auto ban_time = std::chrono::steady_clock::now() + std::chrono::seconds(seconds);
MutexLock lock(m_bansLock);
m_bans[ip] = time(nullptr) + seconds;
m_bans[ip] = ban_time;
}
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>