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

Commit 06864ab6 authored by Ken Chen's avatar Ken Chen
Browse files

Add experiment flag for DNS query global limiter

Make the maximum number of global queries adjustable via flag. The
default value is not changed in this commit.

Bug: 172484780
Bug: 169105756
Test: TH
Change-Id: I44a34a9a1853d8c25b4b48c7073937e157b001c0
parent 0bbc4ee2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ class Experiments {
            "doh_idle_timeout_ms",
            "doh_session_resumption",
            "mdns_resolution",
            "max_queries_global",
    };
    // This value is used in updateInternal as the default value if any flags can't be found.
    static constexpr int kFlagIntDefault = INT_MIN;
+12 −7
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@
#include <android-base/logging.h>
#include <android-base/thread_annotations.h>

#include "Experiments.h"

namespace android {
namespace netdutils {

@@ -43,8 +45,7 @@ namespace netdutils {
template <typename KeyType>
class OperationLimiter {
  public:
    OperationLimiter(int limitPerKey, int globalLimit = INT_MAX)
        : mLimitPerKey(limitPerKey), mGlobalLimit(globalLimit) {}
    OperationLimiter(int limitPerKey) : mLimitPerKey(limitPerKey) {}

    ~OperationLimiter() {
        DCHECK(mCounters.empty()) << "Destroying OperationLimiter with active operations";
@@ -57,15 +58,22 @@ class OperationLimiter {
    // finish(key).
    bool start(KeyType key) EXCLUDES(mMutex) {
        std::lock_guard lock(mMutex);

        if (mGlobalCounter >= mGlobalLimit) {
        int globalLimit =
                android::net::Experiments::getInstance()->getFlag("max_queries_global", INT_MAX);
        if (globalLimit < mLimitPerKey) {
            LOG(ERROR) << "Misconfiguration on max_queries_global " << globalLimit;
            globalLimit = INT_MAX;
        }
        if (mGlobalCounter >= globalLimit) {
            // Oh, no!
            LOG(ERROR) << "Query from " << key << " denied due to global limit: " << globalLimit;
            return false;
        }

        auto& cnt = mCounters[key];  // operator[] creates new entries as needed.
        if (cnt >= mLimitPerKey) {
            // Oh, no!
            LOG(ERROR) << "Query from " << key << " denied due to limit: " << mLimitPerKey;
            return false;
        }

@@ -109,9 +117,6 @@ class OperationLimiter {

    // Maximum number of outstanding queries from a single key.
    const int mLimitPerKey;

    // Maximum number of outstanding queries, globally.
    const int mGlobalLimit;
};

}  // namespace netdutils