From 177738ee5bae91e57c3b8f6f57701635b24bb974 Mon Sep 17 00:00:00 2001 From: liujiaojiao Date: Mon, 30 Dec 2024 09:53:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=AE=E7=9B=98=E6=8F=90=E9=A2=91=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E9=99=90=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liujiaojiao --- frameworks/base/ressched/ressched_report.cpp | 39 ++++++++++++++++++++ frameworks/base/ressched/ressched_report.h | 9 +++++ 2 files changed, 48 insertions(+) diff --git a/frameworks/base/ressched/ressched_report.cpp b/frameworks/base/ressched/ressched_report.cpp index 3525c38cfef..76099cb528d 100644 --- a/frameworks/base/ressched/ressched_report.cpp +++ b/frameworks/base/ressched/ressched_report.cpp @@ -209,8 +209,47 @@ void ResSchedReport::OnTouchEvent(const TouchEvent& touchEvent) } } +bool ResSchedReport::IsRateLimit(int64_t maxCount, std::chrono::seconds durTime, + int64_t& keyEventCount, std::chrono::steady_clock::time_point& startTime) +{ + if (keyEventCount == -1) { + startTime = std::chrono::steady_clock::now(); + keyEventCount = 0; + } + + if (keyEventCount > maxCount) { + auto curTime = std::chrono::steady_clock::now(); + auto elapsedTime = std::chrono::duration_cast(curTime - startTime).count(); + if (elapsedTime < durTime.count()) { + LOGD("The event report exceeds the throttling limit"); + return true; + } + keyEventCount = 0; + startTime = curTime; + } + keyEventCount++; + return false; +} + +bool ResSchedReport::IsPerSecRateLimit() +{ + int64_t maxCountMS = 10; + auto durTimeMS = std::chrono::seconds(1); + return IsRateLimit(maxCountMS, durTimeMS, keyEventCountMS, startTimeMS); +} + +bool ResSchedReport::IsPerMinRateLimit() +{ + int64_t maxCountS = 180; + auto durTimeS = std::chrono::seconds(60); + return IsRateLimit(maxCountS, durTimeS, keyEventCountS, startTimeS); +} + void ResSchedReport::OnKeyEvent(const KeyEvent& event) { + if (IsPerSecRateLimit() || IsPerMinRateLimit()) { + return; + } switch (event.action) { case KeyAction::DOWN: HandleKeyDown(event); diff --git a/frameworks/base/ressched/ressched_report.h b/frameworks/base/ressched/ressched_report.h index 0cf97a5c22b..7bac8dd9352 100644 --- a/frameworks/base/ressched/ressched_report.h +++ b/frameworks/base/ressched/ressched_report.h @@ -16,6 +16,7 @@ #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_RESSCHED_RESSCHED_REPORT_H #define FOUNDATION_ACE_FRAMEWORKS_BASE_RESSCHED_RESSCHED_REPORT_H +#include #include #include @@ -56,6 +57,10 @@ private: ~ResSchedReport() {} void HandleTouchDown(const TouchEvent& touchEvent); void HandleTouchUp(const TouchEvent& touchEvent); + bool IsRateLimit(int64_t maxCount, std::chrono::seconds durTime, + int64_t& keyEventCount, std::chrono::steady_clock::time_point& startTime); + bool IsPerSecRateLimit(); + bool IsPerMinRateLimit(); void HandleKeyDown(const KeyEvent& event); void HandleKeyUp(const KeyEvent& event); void HandleTouchMove(const TouchEvent& touchEvent); @@ -85,6 +90,10 @@ private: bool isInSlide_ = false; bool isInTouch_ = false; double dpi_ = PipelineBase::GetCurrentDensity(); + std::chrono::steady_clock::time_point startTimeMS = std::chrono::steady_clock::now(); + std::chrono::steady_clock::time_point startTimeS = std::chrono::steady_clock::now(); + int64_t keyEventCountMS = -1; + int64_t keyEventCountS = -1; }; class ACE_EXPORT ResSchedReportScope final { -- Gitee