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 #include "distributed_flow_control.h"
17
18 namespace OHOS {
19 namespace Notification {
DistributedFlowControl(size_t kvManagerSecondMaxinum,size_t kvManagerMinuteMaxinum,size_t kvStoreSecondMaxinum,size_t kvStoreMinuteMaxinum)20 DistributedFlowControl::DistributedFlowControl(
21 size_t kvManagerSecondMaxinum, size_t kvManagerMinuteMaxinum, size_t kvStoreSecondMaxinum,
22 size_t kvStoreMinuteMaxinum)
23 : kvManagerSecondMaxinum_(kvManagerSecondMaxinum),
24 kvManagerMinuteMaxinum_(kvManagerMinuteMaxinum),
25 kvStoreSecondMaxinum_(kvStoreSecondMaxinum),
26 kvStoreMinuteMaxinum_(kvStoreMinuteMaxinum)
27 {}
28
KvManagerFlowControl(void)29 bool DistributedFlowControl::KvManagerFlowControl(void)
30 {
31 std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
32 kvDataManagerTimestampList_.remove_if([&](const std::chrono::system_clock::time_point &value) -> bool {
33 return now - value > std::chrono::minutes(1);
34 });
35
36 size_t listSize = kvStoreTimestampList_.size();
37 if (listSize >= kvManagerMinuteMaxinum_) {
38 return false;
39 }
40
41 size_t count = 0;
42 for (auto value : kvDataManagerTimestampList_) {
43 if (now - value > std::chrono::seconds(1)) {
44 if (count >= kvManagerSecondMaxinum_) {
45 return false;
46 } else {
47 break;
48 }
49 }
50 count++;
51 }
52
53 kvDataManagerTimestampList_.push_front(now);
54 return true;
55 }
56
KvStoreFlowControl(void)57 bool DistributedFlowControl::KvStoreFlowControl(void)
58 {
59 std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
60 kvStoreTimestampList_.remove_if([&](const std::chrono::system_clock::time_point &value) -> bool {
61 return now - value > std::chrono::minutes(1);
62 });
63
64 size_t listSize = kvStoreTimestampList_.size();
65 if (listSize >= kvStoreMinuteMaxinum_) {
66 return false;
67 }
68
69 size_t count = 0;
70 for (auto value : kvStoreTimestampList_) {
71 if (now - value > std::chrono::seconds(1)) {
72 if (count >= kvStoreSecondMaxinum_) {
73 return false;
74 } else {
75 break;
76 }
77 }
78 count++;
79 }
80
81 kvStoreTimestampList_.push_front(now);
82 return true;
83 }
84
KvManagerFlowControlClear(void)85 void DistributedFlowControl::KvManagerFlowControlClear(void)
86 {
87 kvDataManagerTimestampList_.clear();
88 }
89
KvStoreFlowControlClear(void)90 void DistributedFlowControl::KvStoreFlowControlClear(void)
91 {
92 kvStoreTimestampList_.clear();
93 }
94 } // namespace Notification
95 } // namespace OHOS
96