/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Threading; namespace QuantConnect.Util.RateLimit { /// /// Defines a token bucket for rate limiting /// See: https://en.wikipedia.org/wiki/Token_bucket /// /// /// This code is ported from https://github.com/mxplusb/TokenBucket - since it's a dotnet core /// project, there were issued importing the nuget package directly. The referenced repository /// is provided under the Apache V2 license. /// public interface ITokenBucket { /// /// Gets the maximum capacity of tokens this bucket can hold. /// long Capacity { get; } /// /// Gets the total number of currently available tokens for consumption /// long AvailableTokens { get; } /// /// Blocks until the specified number of tokens are available for consumption /// and then consumes that number of tokens. /// /// The number of tokens to consume /// The maximum amount of time, in milliseconds, to block. A /// is throw in the event it takes longer than the stated timeout to consume the requested number of tokens. /// The default timeout is set to infinite, which will block forever. void Consume(long tokens, long timeout = Timeout.Infinite); /// /// Attempts to consume the specified number of tokens from the bucket. If the /// requested number of tokens are not immediately available, then this method /// will return false to indicate that zero tokens have been consumed. /// bool TryConsume(long tokens); } }