0

I have developed a restful WEB API 2 (C#, .NET 4.7) for an internal integration project. In my API's backend, I need to use a vendor's dll in order to process the provided input.

The vendor's dll is written in C, and it has a limitation that, there cannot be more than 8 initialized instances at the same time.

Since my REST API can be called multiple times concurrently, there is a risk that I may hit the 8 instances limit during a busy time. (My API is not public. There is a little chance that this can happen. But still, I need to stay safe.)

To prevent this, I was planning to keep the current count of the running instances on a thread-safe singleton variable. And if I can maintain the increment/decrement logic where I initialize the C dll, I can stay safe.

Since a REST API is stateless, is it a good practice to use a static thread-safe approach to implement this? Or there could be better approaches like using server cache, Application State ( Application["VariableName"] = "Value" ), or even a flat file.

And I need to add that, a database is not an option at my situation.

  • Putting a restriction on the number of concurrent calls to your API does not magically make the API stateful, just like using a database for persistent storage doesn't make a REST API stateful. – Bart van Ingen Schenau Jun 19 '18 at 9:12
1

What you really need is an Object Pool with a pool size of 8. It's reasonably easy to implement an object pool using the BufferBlock class.

| improve this answer | |
1

You should wrap initialization and subsequent calls to the vendors lib with a Semaphore to avoid too many instances. Any request coming in when the specified limit has been reached will automatically be queued.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.