I am in the process of designing a feature for a system where I strongly feel that there must be a pattern for this out there, that I should know about before diving into the code.
The scenario is this:
- I have a pool of resources of which I have a limited number.
- I have a variable number of consumers that use those resources; each consumer needs exactly one resource, and it may not use the same resource as any other consumer at a given time.
- The consumers are divided into a fixed number of groups, and the system needs to guarantee that there is a minimum of one resource for each group.
- The number of consumers in each group varies over time; they are allocated and deallocated as needed.
My current approach is to put the resources into two stacks at startup: one "emergency stack" and one "common stack". The emergency stack will contain the same number of resources as there are groups (so, one for each group). The rest of the available resources go into the common stack.
When a new consumer is about to be created, the system will request a resource. If there are resources available in the common stack, one will be popped from it and returned to the caller. If the common stack is empty, a resource may be popped from the emergency stack instead, but only if there are no consumers within the same group that already has an emergency resource.
Whenever a consumer within a group can be deallocated, the associated resource will be returned, and pushed onto one of the resource stacks. The code responsible for deallocating consumers will make sure to always return any emergency resources first, so that the emergency stack is filled, before returned resources are being pushed onto the common stack.
I feel that this is the sort of problem for which there ought to exist a design pattern that has been tested and proven to work well, so I call out to the community: do you know of such a pattern? If so, I kindly ask you to enlighten me.
Update
The solution is now implemented, using bits and pieces from various answers to this question. I published a blog post about it.