Albert
Loading...
Searching...
No Matches
ratelimiter.h
1// SPDX-FileCopyrightText: 2025-2026 Manuel Schneider
2// SPDX-License-Identifier: MIT
3
4#pragma once
5#include <QObject>
6#include <albert/export.h>
7#include <memory>
8#include <chrono>
9#include <coroutine>
10
11namespace albert::detail
12{
13
14class ALBERT_EXPORT Acquire : public QObject
15{
16 Q_OBJECT
17public:
18
19 struct Awaiter {
20 Acquire* acquire;
21 bool await_ready() const noexcept;
22 void await_suspend(std::coroutine_handle<> handle);
23 void await_resume() const noexcept;
24 };
25
26 inline auto operator co_await() { return Awaiter{this}; }
27
28signals:
29 void granted();
30};
31
32template<typename T>
33 requires std::same_as<typename T::element_type, Acquire>
34inline auto operator co_await(T&& acquire) {
35 return Acquire::Awaiter{acquire.get()};
36}
37
38
39class ALBERT_EXPORT RateLimiter : public QObject
40{
41 Q_OBJECT
42public:
43 RateLimiter();
44 ~RateLimiter() override;
45
46 void limit(std::chrono::milliseconds ms);
47 bool isLimited() const;
48
49 std::unique_ptr<Acquire> acquire();
50
51private:
52 class Private;
53 std::unique_ptr<Private> d;
54};
55
56} // namespace albert::detail