1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/simpletokenbucket.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,54 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* Original author: bcampen@mozilla.com */ 1.11 + 1.12 +/* 1.13 + * This file defines a dirt-simple token bucket class. 1.14 + */ 1.15 + 1.16 +#ifndef simpletokenbucket_h__ 1.17 +#define simpletokenbucket_h__ 1.18 + 1.19 +#include <stdint.h> 1.20 + 1.21 +#include "prinrval.h" 1.22 + 1.23 +#include "m_cpp_utils.h" 1.24 + 1.25 +namespace mozilla { 1.26 + 1.27 +class SimpleTokenBucket { 1.28 + public: 1.29 + /* 1.30 + * Create a SimpleTokenBucket with a given maximum size and 1.31 + * token replenishment rate. 1.32 + * (eg; if you want a maximum rate of 5 per second over a 7 second 1.33 + * period, call SimpleTokenBucket b(5*7, 5);) 1.34 + */ 1.35 + SimpleTokenBucket(size_t bucket_size, size_t tokens_per_second); 1.36 + 1.37 + /* 1.38 + * Attempt to acquire a number of tokens. If successful, returns 1.39 + * |num_tokens|, otherwise returns the number of tokens currently 1.40 + * in the bucket. 1.41 + * Note: To get the number of tokens in the bucket, pass something 1.42 + * like UINT32_MAX. 1.43 + */ 1.44 + size_t getTokens(size_t num_tokens); 1.45 + 1.46 + protected: // Allow testing to touch these. 1.47 + uint64_t max_tokens_; 1.48 + uint64_t num_tokens_; 1.49 + size_t tokens_per_second_; 1.50 + PRIntervalTime last_time_tokens_added_; 1.51 + 1.52 + DISALLOW_COPY_ASSIGN(SimpleTokenBucket); 1.53 +}; 1.54 +} // namespace mozilla 1.55 + 1.56 +#endif // simpletokenbucket_h__ 1.57 +