amphp/pipeline
Fiber-safe concurrent iterators and collection operators for AMPHP. Build pipelines from iterables, map/filter/merge, and consume results from multiple fibers safely using ConcurrentIterator (foreach or manual continue/getValue/getPosition). Requires PHP 8.1+.
amphp/pipeline is a natural fit for Laravel applications leveraging AMPHP (e.g., amphp/amp, amphp/http-client) or those requiring fiber-based concurrency (e.g., high-throughput APIs, real-time processing). It aligns with Laravel’s growing support for Swoole and RoadRunner (which use fibers).map/filter/reduce) akin to Laravel’s collection methods, but with asynchronous guarantees. Ideal for:
spatie/laravel-amp) or Swoole (via laravel/swoole).Illuminate\Queue\Worker loops with fiber-based Queue consumers for non-blocking processing.symfony/event-dispatcher + AMPHP) for async listeners.Collection::pipe() with Pipeline::fromIterable() for async operations.Pipeline::concurrent() to parallelize ShouldQueue jobs.DB::cursor() or Eloquent batches via Pipeline.| Risk Area | Mitigation Strategy |
|---|---|
| Fiber Adoption | Laravel’s core does not natively support fibers; requires Swoole/RoadRunner. |
| Blocking Legacy Code | Wrap synchronous code in Amp\async() or use Pipeline::buffer() to avoid deadlocks. |
| State Management | ConcurrentIterator must be properly disposed (e.g., in finally blocks) to avoid memory leaks. |
| Debugging Complexity | Use tap() for logging/debugging in pipelines; AMPHP’s Amp\Loop::run() helps isolate issues. |
| Vendor Lock-in | AMPHP ecosystem is small; prefer interfaces (ConcurrentIterator) over concrete classes. |
Pipeline interact with Laravel’s synchronous services (e.g., Eloquent, Cache)?DisposedException) map to Laravel’s exception handlers?Pipeline::error() integrate with Laravel’s App\Exceptions\Handler?concurrent() batch size for I/O-bound vs. CPU-bound tasks?buffer() affect memory usage in long-running pipelines?ConcurrentIterator in PHPUnit (e.g., for unit tests)?Queue + delay()).Queue::complete() on SIGTERM)?| Laravel Component | Integration Strategy |
|---|---|
| Queue Workers | Replace Illuminate\Queue\Worker with a fiber-based Queue consumer. Example: |
| ```php | |
| Amp\Loop::run(function () { | |
| $queue = new Queue(); | |
| async(function () use ($queue) { | |
| while ($job = dispatch()->get()) { | |
| $queue->push($job->handle()); | |
| } | |
| $queue->complete(); | |
| }); | |
| foreach ($queue->iterate() as $result) { | |
| Log::info("Processed: {$result}"); | |
| } | |
| }); | |
| ``` | |
| HTTP Requests | Parallelize Http::async() calls using Pipeline::concurrent(). |
| Database | Stream Eloquent batches or DB::cursor() results through Pipeline::fromIterable(). |
| Events | Replace Event::dispatch() with Pipeline::merge() for async event aggregation. |
| Commands | Use Pipeline in Artisan commands for async CLI tasks (e.g., imports). |
| Service Providers | Bind Pipeline to Laravel’s container for dependency injection. |
Artisan::call('queue:work')) with a Pipeline-based fiber consumer.foreach loop over a collection to Pipeline::fromIterable().Queue class).Pipeline methods to Laravel’s Collection facade (via traits or macros).Pipeline.Process facade with Amp\Process for async subprocesses.Amp\async() for synchronous code paths to avoid blocking fibers.spatie/laravel-amp for AMPHP.APP_RUN_IN_CONSOLE or SERVER_SOFTWARE checks.database, redis, or beanstalkd queues via Queue consumers.guzzlehttp/guzzle via Amp\Http\Client.Pipeline::merge() to combine multiple event sources.Model::cursor() → Pipeline::fromIterable().DB::statement() with generators for async SQL.file_get_contents() → Amp\File\read()).Pipeline for data processing.Pipeline usage.foreach/yield loops.Pipeline::error().ConcurrentIterator disposal must be explicit (risk of leaks).Queue back-pressure metrics (e.g., `pushHow can I help you explore Laravel packages today?