Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to specify a new queue instance #27

Merged
merged 3 commits into from
Sep 27, 2016

Conversation

joshdifabio
Copy link
Contributor

@joshdifabio joshdifabio commented May 6, 2016

In order to enable efficient integrations with reactors, it would be very helpful to be able to provide a custom task queue.

Here's a simple example of what would be possible after merging this PR:

use Amp\Reactor;
use GuzzleHttp\Promise\TaskQueue;

class AmpTaskQueue extends TaskQueue
{
    private $reactor;

    public function __construct(Reactor $reactor)
    {
        $this->reactor = $reactor;
    }

    public function add(callable $task)
    {
        if ($this->isEmpty()) {
            $this->reactor->immediately([$this, 'run']);
        }

        parent::add($task);
    }
}

\GuzzleHttp\Promise\queue(new AmpTaskQueue($reactor));

Simply calling $reactor->immedately($task); would really be preferable but, unfortunately, this would prevent the run() and isEmpty() methods from working as expected.

@joshdifabio
Copy link
Contributor Author

Note that this is the approach react/promise is likely to take when they introduce a task queue in v3.

https://github.com/reactphp/promise/blob/master/src/functions.php#L203

@joshdifabio
Copy link
Contributor Author

Any thoughts on this @mtdowling? It would be great for optimisation where the curl handler isn't being used.

@mtdowling
Copy link
Member

This should be ok considering it would be an application concern to modify this. Do you think it would be better to introduce an interface here?

@joshdifabio
Copy link
Contributor Author

Do you think it would be better to introduce an interface here?

Probably, yes. Would you like me to add one?

@mtdowling
Copy link
Member

That would be great!

@joshdifabio
Copy link
Contributor Author

joshdifabio commented May 25, 2016

I've added an interface. The only question in my mind was whether disableShutdown() should be included in the interface, but for BC I think it probably should.

@joshdifabio
Copy link
Contributor Author

What are your thoughts on this @mtdowling? We've been using this in production for a while and it's been working well for us. Referencing a fork branch in our Composer configs is getting pretty tricky however.

*
* Note: This shutdown will occur before any destructors are triggered.
*/
public function disableShutdown();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should just be an implementation detail of the default TaskQueue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same thought. However, would this not be a breaking change? (E.g. queue()->disableShutdown() would potentially no longer work.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think anyone calling that method has already coupled themselves to a specific implementation detail though... I would be in favor of removing this from the interface but keeping it on the TaskQueue class.

Copy link
Contributor Author

@joshdifabio joshdifabio Sep 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about merging this if I update the PR to remove disableShutdown() from TaskQueueInterface? Requiring this forked version in our code base is getting more and more tricky.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Removing this from the interface is the right direction in my opinion. Will be merged after.

@joshdifabio
Copy link
Contributor Author

@mtdowling I've removed disableShutdown() from TaskQueueInterface as requested.

P.S. Thanks for your responsiveness here and in general, I really appreciate it.

@mtdowling mtdowling merged commit 338d7e4 into guzzle:master Sep 27, 2016
@mtdowling
Copy link
Member

Thanks! This looks great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants