예제 #1
0
        private static BaseApiModel <TModelType> AuthenticateAndThrottle <TModelType>(this IRestClient client, IRestRequest request)
        {
            BaseApiModel <TModelType> responseObject = null;

            GlobalThrottle.Run(() =>
            {
                AuthorizeRequest(request);
                ThrottleRequest();

                var response   = client.Execute(request);
                responseObject = JsonConvert.DeserializeObject <BaseApiModel <TModelType> >(response.Content);
                AssignNewThrottle(responseObject);
            }, new CancellationToken());
            return(responseObject);
        }
예제 #2
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
            }
        }
 protected override Task<HttpResponseMessage> SendAsync(
     HttpRequestMessage request,
     CancellationToken cancellationToken)
 {
     return _semaphoreSlim.Run(() => base.SendAsync(request, cancellationToken));
 }