Пример #1
0
        public override async Task <bool> DownloadImage(int width, string imageUrl, string outputFilePath)
        {
            if (string.IsNullOrEmpty(imageUrl))
            {
                return(false);
            }

            _imageBucket.Consume(1);
            return(await DownloadImageInternal($"https://image.tmdb.org/t/p/w{width}/{imageUrl}", outputFilePath));
        }
Пример #2
0
        static void Main(string[] args)
        {
            // Create a token bucket with a capacity of 1 token that refills at a fixed interval of 1 token/sec.
            ITokenBucket bucket = TokenBuckets.Construct()
                                  .WithCapacity(100)
                                  .WithFixedIntervalRefillStrategy(100, TimeSpan.FromSeconds(1))
                                  .Build();

            // ...

            while (true)
            {
                // Consume a token from the token bucket.  If a token is not available this method will block until
                // the refill strategy adds one to the bucket.
                bucket.Consume(1);

                Poll();
            }
        }
Пример #3
0
        private async Task <string> Fetch(string url)
        {
            var cacheKey = $"{CachePrefix}:{url}";

            {
                var cachedContent = await CachingService.GetStringAsync(cacheKey);

                if (!string.IsNullOrEmpty(cachedContent))
                {
                    return(cachedContent);
                }
            }

            _bucket.Consume(1);

            var content = await FetchInternal(cacheKey, url);

            if (string.IsNullOrEmpty(content) || content.StartsWith("{\"status_code\":25"))
            {
                return(await Fetch(url));
            }

            return(content);
        }
Пример #4
0
 /// <summary>
 /// Provides an overload of <see cref="ITokenBucket.Consume"/> that accepts a <see cref="TimeSpan"/> timeout
 /// </summary>
 public static void Consume(this ITokenBucket bucket, long tokens, TimeSpan timeout)
 {
     bucket.Consume(tokens, (long)timeout.TotalMilliseconds);
 }