Loading Experiments.h +1 −0 Original line number Original line Diff line number Diff line Loading @@ -68,6 +68,7 @@ class Experiments { "doh_idle_timeout_ms", "doh_idle_timeout_ms", "doh_session_resumption", "doh_session_resumption", "mdns_resolution", "mdns_resolution", "max_queries_global", }; }; // This value is used in updateInternal as the default value if any flags can't be found. // This value is used in updateInternal as the default value if any flags can't be found. static constexpr int kFlagIntDefault = INT_MIN; static constexpr int kFlagIntDefault = INT_MIN; Loading OperationLimiter.h +12 −7 Original line number Original line Diff line number Diff line Loading @@ -23,6 +23,8 @@ #include <android-base/logging.h> #include <android-base/logging.h> #include <android-base/thread_annotations.h> #include <android-base/thread_annotations.h> #include "Experiments.h" namespace android { namespace android { namespace netdutils { namespace netdutils { Loading @@ -43,8 +45,7 @@ namespace netdutils { template <typename KeyType> template <typename KeyType> class OperationLimiter { class OperationLimiter { public: public: OperationLimiter(int limitPerKey, int globalLimit = INT_MAX) OperationLimiter(int limitPerKey) : mLimitPerKey(limitPerKey) {} : mLimitPerKey(limitPerKey), mGlobalLimit(globalLimit) {} ~OperationLimiter() { ~OperationLimiter() { DCHECK(mCounters.empty()) << "Destroying OperationLimiter with active operations"; DCHECK(mCounters.empty()) << "Destroying OperationLimiter with active operations"; Loading @@ -57,15 +58,22 @@ class OperationLimiter { // finish(key). // finish(key). bool start(KeyType key) EXCLUDES(mMutex) { bool start(KeyType key) EXCLUDES(mMutex) { std::lock_guard lock(mMutex); std::lock_guard lock(mMutex); int globalLimit = if (mGlobalCounter >= mGlobalLimit) { 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! // Oh, no! LOG(ERROR) << "Query from " << key << " denied due to global limit: " << globalLimit; return false; return false; } } auto& cnt = mCounters[key]; // operator[] creates new entries as needed. auto& cnt = mCounters[key]; // operator[] creates new entries as needed. if (cnt >= mLimitPerKey) { if (cnt >= mLimitPerKey) { // Oh, no! // Oh, no! LOG(ERROR) << "Query from " << key << " denied due to limit: " << mLimitPerKey; return false; return false; } } Loading Loading @@ -109,9 +117,6 @@ class OperationLimiter { // Maximum number of outstanding queries from a single key. // Maximum number of outstanding queries from a single key. const int mLimitPerKey; const int mLimitPerKey; // Maximum number of outstanding queries, globally. const int mGlobalLimit; }; }; } // namespace netdutils } // namespace netdutils Loading Loading
Experiments.h +1 −0 Original line number Original line Diff line number Diff line Loading @@ -68,6 +68,7 @@ class Experiments { "doh_idle_timeout_ms", "doh_idle_timeout_ms", "doh_session_resumption", "doh_session_resumption", "mdns_resolution", "mdns_resolution", "max_queries_global", }; }; // This value is used in updateInternal as the default value if any flags can't be found. // This value is used in updateInternal as the default value if any flags can't be found. static constexpr int kFlagIntDefault = INT_MIN; static constexpr int kFlagIntDefault = INT_MIN; Loading
OperationLimiter.h +12 −7 Original line number Original line Diff line number Diff line Loading @@ -23,6 +23,8 @@ #include <android-base/logging.h> #include <android-base/logging.h> #include <android-base/thread_annotations.h> #include <android-base/thread_annotations.h> #include "Experiments.h" namespace android { namespace android { namespace netdutils { namespace netdutils { Loading @@ -43,8 +45,7 @@ namespace netdutils { template <typename KeyType> template <typename KeyType> class OperationLimiter { class OperationLimiter { public: public: OperationLimiter(int limitPerKey, int globalLimit = INT_MAX) OperationLimiter(int limitPerKey) : mLimitPerKey(limitPerKey) {} : mLimitPerKey(limitPerKey), mGlobalLimit(globalLimit) {} ~OperationLimiter() { ~OperationLimiter() { DCHECK(mCounters.empty()) << "Destroying OperationLimiter with active operations"; DCHECK(mCounters.empty()) << "Destroying OperationLimiter with active operations"; Loading @@ -57,15 +58,22 @@ class OperationLimiter { // finish(key). // finish(key). bool start(KeyType key) EXCLUDES(mMutex) { bool start(KeyType key) EXCLUDES(mMutex) { std::lock_guard lock(mMutex); std::lock_guard lock(mMutex); int globalLimit = if (mGlobalCounter >= mGlobalLimit) { 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! // Oh, no! LOG(ERROR) << "Query from " << key << " denied due to global limit: " << globalLimit; return false; return false; } } auto& cnt = mCounters[key]; // operator[] creates new entries as needed. auto& cnt = mCounters[key]; // operator[] creates new entries as needed. if (cnt >= mLimitPerKey) { if (cnt >= mLimitPerKey) { // Oh, no! // Oh, no! LOG(ERROR) << "Query from " << key << " denied due to limit: " << mLimitPerKey; return false; return false; } } Loading Loading @@ -109,9 +117,6 @@ class OperationLimiter { // Maximum number of outstanding queries from a single key. // Maximum number of outstanding queries from a single key. const int mLimitPerKey; const int mLimitPerKey; // Maximum number of outstanding queries, globally. const int mGlobalLimit; }; }; } // namespace netdutils } // namespace netdutils Loading