1 /*
2  * Copyright (c) 2020 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 #ifndef LITE_TOKEN_BUCKET_H
16 #define LITE_TOKEN_BUCKET_H
17 
18 #include <ohos_types.h>
19 
20 #ifdef __cplusplus
21 #if __cplusplus
22 extern "C" {
23 #endif
24 #endif
25 #define TOKEN_PRE_MSG 1000
26 typedef struct TokenBucket TokenBucket;
27 struct TokenBucket {
28     int rate;  // The rate of token generation per millisecond. Now, we use the TPS instead.
29     int burst; // The burst size of the token bucket
30     uint64 last; // The time of the last message
31     int used; // The used buckets. When the rate is TPS, the value is used * TOKEN_PRE_MSG.
32 };
33 typedef enum BucketStatus {
34     BUCKET_BUSY,
35     BUCKET_IDLE,
36     BUCKET_INVALID,
37 } BucketStatus;
38 
TB_InitBucket(TokenBucket * bucket,int rate,int burst)39 inline static void TB_InitBucket(TokenBucket *bucket, int rate, int burst)
40 {
41     bucket->last = 0;
42     bucket->rate = rate;
43     bucket->burst = burst;
44     bucket->used = 0;
45 }
46 int TB_CheckMessage(TokenBucket *bucket);
47 #ifdef __cplusplus
48 #if __cplusplus
49 }
50 #endif
51 #endif
52 #endif // LITE_TOKEN_BUCKET_H
53