Beispiel #1
0
        /// <summary>
        /// Adds tokens to your IServiceCollection
        /// </summary>
        /// <typeparam name="TTokenBase"></typeparam>
        /// <param name="tokens"></param>
        /// <returns></returns>
        public HostConfiguration AddTokens <TTokenBase>(IEnumerable <TTokenBase> tokens) where TTokenBase : TokenBase
        {
            TokenContainer <TTokenBase> container = new TokenContainer <TTokenBase>(tokens);

            _services.AddSingleton(services => container);

            return(this);
        }
Beispiel #2
0
        /// <summary>
        /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout.
        /// </summary>
        /// <param name="container"></param>
        public RateLimitProvider(TokenContainer <TTokenBase> container) : base(container.Tokens)
        {
            _semaphore = new SemaphoreSlim(1, 1);

            foreach (TTokenBase token in _tokens)
            {
                token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40));
            }

            for (int i = 0; i < _tokens.Length; i++)
            {
                _tokens[i].TokenBecameAvailable += ((sender) =>
                {
                    TTokenBase token = (TTokenBase)sender;

                    AvailableTokens.TryAdd(token, token);
                });
            }
        }
Beispiel #3
0
        /// <summary>
        /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout.
        /// </summary>
        /// <param name="container"></param>
        public RateLimitProvider(TokenContainer <TTokenBase> container) : base(container.Tokens)
        {
            foreach (TTokenBase token in _tokens)
            {
                token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40));
            }

            BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length)
            {
                FullMode = BoundedChannelFullMode.DropWrite
            };

            AvailableTokens = Channel.CreateBounded <TTokenBase>(options);

            for (int i = 0; i < _tokens.Length; i++)
            {
                _tokens[i].TokenBecameAvailable += ((sender) => AvailableTokens.Writer.TryWrite((TTokenBase)sender));
            }
        }