public HttpClient CreateHttpClient(HttpClientOptions options)
        {
            HttpMessageHandler handler = new HttpClientHandler {AllowAutoRedirect = options.AllowAutoRedirect};
            if (((HttpClientHandler) handler).SupportsAutomaticDecompression)
                ((HttpClientHandler) handler).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

            if (options.AddTokenToRequests)
                handler = new AccessTokenAuthenticator(options.TokenRetriever, handler);

            TimeSpanSemaphore readTimeSpanSemaphore = null;
            TimeSpanSemaphore writeTimeSpanSemaphore = null;
            if (options.ReadRequestsPerSecond.HasValue)
            {
                double delay = (1/options.ReadRequestsPerSecond.Value);
                readTimeSpanSemaphore = (_readTimeSpanSemaphore ?? (_readTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay))));
            }
            if (options.WriteRequestsPerSecond.HasValue)
            {
                double delay = (1/options.WriteRequestsPerSecond.Value);
                writeTimeSpanSemaphore = (_writeTimeSpanSemaphore ?? (_writeTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay))));
            }

            if (readTimeSpanSemaphore != null || writeTimeSpanSemaphore != null)
            {
                handler = new ThrottlingMessageHandler(readTimeSpanSemaphore, writeTimeSpanSemaphore, handler);
            }
            return new HttpClient(handler);
        }
        public HttpClient CreateHttpClient(HttpClientOptions options)
        {
            HttpMessageHandler httpMessageHandler = new HttpClientHandler
            {
                AllowAutoRedirect = options.AllowAutoRedirect
            };
            if (((HttpClientHandler)httpMessageHandler).SupportsAutomaticDecompression)
                ((HttpClientHandler)httpMessageHandler).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

            if (options.AddTokenToRequests)
                httpMessageHandler = new AccessTokenAuthenticator(options.TokenRetriever, httpMessageHandler);

            TimeSpanSemaphore readTimeSpanSemaphore = null;
            TimeSpanSemaphore writeTimeSpanSemaphore = null;

            if (options.ReadRequestsPerSecond.HasValue)
                readTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(1.0 / options.ReadRequestsPerSecond.Value));

            if (options.WriteRequestsPerSecond.HasValue)
                writeTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(1.0 / options.WriteRequestsPerSecond.Value));

            if (readTimeSpanSemaphore != null || writeTimeSpanSemaphore != null)
                httpMessageHandler = new ThrottlingMessageHandler(readTimeSpanSemaphore, writeTimeSpanSemaphore, httpMessageHandler);

            httpMessageHandler = new RemoveEtagDelegatingHandler(httpMessageHandler);

            return new HttpClient(httpMessageHandler);
        }
 public PreventTooManyRequestsDelegatingHandler(int requestsPerSecond, HttpMessageHandler next)
     :base(next)
 {
     _semaphoreSlim = new TimeSpanSemaphore(
         requestsPerSecond,
         TimeSpan.FromSeconds(1));
 }
Example #4
0
        public void TimeSpanSemaphoreTest()
        {
            using (var throttle = new TimeSpanSemaphore(5, TimeSpan.FromSeconds(1)))
            {
                for (int i = 1; i <= 6; i++)
                {
                    Thread t = new Thread(new ParameterizedThreadStart((taskId) =>
                    {
                        throttle.Run(
                            () => DoWork((int)taskId),
                            CancellationToken.None);
                    }));
                    t.Start(i);
                }

                Thread.Sleep(2000); // give all the threads a chance to finish

                // NOTE: view the console output in Test Explorer
            }
        }
Example #5
0
        public HttpClient CreateHttpClient(HttpClientOptions options)
        {
            HttpMessageHandler handler = new HttpClientHandler {
                AllowAutoRedirect = options.AllowAutoRedirect
            };

            if (((HttpClientHandler)handler).SupportsAutomaticDecompression)
            {
                ((HttpClientHandler)handler).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            }

            if (options.AddTokenToRequests)
            {
                handler = new AccessTokenAuthenticator(options.TokenRetriever, handler);
            }

            TimeSpanSemaphore readTimeSpanSemaphore  = null;
            TimeSpanSemaphore writeTimeSpanSemaphore = null;

            if (options.ReadRequestsPerSecond.HasValue)
            {
                double delay = (1 / options.ReadRequestsPerSecond.Value);
                readTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay));
            }
            if (options.WriteRequestsPerSecond.HasValue)
            {
                double delay = (1 / options.WriteRequestsPerSecond.Value);
                writeTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay));
            }

            if (readTimeSpanSemaphore != null || writeTimeSpanSemaphore != null)
            {
                handler = new ThrottlingMessageHandler(readTimeSpanSemaphore, writeTimeSpanSemaphore, handler);
            }
            return(new HttpClient(handler));
        }
 public ThrottlingMessageHandler(TimeSpanSemaphore readTimeSpanSemaphore, TimeSpanSemaphore writeTimeSpanSemaphore, HttpMessageHandler innerHandler)
     : base(innerHandler)
 {
     _readTimeSpanSemaphore  = readTimeSpanSemaphore;
     _writeTimeSpanSemaphore = writeTimeSpanSemaphore;
 }
 public ThrottlingMessageHandler(TimeSpanSemaphore readTimeSpanSemaphore, TimeSpanSemaphore writeTimeSpanSemaphore)
     : this(readTimeSpanSemaphore, writeTimeSpanSemaphore, null)
 {
 }
 public ThrottlingMessageHandler(TimeSpanSemaphore readTimeSpanSemaphore, TimeSpanSemaphore writeTimeSpanSemaphore, HttpMessageHandler innerHandler = null)
     : base(innerHandler)
 {
     _readTimeSpanSemaphore = readTimeSpanSemaphore;
     _writeTimeSpanSemaphore = writeTimeSpanSemaphore;
 }
 public ThrottlingMessageHandler(TimeSpanSemaphore readTimeSpanSemaphore, TimeSpanSemaphore writeTimeSpanSemaphore)
     : this(readTimeSpanSemaphore, writeTimeSpanSemaphore, null)
 {
 }
 public ThrottlingMessageHandler(TimeSpanSemaphore timeSpanSemaphore)
     : base(new HttpClientHandler())
 {
     _timeSpanSemaphore = timeSpanSemaphore;
 }