Browse Source

don't build tunnels for all pools at the time

pull/1553/merge
orignal 4 years ago
parent
commit
44ca315c75
  1. 20
      libi2pd/Tunnel.cpp
  2. 2
      libi2pd/Tunnel.h
  3. 15
      libi2pd/TunnelPool.cpp
  4. 4
      libi2pd/TunnelPool.h

20
libi2pd/Tunnel.cpp

@ -474,7 +474,7 @@ namespace tunnel
{ {
std::this_thread::sleep_for (std::chrono::seconds(1)); // wait for other parts are ready std::this_thread::sleep_for (std::chrono::seconds(1)); // wait for other parts are ready
uint64_t lastTs = 0; uint64_t lastTs = 0, lastPoolsTs = 0;
while (m_IsRunning) while (m_IsRunning)
{ {
try try
@ -535,12 +535,20 @@ namespace tunnel
while (msg); while (msg);
} }
if (i2p::transport::transports.IsOnline())
{
uint64_t ts = i2p::util::GetSecondsSinceEpoch (); uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
if (ts - lastTs >= 15 && i2p::transport::transports.IsOnline()) // manage tunnels every 15 seconds if (ts - lastTs >= 15) // manage tunnels every 15 seconds
{ {
ManageTunnels (); ManageTunnels ();
lastTs = ts; lastTs = ts;
} }
if (ts - lastPoolsTs >= 5) // manage pools every 5 seconds
{
ManageTunnelPools (ts);
lastPoolsTs = ts;
}
}
} }
catch (std::exception& ex) catch (std::exception& ex)
{ {
@ -582,7 +590,6 @@ namespace tunnel
ManageInboundTunnels (); ManageInboundTunnels ();
ManageOutboundTunnels (); ManageOutboundTunnels ();
ManageTransitTunnels (); ManageTransitTunnels ();
ManageTunnelPools ();
} }
void Tunnels::ManagePendingTunnels () void Tunnels::ManagePendingTunnels ()
@ -794,16 +801,13 @@ namespace tunnel
} }
} }
void Tunnels::ManageTunnelPools () void Tunnels::ManageTunnelPools (uint64_t ts)
{ {
std::unique_lock<std::mutex> l(m_PoolsMutex); std::unique_lock<std::mutex> l(m_PoolsMutex);
for (auto& pool : m_Pools) for (auto& pool : m_Pools)
{ {
if (pool && pool->IsActive ()) if (pool && pool->IsActive ())
{ pool->ManageTunnels (ts);
pool->CreateTunnels ();
pool->TestTunnels ();
}
} }
} }

2
libi2pd/Tunnel.h

@ -230,7 +230,7 @@ namespace tunnel
void ManagePendingTunnels (); void ManagePendingTunnels ();
template<class PendingTunnels> template<class PendingTunnels>
void ManagePendingTunnels (PendingTunnels& pendingTunnels); void ManagePendingTunnels (PendingTunnels& pendingTunnels);
void ManageTunnelPools (); void ManageTunnelPools (uint64_t ts);
std::shared_ptr<ZeroHopsInboundTunnel> CreateZeroHopsInboundTunnel (); std::shared_ptr<ZeroHopsInboundTunnel> CreateZeroHopsInboundTunnel ();
std::shared_ptr<ZeroHopsOutboundTunnel> CreateZeroHopsOutboundTunnel (); std::shared_ptr<ZeroHopsOutboundTunnel> CreateZeroHopsOutboundTunnel ();

15
libi2pd/TunnelPool.cpp

@ -26,9 +26,10 @@ namespace tunnel
{ {
TunnelPool::TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels): TunnelPool::TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels):
m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops), m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops),
m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels), m_IsActive (true), m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels),
m_CustomPeerSelector(nullptr) m_IsActive (true), m_CustomPeerSelector(nullptr)
{ {
m_NextManageTime = i2p::util::GetSecondsSinceEpoch () + rand () % TUNNEL_POOL_MANAGE_INTERVAL;
} }
TunnelPool::~TunnelPool () TunnelPool::~TunnelPool ()
@ -321,6 +322,16 @@ namespace tunnel
} }
} }
void TunnelPool::ManageTunnels (uint64_t ts)
{
if (ts > m_NextManageTime)
{
CreateTunnels ();
TestTunnels ();
m_NextManageTime = ts + TUNNEL_POOL_MANAGE_INTERVAL + (rand () % TUNNEL_POOL_MANAGE_INTERVAL)/2;
}
}
void TunnelPool::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg) void TunnelPool::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
{ {
if (m_LocalDestination) if (m_LocalDestination)

4
libi2pd/TunnelPool.h

@ -27,6 +27,8 @@ namespace i2p
{ {
namespace tunnel namespace tunnel
{ {
const int TUNNEL_POOL_MANAGE_INTERVAL = 10; // in seconds
class Tunnel; class Tunnel;
class InboundTunnel; class InboundTunnel;
class OutboundTunnel; class OutboundTunnel;
@ -69,6 +71,7 @@ namespace tunnel
std::shared_ptr<InboundTunnel> GetNextInboundTunnel (std::shared_ptr<InboundTunnel> excluded = nullptr) const; std::shared_ptr<InboundTunnel> GetNextInboundTunnel (std::shared_ptr<InboundTunnel> excluded = nullptr) const;
std::shared_ptr<OutboundTunnel> GetNewOutboundTunnel (std::shared_ptr<OutboundTunnel> old) const; std::shared_ptr<OutboundTunnel> GetNewOutboundTunnel (std::shared_ptr<OutboundTunnel> old) const;
void TestTunnels (); void TestTunnels ();
void ManageTunnels (uint64_t ts);
void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg); void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
void ProcessDeliveryStatus (std::shared_ptr<I2NPMessage> msg); void ProcessDeliveryStatus (std::shared_ptr<I2NPMessage> msg);
@ -123,6 +126,7 @@ namespace tunnel
mutable std::mutex m_TestsMutex; mutable std::mutex m_TestsMutex;
std::map<uint32_t, std::pair<std::shared_ptr<OutboundTunnel>, std::shared_ptr<InboundTunnel> > > m_Tests; std::map<uint32_t, std::pair<std::shared_ptr<OutboundTunnel>, std::shared_ptr<InboundTunnel> > > m_Tests;
bool m_IsActive; bool m_IsActive;
uint64_t m_NextManageTime; // in seconds
std::mutex m_CustomPeerSelectorMutex; std::mutex m_CustomPeerSelectorMutex;
ITunnelPeerSelector * m_CustomPeerSelector; ITunnelPeerSelector * m_CustomPeerSelector;

Loading…
Cancel
Save