1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ABILITY_RUNTIME_RATE_LIMITER_H 17 #define OHOS_ABILITY_RUNTIME_RATE_LIMITER_H 18 19 #include <mutex> 20 #include <string> 21 #include <time.h> 22 #include <unordered_map> 23 24 #include "nocopyable.h" 25 26 namespace OHOS { 27 namespace AAFwk { 28 class RateLimiter { 29 public: 30 static RateLimiter &GetInstance(); 31 32 ~RateLimiter() = default; 33 34 bool CheckExtensionLimit(int32_t uid); 35 bool CheckReportLimit(int32_t uid); 36 37 private: 38 RateLimiter() = default; 39 40 bool CheckLimit(int32_t uid); 41 bool CheckSingleLimit(int32_t uid, std::unordered_map<int32_t, std::vector<int64_t>> &callMap, 42 std::mutex &mapLock, int64_t limitInterval, int32_t maxLimit); 43 void CleanCallMap(); 44 void CleanSingleCallMap(std::unordered_map<int32_t, std::vector<int64_t>> &callMap, std::mutex &mapLock, 45 int64_t limitInterval); 46 int64_t CurrentTimeMillis(); 47 48 int64_t lastCleanTimeMillis_ = 0; 49 std::mutex lastCleanTimeMillisLock_; 50 std::unordered_map<int32_t, std::vector<int64_t>> extensionCallMap_; 51 std::mutex extensionCallMapLock_; 52 std::unordered_map<int32_t, std::vector<int64_t>> reportCallMap_; 53 std::mutex reportCallMapLock_; 54 55 DISALLOW_COPY_AND_MOVE(RateLimiter); 56 }; 57 } // namespace AAFwk 58 } // namespace OHOS 59 #endif // OHOS_ABILITY_RUNTIME_RATE_LIMITER_H 60