Throttle the number of requests through WCF services

I have a WCF service load balanced on three different machines.

Say these services can handle requests of types A , B, and C. There are no restrictions on handling A or B. However, we can only process 5 requests of type C at a time, so if the 6th request of type C appears, he will have to wait until one of the previous requests completes.

How can I make sure that only 5 requests of type C are processed on all three machines?

+7
c # wcf windows-services
source share
1 answer

It sounds like you need a semaphore with several machines, you can implement it with a distributed caching solution like memcached. Alternatively, you can use another WCF service on the same machine, which manages the semaphore for your load-balanced services.

Thus, a new service might look something like this:

[ServiceContract] public interface ISemaphorService { [OperationContract] void Acquire(); [OperationContract] void Release(); } [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class SemaphoreService { private readonly static Semaphore Pool = new Semaphore(5, 5); public void Acquire() { Pool.WaitOne(); } public void Release() { Pool.Release(); } } 

In a real-world application, you might want the number of semaphores to be configured in a config or something else, and put in a few timeouts and enable a mechanism to ensure timely release of semaphores and / or when a client fails:

 // on the client side (service C) var client = new SemaphoreServiceClient(); try { // acquire the semaphore before processing the request client.Acquire(); // process request ... } finally { // always remember the release the semaphore client.Release(); } 
+8
source share

All Articles