Browse Source

cache complete request for a while and not send the same request again

pull/1941/merge
orignal 8 months ago
parent
commit
396aa6944d
  1. 4
      libi2pd/NetDb.cpp
  2. 65
      libi2pd/NetDbRequests.cpp
  3. 1
      libi2pd/NetDbRequests.h

4
libi2pd/NetDb.cpp

@ -780,7 +780,7 @@ namespace data
auto dest = m_Requests.CreateRequest (destination, false, direct, requestComplete); // non-exploratory auto dest = m_Requests.CreateRequest (destination, false, direct, requestComplete); // non-exploratory
if (!dest) if (!dest)
{ {
LogPrint (eLogWarning, "NetDb: Destination ", destination.ToBase64(), " is requested already"); LogPrint (eLogWarning, "NetDb: Destination ", destination.ToBase64(), " is requested already or cached");
return; return;
} }
@ -1000,7 +1000,7 @@ namespace data
LogPrint (eLogDebug, "NetDb: DatabaseSearchReply for ", key, " num=", num); LogPrint (eLogDebug, "NetDb: DatabaseSearchReply for ", key, " num=", num);
IdentHash ident (buf); IdentHash ident (buf);
auto dest = m_Requests.FindRequest (ident); auto dest = m_Requests.FindRequest (ident);
if (dest) if (dest && dest->IsActive ())
{ {
if (!dest->IsExploratory () && (num > 0 || dest->GetNumExcludedPeers () < 3)) // before 3-rd attempt might be just bad luck if (!dest->IsExploratory () && (num > 0 || dest->GetNumExcludedPeers () < 3)) // before 3-rd attempt might be just bad luck
// try to send next requests // try to send next requests

65
libi2pd/NetDbRequests.cpp

@ -81,21 +81,27 @@ namespace data
void RequestedDestination::Success (std::shared_ptr<RouterInfo> r) void RequestedDestination::Success (std::shared_ptr<RouterInfo> r)
{ {
m_IsActive = false; if (m_IsActive)
if (m_RequestComplete)
{ {
m_RequestComplete (r); m_IsActive = false;
m_RequestComplete = nullptr; if (m_RequestComplete)
{
m_RequestComplete (r);
m_RequestComplete = nullptr;
}
} }
} }
void RequestedDestination::Fail () void RequestedDestination::Fail ()
{ {
m_IsActive = false; if (m_IsActive)
if (m_RequestComplete)
{ {
m_RequestComplete (nullptr); m_IsActive = false;
m_RequestComplete = nullptr; if (m_RequestComplete)
{
m_RequestComplete (nullptr);
m_RequestComplete = nullptr;
}
} }
} }
@ -152,7 +158,9 @@ namespace data
if (it != m_RequestedDestinations.end ()) if (it != m_RequestedDestinations.end ())
{ {
request = it->second; request = it->second;
m_RequestedDestinations.erase (it); if (request->IsExploratory ())
m_RequestedDestinations.erase (it);
// otherwise cache for a while
} }
} }
if (request) if (request)
@ -180,24 +188,37 @@ namespace data
for (auto it = m_RequestedDestinations.begin (); it != m_RequestedDestinations.end ();) for (auto it = m_RequestedDestinations.begin (); it != m_RequestedDestinations.end ();)
{ {
auto& dest = it->second; auto& dest = it->second;
bool done = false; if (dest->IsActive () || ts < dest->GetCreationTime () + REQUEST_CACHE_TIME)
if (!dest->IsExploratory ())
{ {
if (ts < dest->GetCreationTime () + MAX_REQUEST_TIME) // request becomes worthless if (!dest->IsExploratory ())
{
// regular request
bool done = false;
if (ts < dest->GetCreationTime () + MAX_REQUEST_TIME)
{
if (ts > dest->GetLastRequestTime () + MIN_REQUEST_TIME) // try next floodfill if no response after min interval
done = !SendNextRequest (dest);
}
else // request is expired
done = true;
if (done)
dest->Fail ();
it++;
}
else
{ {
if (ts > dest->GetLastRequestTime () + MIN_REQUEST_TIME) // try next floodfill if no response after min interval // exploratory
done = !SendNextRequest (dest); if (ts >= dest->GetCreationTime () + MAX_EXPLORATORY_REQUEST_TIME)
{
dest->Fail ();
it = m_RequestedDestinations.erase (it); // delete expired exploratory request right a way
}
else
it++;
} }
else // delete obsolete request
done = true;
} }
else if (ts >= dest->GetCreationTime () + MAX_EXPLORATORY_REQUEST_TIME)
done = true;
if (done)
it = m_RequestedDestinations.erase (it);
else else
++it; it = m_RequestedDestinations.erase (it);
} }
} }

1
libi2pd/NetDbRequests.h

@ -25,6 +25,7 @@ namespace data
const uint64_t MIN_REQUEST_TIME = 5; // in seconds const uint64_t MIN_REQUEST_TIME = 5; // in seconds
const uint64_t MAX_REQUEST_TIME = MAX_NUM_REQUEST_ATTEMPTS * (MIN_REQUEST_TIME + MANAGE_REQUESTS_INTERVAL); const uint64_t MAX_REQUEST_TIME = MAX_NUM_REQUEST_ATTEMPTS * (MIN_REQUEST_TIME + MANAGE_REQUESTS_INTERVAL);
const uint64_t MAX_EXPLORATORY_REQUEST_TIME = 30; // in seconds const uint64_t MAX_EXPLORATORY_REQUEST_TIME = 30; // in seconds
const uint64_t REQUEST_CACHE_TIME = MAX_REQUEST_TIME + 40; // in seconds
class RequestedDestination class RequestedDestination
{ {

Loading…
Cancel
Save