configured/race
Composer 安装命令:
composer require configured/race
包简介
Race for Laravel: run multiple attempts concurrently and return the first valid result.
README 文档
README
Run multiple forked attempts concurrently and return the first valid result.
Race for Laravel requires PHP 8.3+, PCNTL, POSIX, and socket support. It does
not use queues, so times: 5 means five child processes are forked
immediately from the current PHP process.
Installation
composer require configured/race:^0.1.0
Publish the config file if you want to customize timing behavior:
php artisan vendor:publish --tag=race-config
return [ 'poll_interval_ms' => 10, 'execution_buffer_seconds' => 5, ];
execution_buffer_seconds is added to the race timeout when Race temporarily
raises PHP's max_execution_time. For example, timeout(60) with the default
buffer sets the process execution limit to 65 seconds while the race runs, then
restores the previous limit.
Usage
$result = race( times: 3, timeout: 20, callback: fn () => Ai::agent()->prompt($prompt, timeout: 15), valid: fn ($result) => filled((string) $result), map: fn ($result) => (string) $result, );
use Configured\Race\Exceptions\RaceFailedException; $result = Race::times(3) ->timeout(20) ->valid(fn ($result) => filled($result)) ->map(fn ($result) => (string) $result) ->failed(function (RaceFailedException $failure): string { logger()->warning('All race attempts failed.', [ 'attempts' => $failure->attempts(), 'timed_out' => $failure->timedOut(), 'failures' => $failure->failures(), 'invalid_results' => $failure->invalidResults(), ]); return 'fallback'; }) ->run(fn () => Ai::agent()->prompt($prompt, timeout: 15));
The first attempt that passes the valid callback wins. Remaining child
processes are terminated and ignored.
The failed callback runs only when the whole race fails. It receives a
RaceFailedException with the number of attempts, failed attempt details,
invalid results, and whether the race timed out. If the callback returns a
value, that value is returned as the race result.
Available failure details:
$failure->attempts(); // Total attempts considered by the race. $failure->timedOut(); // True when child processes were still running at timeout. $failure->failures(); // Thrown attempt errors: attempt, class, message. $failure->invalidResults(); // Results that completed but failed your valid callback.
统计信息
- 总下载量: 464
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-05