1 /*
2  * Copyright (c) 2021 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 #define LOG_TAG "KvStoreFlowCtrlManager"
17 
18 #include "kvstore_flowctrl_manager.h"
19 #include <cinttypes>
20 #include <sys/time.h>
21 
22 namespace OHOS {
23 namespace DistributedKv {
24 const int SECOND_TO_MICROSECOND = 1000;
CurrentTimeMicros()25 uint64_t CurrentTimeMicros()
26 {
27     struct timeval tv = { 0, 0 };
28     gettimeofday(&tv, nullptr);
29     return (tv.tv_sec * SECOND_TO_MICROSECOND + tv.tv_usec / SECOND_TO_MICROSECOND);
30 }
31 
KvStoreFlowCtrlManager(const int burstCapacity,const int sustainedCapacity)32 KvStoreFlowCtrlManager::KvStoreFlowCtrlManager(const int burstCapacity, const int sustainedCapacity)
33 {
34     burstTokenBucket_.maxCapacity = burstCapacity;
35     burstTokenBucket_.refreshTimeGap = BURST_REFRESH_TIME;
36     sustainedTokenBucket_.maxCapacity = sustainedCapacity;
37     sustainedTokenBucket_.refreshTimeGap = SUSTAINED_REFRESH_TIME;
38 }
39 
RefreshTokenBucket(TokenBucket & tokenBucket,uint64_t timestamp)40 void KvStoreFlowCtrlManager::RefreshTokenBucket(TokenBucket &tokenBucket, uint64_t timestamp)
41 {
42     tokenBucket.leftNumInTokenBucket = tokenBucket.maxCapacity;
43     tokenBucket.tokenBucketRefreshTime = timestamp;
44 }
45 
IsTokenEnough()46 bool KvStoreFlowCtrlManager::IsTokenEnough()
47 {
48     uint64_t curTime = CurrentTimeMicros();
49     if (IsTokenEnoughSlice(burstTokenBucket_, curTime) && IsTokenEnoughSlice(sustainedTokenBucket_, curTime)) {
50         burstTokenBucket_.lastAccessTime = curTime;
51         burstTokenBucket_.leftNumInTokenBucket--;
52 
53         sustainedTokenBucket_.lastAccessTime = curTime;
54         sustainedTokenBucket_.leftNumInTokenBucket--;
55         return true;
56     }
57     return false;
58 }
59 
IsTokenEnoughSlice(TokenBucket & tokenBucket,uint64_t timestamp)60 bool KvStoreFlowCtrlManager::IsTokenEnoughSlice(TokenBucket &tokenBucket, uint64_t timestamp)
61 {
62     // the first time to get token will be allowed;
63     // if the gap between this time to get token and the least time to fill the bucket
64     // to the full is larger than 10ms, this operation will be allowed;
65     if (tokenBucket.tokenBucketRefreshTime == 0 ||
66         timestamp - tokenBucket.tokenBucketRefreshTime > tokenBucket.refreshTimeGap) {
67         RefreshTokenBucket(tokenBucket, timestamp);
68         return true;
69     } else {
70         return tokenBucket.leftNumInTokenBucket >= 1;
71     }
72 }
73 } // namespace DistributedKv
74 } // namespace OHOS
75