Remove "count" from the Queue interface
Options:
-
Remove
count()method from queues -
Leave
count()and addCountableinterface:
class InMemoryQueue implements Queue, \Countable
{
...
public function count()
{
...
}
}
- Put
count()into separate interface:
interface AdvancedQueue extends Queue, \Countable
{
public function slice($offset, $limit);
}
... why?
There are several reasons that come to mind:
- I don't have a use case where I need
count()to be in theQueueinterface; - Removing it will simplify decorating a queue;
- Some backends don't support
counteither.
Do you use Queue::count()? If so, could you please describe your use case?
Yes - scaling workers.
If the queue size > (current workers * 10) = scale up If the queue size < (current workers) = scale down
Will the option 2) work for you? Roughly speaking, to scale workers all you need to know is the queue size, so \Countable should be enough:
// WorkerManager
public function maybeScale(\Countable $pool)
{
$count = count($pool);
if ($count > $numberOfWorkers * 10) {
// create more workers
} else if ($count < $numberOfWorkers) {
// destroy workers
}
}
And your workers will process the Queue instances:
// Worker
public function process(Queue $queue)
{
$item = $queue->pop();
// process the item
}
Yes that would work. :-)