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