periodic_update.h
Go to the documentation of this file.
1 // Copyright 2022 gRPC authors.
2 //
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 GRPC_CORE_LIB_RESOURCE_QUOTA_PERIODIC_UPDATE_H
16 #define GRPC_CORE_LIB_RESOURCE_QUOTA_PERIODIC_UPDATE_H
17 
19 
20 #include <inttypes.h>
21 
22 #include <atomic>
23 
26 
27 namespace grpc_core {
28 
29 // Lightweight timer-like mechanism for periodic updates.
30 // Fast path only decrements an atomic int64.
31 // Slow path runs corrections and estimates how many ticks are required to hit
32 // the target period.
33 // This is super inaccurate of course, but for places where we can't run timers,
34 // or places where continuous registration/unregistration would cause problems
35 // it can be quite useful.
37  public:
38  explicit PeriodicUpdate(Duration period) : period_(period) {}
39 
40  // Tick the update, return true if we think the period expired.
42  // Atomically decrement the remaining ticks counter.
43  // If we hit 0 our estimate of period length has expired.
44  // See the comment next to the data members for a description of thread
45  // safety.
46  if (updates_remaining_.fetch_sub(1, std::memory_order_acquire) == 1) {
47  return MaybeEndPeriod();
48  }
49  return false;
50  }
51 
52  private:
54 
55  // Thread safety:
56  // When updates_remaining_ reaches 0 the thread that decremented becomes
57  // responsible for updating any mutable variables and then setting
58  // updates_remaining_ to a value greater than zero.
59  // Whilst in this state other threads *may* decrement updates_remaining_, but
60  // this is fine because they'll observe an ignorable negative value.
61 
65  std::atomic<int64_t> updates_remaining_{1};
66 };
67 
68 } // namespace grpc_core
69 
70 #endif // GRPC_CORE_LIB_RESOURCE_QUOTA_PERIODIC_UPDATE_H
grpc_core::PeriodicUpdate::Tick
GRPC_MUST_USE_RESULT bool Tick()
Definition: periodic_update.h:41
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc_core::PeriodicUpdate::updates_remaining_
std::atomic< int64_t > updates_remaining_
Definition: periodic_update.h:65
grpc_core::PeriodicUpdate
Definition: periodic_update.h:36
grpc_core::PeriodicUpdate::period_
const Duration period_
Definition: periodic_update.h:62
time.h
grpc_core::PeriodicUpdate::MaybeEndPeriod
GRPC_MUST_USE_RESULT bool MaybeEndPeriod()
Definition: periodic_update.cc:25
grpc_core::PeriodicUpdate::expected_updates_per_period_
int64_t expected_updates_per_period_
Definition: periodic_update.h:64
grpc_core::PeriodicUpdate::PeriodicUpdate
PeriodicUpdate(Duration period)
Definition: periodic_update.h:38
GRPC_MUST_USE_RESULT
#define GRPC_MUST_USE_RESULT
Definition: impl/codegen/port_platform.h:584
exec_ctx.h
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
grpc_core::PeriodicUpdate::period_start_
Timestamp period_start_
Definition: periodic_update.h:63
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:42