Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8366edca authored by Mike Yu's avatar Mike Yu
Browse files

Unify forceCleanupLocked() and cleanup()

Use std::erase_if instead of for loop to remove the expired
Transports.

Bug: Bug: 233345976
Test: atest
Change-Id: Ic7ca295239f9adb8c6b50f38c5dbb11157ec7e87
parent 91d1c929
Loading
Loading
Loading
Loading
+15 −22
Original line number Diff line number Diff line
@@ -114,9 +114,9 @@ DnsTlsTransport::Response DnsTlsDispatcher::query(const std::list<DnsTlsServer>&
    if (servers.empty()) {
        LOG(WARNING) << "No usable DnsTlsServers";

        // Call cleanup so the expired Transports can be removed as expected.
        // Call maybeCleanup so the expired Transports can be removed as expected.
        std::lock_guard guard(sLock);
        cleanup(std::chrono::steady_clock::now());
        maybeCleanup(std::chrono::steady_clock::now());
    }

    DnsTlsTransport::Response code = DnsTlsTransport::Response::internal_error;
@@ -236,14 +236,14 @@ DnsTlsTransport::Response DnsTlsDispatcher::query(const DnsTlsServer& server, un
                         << (result.ok() ? "succeeded" : "failed: " + result.error().message());
        }

        cleanup(now);
        maybeCleanup(now);
    }
    return code;
}

void DnsTlsDispatcher::forceCleanup(unsigned netId) {
    std::lock_guard guard(sLock);
    forceCleanupLocked(netId);
    cleanup(std::chrono::steady_clock::now(), netId);
}

DnsTlsTransport::Result DnsTlsDispatcher::queryInternal(Transport& xport,
@@ -275,33 +275,26 @@ DnsTlsTransport::Result DnsTlsDispatcher::queryInternal(Transport& xport,

// This timeout effectively controls how long to keep SSL session tickets.
static constexpr std::chrono::minutes IDLE_TIMEOUT(5);
void DnsTlsDispatcher::cleanup(std::chrono::time_point<std::chrono::steady_clock> now) {
void DnsTlsDispatcher::maybeCleanup(std::chrono::time_point<std::chrono::steady_clock> now) {
    // To avoid scanning mStore after every query, return early if a cleanup has been
    // performed recently.
    if (now - mLastCleanup < IDLE_TIMEOUT) {
        return;
    }
    for (auto it = mStore.begin(); it != mStore.end();) {
        auto& s = it->second;
        if (s->useCount == 0 && now - s->lastUsed > IDLE_TIMEOUT) {
            it = mStore.erase(it);
        } else {
            ++it;
        }
    }
    cleanup(now, std::nullopt);
    mLastCleanup = now;
}

// TODO: unify forceCleanupLocked() and cleanup().
void DnsTlsDispatcher::forceCleanupLocked(unsigned netId) {
    for (auto it = mStore.begin(); it != mStore.end();) {
        auto& s = it->second;
        if (s->useCount == 0 && s->mNetId == netId) {
            it = mStore.erase(it);
        } else {
            ++it;
        }
void DnsTlsDispatcher::cleanup(std::chrono::time_point<std::chrono::steady_clock> now,
                               std::optional<unsigned> netId) {
    std::erase_if(mStore, [&](const auto& item) REQUIRES(sLock) {
        auto const& [_, xport] = item;
        if (xport->useCount == 0) {
            if (netId.has_value() && xport->mNetId == netId.value()) return true;
            if (now - xport->lastUsed > IDLE_TIMEOUT) return true;
        }
        return false;
    });
}

DnsTlsDispatcher::Transport* DnsTlsDispatcher::addTransport(const DnsTlsServer& server,
+4 −4
Original line number Diff line number Diff line
@@ -160,12 +160,12 @@ class DnsTlsDispatcher : public PrivateDnsValidationObserver {
    DnsTlsTransport::Result queryInternal(Transport& transport, const netdutils::Slice query)
            EXCLUDES(sLock);

    void maybeCleanup(std::chrono::time_point<std::chrono::steady_clock> now) REQUIRES(sLock);

    // Drop any cache entries whose useCount is zero and which have not been used recently.
    // This function performs a linear scan of mStore.
    void cleanup(std::chrono::time_point<std::chrono::steady_clock> now) REQUIRES(sLock);

    // Force dropping any Transports whose useCount is zero.
    void forceCleanupLocked(unsigned netId) REQUIRES(sLock);
    void cleanup(std::chrono::time_point<std::chrono::steady_clock> now,
                 std::optional<unsigned> netId) REQUIRES(sLock);

    // Return a sorted list of usable DnsTlsServers in preference order.
    std::list<DnsTlsServer> getOrderedAndUsableServerList(const std::list<DnsTlsServer>& tlsServers,