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