Freigeben über


Flow Throttles

When programming using channels, the common usage pattern for the server is to sit in a loop accepting channels. This loop, sometimes called a pump, cycles through each time accepting a channel and initiating some work. Let's look at the most basic channel pump possible. I'm going to write things with pseudocode to make it even simpler.

 ChannelPump
BEGIN LOOP
  AcceptChannel
   DoWork
END LOOP

We'll assume that DoWork goes off in the background to perform some asynchronous processing and will take care of cleaning up resources. There's nothing we could cut out of this pump. There are many things that we'd have to add though to make the pump survive more than a few iterations without blowing up.

One problem with the pump is that we spin through it as fast as possible. The pump will continue kicking off additional work even if the system can't withstand the load. We need some way to throttle the flow of data.

A simple throttling mechanism is to limit the number of channels that we can have working at once. Let's add a semaphore to count the number of active channels and prevent DoWork from being called if we're already using all of the available work throttles. We'll need to change ChannelPump and also put some details into DoWork.

 ChannelPump
BEGIN LOOP
 AcceptChannel
   AcquireThrottle
 DoWork
END LOOP
 DoWork
MessagePump
CloseChannel
ReleaseThrottle

There are some new constructs here but the only one that needs explanation is MessagePump. MessagePump is what actually receives messages from the channel and processes them. All of DoWork runs asynchronously though so ChannelPump will continue running while DoWork is taking place.

Unfortunately, ChannelPump will block when AcquireThrottle isn't able to acquire the semaphore immediately. This does what we want because DoWork won't get called but it's less than ideal that we sit there waiting. Next time, we'll look at how to change ChannelPump to solve the blocking problem.

Next time: Unblocking Flow Throttles

Comments

  • Anonymous
    October 10, 2007
    Last time, we were looking at how to control flow through a channel pump by introducing the concept of

  • Anonymous
    October 10, 2007
    How do I use % Feature X% from ASP.NET in a WCF service? ASP.NET has an HTTP-centric application model