media/mtransport/simpletokenbucket.h
author Sven Sauleau <ssauleau@igalia.com>
Tue, 05 Mar 2019 12:19:31 +0000
changeset 522212 64cc60d52a04d0c97162b18b4c3c744a11684e41
parent 505383 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1526694 [wpt PR 15276] - [wasm] update js-api test runner and expectations, a=testonly Automatic update from web-platform-tests [wasm] update js-api test runner and expectations wasm-module-builder: - consistently adds the end opcode in function bodies (using addBody). - add missing setTableLength method table/get-set: - Avoid unrelated errors like: Cannot destructure property `fn` of 'undefined' or 'null'. Bug: 926311, v8:8319 Change-Id: Ia7db1ee5c3a9b1c1d6092cdce8885ddee4dd9b0e Reviewed-on: https://chromium-review.googlesource.com/c/1458099 Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Commit-Queue: Sven Sauleau <ssauleau@igalia.com> Cr-Commit-Position: refs/heads/master@{#633121} -- wpt-commits: 22400f9755a49a50632a8f2067cb06bca74efff8 wpt-pr: 15276

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Original author: bcampen@mozilla.com */

/*
 *  This file defines a dirt-simple token bucket class.
 */

#ifndef simpletokenbucket_h__
#define simpletokenbucket_h__

#include <stdint.h>

#include "prinrval.h"

#include "m_cpp_utils.h"

namespace mozilla {

class SimpleTokenBucket {
 public:
  /*
   *  Create a SimpleTokenBucket with a given maximum size and
   *  token replenishment rate.
   *  (eg; if you want a maximum rate of 5 per second over a 7 second
   *  period, call SimpleTokenBucket b(5*7, 5);)
   */
  SimpleTokenBucket(size_t bucket_size, size_t tokens_per_second);

  /*
   *  Attempt to acquire a number of tokens. If successful, returns
   *  |num_tokens|, otherwise returns the number of tokens currently
   *  in the bucket.
   *  Note: To get the number of tokens in the bucket, pass something
   *  like UINT32_MAX.
   */
  size_t getTokens(size_t num_tokens);

 protected:  // Allow testing to touch these.
  uint64_t max_tokens_;
  uint64_t num_tokens_;
  size_t tokens_per_second_;
  PRIntervalTime last_time_tokens_added_;

  DISALLOW_COPY_ASSIGN(SimpleTokenBucket);
};

}  // namespace mozilla

#endif  // simpletokenbucket_h__