tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

simpletokenbucket.h (1481B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /* Original author: bcampen@mozilla.com */
      8 
      9 /*
     10 *  This file defines a dirt-simple token bucket class.
     11 */
     12 
     13 #ifndef simpletokenbucket_h__
     14 #define simpletokenbucket_h__
     15 
     16 #include <stdint.h>
     17 
     18 #include "m_cpp_utils.h"
     19 #include "prinrval.h"
     20 
     21 namespace mozilla {
     22 
     23 class SimpleTokenBucket {
     24 public:
     25  /*
     26   *  Create a SimpleTokenBucket with a given maximum size and
     27   *  token replenishment rate.
     28   *  (eg; if you want a maximum rate of 5 per second over a 7 second
     29   *  period, call SimpleTokenBucket b(5*7, 5);)
     30   */
     31  SimpleTokenBucket(size_t bucket_size, size_t tokens_per_second);
     32 
     33  /*
     34   *  Attempt to acquire a number of tokens. If successful, returns
     35   *  |num_tokens|, otherwise returns the number of tokens currently
     36   *  in the bucket.
     37   *  Note: To get the number of tokens in the bucket, pass something
     38   *  like UINT32_MAX.
     39   */
     40  size_t getTokens(size_t num_tokens);
     41 
     42 protected:  // Allow testing to touch these.
     43  uint64_t max_tokens_;
     44  uint64_t num_tokens_;
     45  size_t tokens_per_second_;
     46  PRIntervalTime last_time_tokens_added_;
     47 
     48  DISALLOW_COPY_ASSIGN(SimpleTokenBucket);
     49 };
     50 
     51 }  // namespace mozilla
     52 
     53 #endif  // simpletokenbucket_h__