recca0120/laravel-parallel
Composer 安装命令:
composer require recca0120/laravel-parallel
包简介
README 文档
README
Requirements
- Laravel versions 5.7, 6.x, 7.x, 8.x, 9.x, 10.x and 11.x
- PHP 7.0 or greater
Installation
Install the package with composer:
composer require recca0120/laravel-parallel
Usage
- make a product migration
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('name'); $table->integer('quantity')->default(0); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } }
- define product model
App\Models\Product
<?php namespace App\Models; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; /** * Class Product. * @property int id * @property string name * @property int quantity * @package App\Models * @mixin Builder */ class Product extends Model { use HasFactory; protected $fillable = ['name', 'quantity']; protected $casts = ['quantity' => 'int']; }
- define router in
routes/web.php
<?php use App\Models\Product; use Illuminate\Support\Facades\Route; Route::get('/product/{productId}', function ($productId) { return Product::findOrFail($productId); }); Route::post('/product/{productId}', function ($productId) { $product = Product::findOrFail($productId); if ($product->quantity > 0) { // wrong, it will make test fail // $product->fill(['quantity' => $product->quantity - 1])->save(); // correct $product->where('id', $product->id) ->where('quantity', '>', 0) ->update(['quantity' => DB::raw('quantity - 1')]); } return $product->fresh(); });
- testing
<?php namespace Tests\Feature; use App\Models\Product; use Illuminate\Foundation\Testing\DatabaseMigrations; use Recca0120\LaravelParallel\Tests\Concerns\WithParallelPhyiscalDatabase; use Recca0120\LaravelParallel\Tests\ParallelRequest; use Tests\TestCase; class RaceConditionTest extends TestCase { use DatabaseMigrations; use WithParallelPhyiscalDatabase; private $product; private $quantity = 10; public function test_race_condition() { $this->useParallelPhyiscalDatabase(); $this->product = Product::create(['name' => 'test', 'quantity' => $this->quantity]); $request = $this->app->make(ParallelRequest::class); $promises = collect(); for ($i = 0; $i < $this->quantity; $i++) { // you will get \GuzzleHttp\Promise\PromiseInterface $promise = $request->post('/product/'.$this->product->id); $promises->add($promise); } // you need wait response $promises->map->wait()->each->assertOk(); $this->get('/product/'.$this->product->id) ->assertOk() ->assertJsonPath('quantity', 0); } public function test_multiple_times_to_test_race_condition() { $this->useParallelPhyiscalDatabase(); $this->product = Product::create(['name' => 'test', 'quantity' => $this->quantity]); $request = $this->app->make(ParallelRequest::class); $promises = collect($request->times(10)->post('/product/'.$this->product->id)); // you need wait response $promises->map->wait()->each->assertOk(); $this->get('/product/'.$this->product->id) ->assertOk() ->assertJsonPath('quantity', 0); } }
License
The MIT License (MIT). Please see License File for more information.
recca0120/laravel-parallel 适用场景与选型建议
recca0120/laravel-parallel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 126.98k 次下载、GitHub Stars 达 64, 最近一次更新时间为 2022 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「parallel」 「parallel request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 recca0120/laravel-parallel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 recca0120/laravel-parallel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 recca0120/laravel-parallel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Sequential way to run Symfony Processes in parallel
HTTP request logger middleware for Laravel
Generic PHP Threads library using only pure PHP
Asynchronous parallel Twitter client built on the Amp concurrency framework
Adds request-parameter validation to the SLIM 3.x PHP framework
Execute PHP code in parallel with single or persistent task worker.
统计信息
- 总下载量: 126.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 64
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-19
