-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathrecent_queue.h
52 lines (38 loc) · 1.1 KB
/
recent_queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#ifdef TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL
#pragma once
#include <algorithm>
#include <memory>
#include <mutex>
#include <vector>
#include "trpc/util/time.h"
namespace trpc::overload_control {
class RecentQueue {
public:
explicit RecentQueue(int64_t limit, uint64_t window_size);
bool Add();
int64_t ActiveCount();
private:
std::vector<uint64_t> cache_;
std::mutex mutex_;
// The location for cache eviction is also where the new timestamp is.
int64_t cur_{0};
// Maximum number of requests per second.
int64_t limit_;
// The time window size for cache eviction.
uint64_t window_size_;
};
using RecentQueuePtr = std::unique_ptr<RecentQueue>;
} // namespace trpc::overload_control
#endif