• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_FLOW_CONTROL_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_FLOW_CONTROL_H
18 
19 #include <chrono>
20 #include <list>
21 
22 namespace OHOS {
23 namespace Notification {
24 class DistributedFlowControl {
25 public:
26     /**
27      * @brief The constructor.
28      *
29      * @param kvManagerSecondMaxinum The maximum number of calls to DistributedKvDataManager interface per second.
30      * @param kvManagerMinuteMaxinum The maximum number of calls to DistributedKvDataManager interface per minute.
31      * @param kvStoreSecondMaxinum The maximum number of calls to SingleKvStore interface per second.
32      * @param kvStoreMinuteMaxinum The maximum number of calls to SingleKvStore interface per minute.
33      */
34     DistributedFlowControl(size_t kvManagerSecondMaxinum = KVMANAGER_MAXINUM_PER_SECOND,
35         size_t kvManagerMinuteMaxinum = KVMANAGER_MAXINUM_PER_MINUTE,
36         size_t kvStoreSecondMaxinum = KVSTORE_MAXINUM_PER_SECOND,
37         size_t kvStoreMinuteMaxinum = KVSTORE_MAXINUM_PER_MINUTE);
38 
39     /**
40      * @brief Check if DistributedKvDataManager interface flow control can pass.
41      *
42      * @return True on passed, otherwise false.
43      */
44     bool KvManagerFlowControl(void);
45 
46     /**
47      * @brief Check if SingleKvStore interface flow control can pass.
48      *
49      * @return True on passed, otherwise false.
50      */
51     bool KvStoreFlowControl(void);
52 
53     /**
54      * @brief Clear DistributedKvDataManager interface flow control count.
55      */
56     void KvManagerFlowControlClear(void);
57 
58     /**
59      * @brief Clear SingleKvStore interface flow control count.
60      */
61     void KvStoreFlowControlClear(void);
62 
63 protected:
64     static const size_t KVMANAGER_MAXINUM_PER_SECOND = 50;
65     static const size_t KVMANAGER_MAXINUM_PER_MINUTE = 500;
66     static const size_t KVSTORE_MAXINUM_PER_SECOND = 1000;
67     static const size_t KVSTORE_MAXINUM_PER_MINUTE = 10000;
68 
69 private:
70     size_t kvManagerSecondMaxinum_;
71     size_t kvManagerMinuteMaxinum_;
72     size_t kvStoreSecondMaxinum_;
73     size_t kvStoreMinuteMaxinum_;
74     std::list<std::chrono::system_clock::time_point> kvDataManagerTimestampList_;
75     std::list<std::chrono::system_clock::time_point> kvStoreTimestampList_;
76 };
77 }  // namespace Notification
78 }  // namespace OHOS
79 
80 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_FLOW_CONTROL_H