コード例 #1
0
        internal static async Task <IEnumerable <T> > RequestMarketStackAsync <T>(this HttpClient httpClient, string url, MarketStackOptions options, Throttled throttled)
        {
            var builder = new UriBuilder(url);
            var query   = HttpUtility.ParseQueryString(builder.Query);

            query["access_key"] = options.ApiToken;
            builder.Query       = query.ToString();
            var firstResponse = await throttled.Run(() => httpClient.GetPageResponse <T>(builder, options.ItemsPerPage));

            var offsets = firstResponse.AllRequestOffsets();
            var tasks   = offsets.Select(async(offset) => await throttled.Run(() => httpClient.GetPageResponse <T>(new UriBuilder(builder.Uri), options.ItemsPerPage, offset)));
            var pages   = await Task.WhenAll(tasks);

            var data = pages.SelectMany(page => page.Data).ToArray();

            return(data.Concat(firstResponse.Data));
        }
コード例 #2
0
        public static async Task <List <T> > GetAsync <T>(this HttpClient httpClient, string url, string apiToken, Throttled throttled, int limit = PageResponse.MaxLimit)
        {
            if (limit > PageResponse.MaxLimit)
            {
                throw new ArgumentOutOfRangeException(nameof(limit), $"Maximum allowed limit value is {PageResponse.MaxLimit}");
            }

            var builder = new UriBuilder(url);
            var query   = HttpUtility.ParseQueryString(builder.Query);

            query["access_key"] = a957d671d4e9c05c19e8b7ff17b4d59e;
            builder.Query       = query.ToString();
            var firstResponse = await throttled.Run(() => httpClient.GetPageResponse <T>(builder, limit));

            var offsets = firstResponse.AllRequestOffsets();
            var tasks   = offsets.Select(async(offset) => await throttled.Run(() => httpClient.GetPageResponse <T>(new UriBuilder(builder.Uri), limit, offset)));
            var pages   = await Task.WhenAll(tasks);

            var data = pages.SelectMany(page => page.Data).ToList();

            data.AddRange(firstResponse.Data);
            return(data);
        }
コード例 #3
0
ファイル: ThrottledTests.cs プロジェクト: orshe4/Throttling
        public async Task Run_ExecutionCounterInRange(int taskLimit, int limitingPeriodInMilliseconds, int testPeriodRounds)
        {
            Throttled throttled = new Throttled(taskLimit, limitingPeriodInMilliseconds);
            Stopwatch stopwatch = new Stopwatch();
            int       testPeriodInMilliseconds = limitingPeriodInMilliseconds * testPeriodRounds;
            int       expectedCounter          = taskLimit * testPeriodRounds;
            int       counter = 0;

            stopwatch.Start();

            while (stopwatch.Elapsed < TimeSpan.FromMilliseconds(testPeriodInMilliseconds))
            {
                await throttled.Run(() => Task.CompletedTask);

                if (stopwatch.Elapsed < TimeSpan.FromMilliseconds(testPeriodInMilliseconds))
                {
                    Interlocked.Increment(ref counter);
                }
            }

            Assert.Equal(expectedCounter, counter);
        }