Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7876541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:10:56+00:00 2026-06-03T03:10:56+00:00

I need to implement a throttling mechanism (requests per second) when using HttpWebRequest for

  • 0

I need to implement a throttling mechanism (requests per second) when using HttpWebRequest for making parallel requests towards one application server. My C# app must issue no more than 80 requests per second to a remote server. The limit is imposed by the remote service admins not as a hard limit but as “SLA” between my platform and theirs.

How can I control the number of requests per second when using HttpWebRequest?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:10:57+00:00Added an answer on June 3, 2026 at 3:10 am

    I had the same problem and couldn’t find a ready solution so I made one, and here it is. The idea is to use a BlockingCollection<T> to add items that need processing and use Reactive Extensions to subscribe with a rate-limited processor.

    Throttle class is the renamed version of this rate limiter

    public static class BlockingCollectionExtensions
    {
        // TODO: devise a way to avoid problems if collection gets too big (produced faster than consumed)
        public static IObservable<T> AsRateLimitedObservable<T>(this BlockingCollection<T> sequence, int items, TimeSpan timePeriod, CancellationToken producerToken)
        {
            Subject<T> subject = new Subject<T>();
    
            // this is a dummyToken just so we can recreate the TokenSource
            // which we will pass the proxy class so it can cancel the task
            // on disposal
            CancellationToken dummyToken = new CancellationToken();
            CancellationTokenSource tokenSource = CancellationTokenSource.CreateLinkedTokenSource(producerToken, dummyToken);
    
            var consumingTask = new Task(() =>
            {
                using (var throttle = new Throttle(items, timePeriod))
                {
                    while (!sequence.IsCompleted)
                    {
                        try
                        {
                            T item = sequence.Take(producerToken);
                            throttle.WaitToProceed();
                            try
                            {
                                subject.OnNext(item);
                            }
                            catch (Exception ex)
                            {
                                subject.OnError(ex);
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            break;
                        }
                    }
                    subject.OnCompleted();
                }
            }, TaskCreationOptions.LongRunning);
    
            return new TaskAwareObservable<T>(subject, consumingTask, tokenSource);
        }
    
        private class TaskAwareObservable<T> : IObservable<T>, IDisposable
        {
            private readonly Task task;
            private readonly Subject<T> subject;
            private readonly CancellationTokenSource taskCancellationTokenSource;
    
            public TaskAwareObservable(Subject<T> subject, Task task, CancellationTokenSource tokenSource)
            {
                this.task = task;
                this.subject = subject;
                this.taskCancellationTokenSource = tokenSource;
            }
    
            public IDisposable Subscribe(IObserver<T> observer)
            {
                var disposable = subject.Subscribe(observer);
                if (task.Status == TaskStatus.Created)
                    task.Start();
                return disposable;
            }
    
            public void Dispose()
            {
                // cancel consumption and wait task to finish
                taskCancellationTokenSource.Cancel();
                task.Wait();
    
                // dispose tokenSource and task
                taskCancellationTokenSource.Dispose();
                task.Dispose();
    
                // dispose subject
                subject.Dispose();
            }
        }
    }
    

    Unit test:

    class BlockCollectionExtensionsTest
    {
        [Fact]
        public void AsRateLimitedObservable()
        {
            const int maxItems = 1; // fix this to 1 to ease testing
            TimeSpan during = TimeSpan.FromSeconds(1);
    
            // populate collection
            int[] items = new[] { 1, 2, 3, 4 };
            BlockingCollection<int> collection = new BlockingCollection<int>();
            foreach (var i in items) collection.Add(i);
            collection.CompleteAdding();
    
            IObservable<int> observable = collection.AsRateLimitedObservable(maxItems, during, CancellationToken.None);
            BlockingCollection<int> processedItems = new BlockingCollection<int>();
            ManualResetEvent completed = new ManualResetEvent(false);
            DateTime last = DateTime.UtcNow;
            observable
                // this is so we'll receive exceptions
                .ObserveOn(new SynchronizationContext()) 
                .Subscribe(item =>
                    {
                        if (item == 1)
                            last = DateTime.UtcNow;
                        else
                        {
                            TimeSpan diff = (DateTime.UtcNow - last);
                            last = DateTime.UtcNow;
    
                            Assert.InRange(diff.TotalMilliseconds,
                                during.TotalMilliseconds - 30,
                                during.TotalMilliseconds + 30);
                        }
                        processedItems.Add(item);
                    },
                    () => completed.Set()
                );
            completed.WaitOne();
            Assert.Equal(items, processedItems, new CollectionEqualityComparer<int>());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement a set of sets in my application. Using QSet with
I need to implement handling of redelivery of JMS messages in the application that
I need to implement an MVC site with urls per below: category1/product/1/wiki category1/product/2/wiki category1/sub-category2/product/3/wiki
I need to implement the following flow in my application: the user can click
We need to implement client / server to open / edit Office documents from
I need to implement a WatchList class as a part of a Java client-server
I need to implement a web application hosted on sharepoint. This is a client
Hi I need implement context help inside my .NET application. I have .chm file
I'm trying to implement system-wide login throttling and I need to calculate the daily
I need to implement the website visitor count Usercontrol.Can any one help me regarding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.