chromium/components/reporting/util/wrapped_rate_limiter.h

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_REPORTING_UTIL_WRAPPED_RATE_LIMITER_H_
#define COMPONENTS_REPORTING_UTIL_WRAPPED_RATE_LIMITER_H_

#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "components/reporting/util/rate_limiter_interface.h"

namespace reporting {

// This class wraps around rate limiter instance to ensure the latter
// is used sequentially. It is instantiated by calling
// `WrappedRateLimiter::Create`.
//
// The usage snippet in an owner class:
//
// class RateLimitedEventSender {
//  public:
//   explicit RateLimitedEventSender(
//       std::unique_ptr<RateLimiterInterface> rate_limiter)
//       : wrapped_rate_limiter_(
//             WrappedRateLimiter::Create(std::move(rate_limiter))),
//         async_acquire_cb_(wrapped_rate_limiter_->async_acquire_cb()) {}
//
//   void SendEvent(Event event, EnqueueCallback cb) {
//     const size_t event_size = event.ByteSizeLong();
//     async_acquire_cb_.Run(
//         event_size,
//         base::BindOnce([](Event event, EnqueueCallback cb, bool acquired) {
//             if (!acquired) {
//               std::move(cb).Run(Status{error::OUT_OF_RANGE, "..."});
//               return;
//             }
//             ...  // Actually enqueue `event`, using `cb` to return outcome.
//         }, std::move(event), std::move(cb));
//   }
//
//  private:
//   WrappedRateLimiter::SmartPtr wrapped_rate_limiter_;
//   AsyncAcquireCb async_acquire_cb_;
// };
//
class WrappedRateLimiter {};
}  // namespace reporting

#endif  // COMPONENTS_REPORTING_UTIL_WRAPPED_RATE_LIMITER_H_