phive-queue icon indicating copy to clipboard operation
phive-queue copied to clipboard

Remove "count" from the Queue interface

Open rybakit opened this issue 11 years ago • 5 comments

Options:

  1. Remove count() method from queues

  2. Leave count() and add Countable interface:

class InMemoryQueue implements Queue, \Countable
{
    ...

    public function count()
    {
        ...
    }
}
  1. Put count() into separate interface:
interface AdvancedQueue extends Queue, \Countable
{
    public function slice($offset, $limit);
}

rybakit avatar Oct 16 '14 09:10 rybakit

... why?

auro1 avatar Nov 24 '15 23:11 auro1

There are several reasons that come to mind:

  • I don't have a use case where I need count() to be in the Queue interface;
  • Removing it will simplify decorating a queue;
  • Some backends don't support count either.

Do you use Queue::count()? If so, could you please describe your use case?

rybakit avatar Nov 25 '15 07:11 rybakit

Yes - scaling workers.

If the queue size > (current workers * 10) = scale up If the queue size < (current workers) = scale down

auro1 avatar Nov 26 '15 13:11 auro1

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
}

rybakit avatar Nov 26 '15 23:11 rybakit

Yes that would work. :-)

auro1 avatar Dec 02 '15 11:12 auro1