Beispiel #1
0
            public DrapiHttpClientHandler()
            {
                InnerHandler = new HttpClientHandler();

                // Denari API doesn't publish rate limits. Impose a limit of 100 requests every 10 seconds anyways.
                m_rateLimiter = SimpleRateLimiter.MaxRequestsPerInterval(100, TimeSpan.FromSeconds(10));
            }
        public HubspotService(IConfiguration configuration, IMemoryCache memoryCache)
        {
            m_client = new HttpClient(new HubspotHttpClientHandler(configuration["HubspotApiKey"]))
            {
                BaseAddress = new Uri("https://api.hubapi.com/")
            };
            m_cache = memoryCache;

            // Hubspot API has a global limit of 100 requests every 10 seconds
            m_rateLimiter = SimpleRateLimiter.MaxRequestsPerInterval(100, TimeSpan.FromSeconds(10));
            m_jitterer    = new Random();

            m_client.DefaultRequestHeaders.Accept.Clear();
            m_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
        public async Task TestRateLimitingAsync()
        {
            // 10 requests per 1 seconds
            var ratelimiter = SimpleRateLimiter.MaxRequestsPerInterval(10, TimeSpan.FromSeconds(1));
            var stopwatch   = new Stopwatch();
            var random      = new Random();

            stopwatch.Start();
            for (int i = 0; i < 101; i++)
            {
                await ratelimiter.WaitForReady();

                // simulate some kind of actual network request
                await Task.Delay(random.Next(10, 50));
            }

            stopwatch.Stop();
            Assert.That(stopwatch.Elapsed >= TimeSpan.FromSeconds(10), "Expected 101 requests would take 10 seconds or more.");
        }