aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdutils/td/utils/FloodControlGlobal.h
blob: 9c783c811ae326a74bff137341cbd067c928e869 (plain)
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
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once

#include "td/utils/common.h"

#include <atomic>
#include <memory>

namespace td {

// Restricts the total number of events
class FloodControlGlobal {
 public:
  explicit FloodControlGlobal(uint64 limit);

  struct Finish {
    void operator()(FloodControlGlobal *ctrl) const;
  };
  using Guard = std::unique_ptr<FloodControlGlobal, Finish>;

  Guard try_start();

 private:
  std::atomic<uint64> active_count_{0};
  uint64 limit_{0};

  void finish();
};

}  // namespace td