| 633 | } |
| 634 | |
| 635 | void NetDb::SaveUpdated () |
| 636 | { |
| 637 | if (m_PersistingRouters.valid ()) |
| 638 | { |
| 639 | if (m_PersistingRouters.wait_for(std::chrono::seconds(0)) == std::future_status::ready) |
| 640 | m_PersistingRouters.get (); |
| 641 | else |
| 642 | { |
| 643 | LogPrint (eLogWarning, "NetDb: Can't save updated routers. Routers are being saved to disk"); |
| 644 | return; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | int updatedCount = 0, deletedCount = 0, deletedFloodfillsCount = 0; |
| 649 | auto total = m_RouterInfos.size (); |
| 650 | auto totalFloodfills = m_Floodfills.GetSize (); |
| 651 | uint64_t expirationTimeout = NETDB_MAX_EXPIRATION_TIMEOUT*1000LL; |
| 652 | uint64_t ts = i2p::util::GetMillisecondsSinceEpoch(); |
| 653 | auto uptime = i2p::context.GetUptime (); |
| 654 | double minTunnelCreationSuccessRate; |
| 655 | i2p::config::GetOption("limits.zombies", minTunnelCreationSuccessRate); |
| 656 | bool isLowRate = i2p::tunnel::tunnels.GetPreciseTunnelCreationSuccessRate () < minTunnelCreationSuccessRate; |
| 657 | // routers don't expire if less than 90 or uptime is less than 1 hour |
| 658 | bool checkForExpiration = total > NETDB_MIN_ROUTERS && uptime > NETDB_CHECK_FOR_EXPIRATION_UPTIME; // 10 minutes |
| 659 | if (checkForExpiration && uptime > i2p::transport::SSU2_TO_INTRODUCER_SESSION_DURATION) // 1 hour |
| 660 | expirationTimeout = i2p::context.IsFloodfill () ? NETDB_FLOODFILL_EXPIRATION_TIMEOUT*1000LL : |
| 661 | NETDB_MIN_EXPIRATION_TIMEOUT*1000LL + (NETDB_MAX_EXPIRATION_TIMEOUT - NETDB_MIN_EXPIRATION_TIMEOUT)*1000LL*NETDB_MIN_ROUTERS/total; |
| 662 | bool isOffline = checkForExpiration && i2p::transport::transports.GetNumPeers () < NETDB_MIN_TRANSPORTS; // enough routers and uptime, but no transports |
| 663 | |
| 664 | std::list<std::pair<std::string, std::shared_ptr<RouterInfo::Buffer> > > saveToDisk; |
| 665 | std::list<std::string> removeFromDisk; |
| 666 | |
| 667 | auto own = i2p::context.GetSharedRouterInfo (); |
| 668 | for (auto [ident, r]: m_RouterInfos) |
| 669 | { |
| 670 | if (!r || r == own) continue; // skip own |
| 671 | if (r->IsBufferScheduledToDelete ()) // from previous SaveUpdated, we assume m_PersistingRouters complete |
| 672 | { |
| 673 | std::lock_guard<std::mutex> l(m_RouterInfosMutex); // possible collision between DeleteBuffer and Update |
| 674 | r->DeleteBuffer (); |
| 675 | } |
| 676 | if (r->IsUpdated ()) |
| 677 | { |
| 678 | if (r->GetBuffer () && !r->IsUnreachable ()) |
| 679 | { |
| 680 | // we have something to save |
| 681 | std::shared_ptr<RouterInfo::Buffer> buffer; |
| 682 | { |
| 683 | std::lock_guard<std::mutex> l(m_RouterInfosMutex); // possible collision between DeleteBuffer and Update |
| 684 | buffer = r->CopyBuffer (); |
| 685 | } |
| 686 | if (!i2p::transport::transports.IsConnected (ident)) |
| 687 | r->ScheduleBufferToDelete (); |
| 688 | if (buffer) |
| 689 | saveToDisk.emplace_back(ident.ToBase64 (), buffer); |
| 690 | } |
| 691 | r->SetUpdated (false); |
| 692 | updatedCount++; |
nothing calls this directly
no test coverage detected