michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Original author: bcampen@mozilla.com */ michael@0: michael@0: /* michael@0: * This file defines a dirt-simple token bucket class. michael@0: */ michael@0: michael@0: #ifndef simpletokenbucket_h__ michael@0: #define simpletokenbucket_h__ michael@0: michael@0: #include michael@0: michael@0: #include "prinrval.h" michael@0: michael@0: #include "m_cpp_utils.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: class SimpleTokenBucket { michael@0: public: michael@0: /* michael@0: * Create a SimpleTokenBucket with a given maximum size and michael@0: * token replenishment rate. michael@0: * (eg; if you want a maximum rate of 5 per second over a 7 second michael@0: * period, call SimpleTokenBucket b(5*7, 5);) michael@0: */ michael@0: SimpleTokenBucket(size_t bucket_size, size_t tokens_per_second); michael@0: michael@0: /* michael@0: * Attempt to acquire a number of tokens. If successful, returns michael@0: * |num_tokens|, otherwise returns the number of tokens currently michael@0: * in the bucket. michael@0: * Note: To get the number of tokens in the bucket, pass something michael@0: * like UINT32_MAX. michael@0: */ michael@0: size_t getTokens(size_t num_tokens); michael@0: michael@0: protected: // Allow testing to touch these. michael@0: uint64_t max_tokens_; michael@0: uint64_t num_tokens_; michael@0: size_t tokens_per_second_; michael@0: PRIntervalTime last_time_tokens_added_; michael@0: michael@0: DISALLOW_COPY_ASSIGN(SimpleTokenBucket); michael@0: }; michael@0: } // namespace mozilla michael@0: michael@0: #endif // simpletokenbucket_h__ michael@0: