mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-19 01:09:58 +00:00
use memory pool for tunnel messages
This commit is contained in:
parent
4ce7e192d6
commit
ae0cf2e831
@ -38,20 +38,14 @@ namespace i2p
|
|||||||
|
|
||||||
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage (bool endpoint)
|
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage (bool endpoint)
|
||||||
{
|
{
|
||||||
I2NPMessage * msg = nullptr;
|
|
||||||
if (endpoint)
|
if (endpoint)
|
||||||
{
|
return i2p::tunnel::tunnels.NewI2NPTunnelMessage ();
|
||||||
// should fit two tunnel message + tunnel gateway header, enough for one garlic encrypted streaming packet
|
|
||||||
msg = new I2NPMessageBuffer<2*i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE + 28>(); // reserved for alignment and NTCP 16 + 6 + 6
|
|
||||||
msg->Align (6);
|
|
||||||
msg->offset += TUNNEL_GATEWAY_HEADER_SIZE; // reserve room for TunnelGateway header
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
msg = new I2NPMessageBuffer<i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + 34>(); // reserved for alignment and NTCP 16 + 6 + 12
|
auto msg = new I2NPMessageBuffer<i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + 34>(); // reserved for alignment and NTCP 16 + 6 + 12
|
||||||
msg->Align (12);
|
msg->Align (12);
|
||||||
|
return std::shared_ptr<I2NPMessage>(msg);
|
||||||
}
|
}
|
||||||
return std::shared_ptr<I2NPMessage>(msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<I2NPMessage> NewI2NPMessage (size_t len)
|
std::shared_ptr<I2NPMessage> NewI2NPMessage (size_t len)
|
||||||
|
@ -488,7 +488,7 @@ namespace tunnel
|
|||||||
i2p::util::SetThreadName("Tunnels");
|
i2p::util::SetThreadName("Tunnels");
|
||||||
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, lastPoolsTs = 0;
|
uint64_t lastTs = 0, lastPoolsTs = 0, lastMemoryPoolTs = 0;
|
||||||
while (m_IsRunning)
|
while (m_IsRunning)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -564,6 +564,11 @@ namespace tunnel
|
|||||||
ManageTunnelPools (ts);
|
ManageTunnelPools (ts);
|
||||||
lastPoolsTs = ts;
|
lastPoolsTs = ts;
|
||||||
}
|
}
|
||||||
|
if (ts - lastMemoryPoolTs >= 120) // manage memory pool every 2 minutes
|
||||||
|
{
|
||||||
|
m_I2NPTunnelMessagesMemoryPool.CleanUpMt ();
|
||||||
|
lastMemoryPoolTs = ts;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (std::exception& ex)
|
catch (std::exception& ex)
|
||||||
@ -935,6 +940,15 @@ namespace tunnel
|
|||||||
return outboundTunnel;
|
return outboundTunnel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<I2NPMessage> Tunnels::NewI2NPTunnelMessage ()
|
||||||
|
{
|
||||||
|
// should fit two tunnel message + tunnel gateway header, enough for one garlic encrypted streaming packet
|
||||||
|
auto msg = m_I2NPTunnelMessagesMemoryPool.AcquireSharedMt ();
|
||||||
|
msg->Align (6);
|
||||||
|
msg->offset += TUNNEL_GATEWAY_HEADER_SIZE; // reserve room for TunnelGateway header
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
int Tunnels::GetTransitTunnelsExpirationTimeout ()
|
int Tunnels::GetTransitTunnelsExpirationTimeout ()
|
||||||
{
|
{
|
||||||
int timeout = 0;
|
int timeout = 0;
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "util.h"
|
||||||
#include "Queue.h"
|
#include "Queue.h"
|
||||||
#include "Crypto.h"
|
#include "Crypto.h"
|
||||||
#include "TunnelConfig.h"
|
#include "TunnelConfig.h"
|
||||||
@ -40,6 +41,8 @@ namespace tunnel
|
|||||||
const int MAX_NUM_RECORDS = 8;
|
const int MAX_NUM_RECORDS = 8;
|
||||||
const int HIGH_LATENCY_PER_HOP = 250; // in milliseconds
|
const int HIGH_LATENCY_PER_HOP = 250; // in milliseconds
|
||||||
|
|
||||||
|
const size_t I2NP_TUNNEL_ENPOINT_MESSAGE_SIZE = 2*TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE + 28; // reserved for alignment and NTCP 16 + 6 + 6
|
||||||
|
|
||||||
enum TunnelState
|
enum TunnelState
|
||||||
{
|
{
|
||||||
eTunnelStatePending,
|
eTunnelStatePending,
|
||||||
@ -219,6 +222,8 @@ namespace tunnel
|
|||||||
void DeleteTunnelPool (std::shared_ptr<TunnelPool> pool);
|
void DeleteTunnelPool (std::shared_ptr<TunnelPool> pool);
|
||||||
void StopTunnelPool (std::shared_ptr<TunnelPool> pool);
|
void StopTunnelPool (std::shared_ptr<TunnelPool> pool);
|
||||||
|
|
||||||
|
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
template<class TTunnel>
|
template<class TTunnel>
|
||||||
@ -257,6 +262,7 @@ namespace tunnel
|
|||||||
std::list<std::shared_ptr<TunnelPool>> m_Pools;
|
std::list<std::shared_ptr<TunnelPool>> m_Pools;
|
||||||
std::shared_ptr<TunnelPool> m_ExploratoryPool;
|
std::shared_ptr<TunnelPool> m_ExploratoryPool;
|
||||||
i2p::util::Queue<std::shared_ptr<I2NPMessage> > m_Queue;
|
i2p::util::Queue<std::shared_ptr<I2NPMessage> > m_Queue;
|
||||||
|
i2p::util::MemoryPoolMt<I2NPMessageBuffer<I2NP_TUNNEL_ENPOINT_MESSAGE_SIZE> > m_I2NPTunnelMessagesMemoryPool;
|
||||||
|
|
||||||
// some stats
|
// some stats
|
||||||
int m_NumSuccesiveTunnelCreations, m_NumFailedTunnelCreations;
|
int m_NumSuccesiveTunnelCreations, m_NumFailedTunnelCreations;
|
||||||
|
@ -56,12 +56,8 @@ namespace util
|
|||||||
|
|
||||||
void CleanUp ()
|
void CleanUp ()
|
||||||
{
|
{
|
||||||
while (m_Head)
|
CleanUp (m_Head);
|
||||||
{
|
m_Head = nullptr;
|
||||||
auto tmp = m_Head;
|
|
||||||
m_Head = static_cast<T*>(*(void * *)m_Head); // next
|
|
||||||
::operator delete ((void *)tmp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TArgs>
|
template<typename... TArgs>
|
||||||
@ -98,6 +94,18 @@ namespace util
|
|||||||
std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
|
std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
void CleanUp (T * head)
|
||||||
|
{
|
||||||
|
while (head)
|
||||||
|
{
|
||||||
|
auto tmp = head;
|
||||||
|
head = static_cast<T*>(*(void * *)head); // next
|
||||||
|
::operator delete ((void *)tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
T * m_Head;
|
T * m_Head;
|
||||||
@ -131,6 +139,24 @@ namespace util
|
|||||||
this->Release (it);
|
this->Release (it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... TArgs>
|
||||||
|
std::shared_ptr<T> AcquireSharedMt (TArgs&&... args)
|
||||||
|
{
|
||||||
|
return std::shared_ptr<T>(AcquireMt (std::forward<TArgs>(args)...),
|
||||||
|
std::bind<void (MemoryPoolMt<T>::*)(T *)> (&MemoryPoolMt<T>::ReleaseMt, this, std::placeholders::_1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CleanUpMt ()
|
||||||
|
{
|
||||||
|
T * head;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(m_Mutex);
|
||||||
|
head = this->m_Head;
|
||||||
|
this->m_Head = nullptr;
|
||||||
|
}
|
||||||
|
if (head) this->CleanUp (head);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::mutex m_Mutex;
|
std::mutex m_Mutex;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user