media/mtransport/simpletokenbucket.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:bca586e0986b
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 "prinrval.h"
19
20 #include "m_cpp_utils.h"
21
22 namespace mozilla {
23
24 class SimpleTokenBucket {
25 public:
26 /*
27 * Create a SimpleTokenBucket with a given maximum size and
28 * token replenishment rate.
29 * (eg; if you want a maximum rate of 5 per second over a 7 second
30 * period, call SimpleTokenBucket b(5*7, 5);)
31 */
32 SimpleTokenBucket(size_t bucket_size, size_t tokens_per_second);
33
34 /*
35 * Attempt to acquire a number of tokens. If successful, returns
36 * |num_tokens|, otherwise returns the number of tokens currently
37 * in the bucket.
38 * Note: To get the number of tokens in the bucket, pass something
39 * like UINT32_MAX.
40 */
41 size_t getTokens(size_t num_tokens);
42
43 protected: // Allow testing to touch these.
44 uint64_t max_tokens_;
45 uint64_t num_tokens_;
46 size_t tokens_per_second_;
47 PRIntervalTime last_time_tokens_added_;
48
49 DISALLOW_COPY_ASSIGN(SimpleTokenBucket);
50 };
51 } // namespace mozilla
52
53 #endif // simpletokenbucket_h__
54

mercurial