friendsofhyperf / support
Another support component for Hyperf.
Fund package maintenance!
huangdijia
hdj.me/sponsors
Installs: 48 091
Dependents: 8
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
pkg:composer/friendsofhyperf/support
Requires
- hyperf/collection: ~3.2.0
- hyperf/context: ~3.2.0
- hyperf/di: ~3.2.0
- hyperf/macroable: ~3.2.0
- hyperf/pipeline: ~3.2.0
- hyperf/stringable: ~3.2.0
- hyperf/support: ~3.2.0
- hyperf/tappable: ~3.2.0
- laravel/serializable-closure: ^1.0 || ^2.0
- nesbot/carbon: ^2.0 || ^3.0
- symfony/polyfill-php85: ^1.33
Suggests
- hyperf/amqp: Required to use PendingAmqpProducerMessageDispatch.(~3.2.0)
- hyperf/kafka: Required to use PendingKafkaProducerMessageDispatch.(~3.2.0)
- hyperf/queue: Required to use PendingAsyncQueueDispatch.(~3.2.0)
- vlucas/phpdotenv: Required to use Dotenv.(^5.0)
- 3.2.x-dev
- v3.2.0-beta.1
- v3.2.0-alpha.6
- v3.2.0-alpha.5
- v3.2.0-alpha.4
- v3.2.0-alpha.3
- v3.2.0-alpha.1
- dev-main / 3.1.x-dev
- v3.1.76
- v3.1.75
- v3.1.73
- v3.1.72
- v3.1.69
- v3.1.67
- v3.1.61.1
- v3.1.61
- v3.1.58
- v3.1.52
- v3.1.48
- v3.1.41
- v3.1.34
- v3.1.32
- v3.1.31
- v3.1.28.3
- v3.1.28.2
- v3.1.28
- v3.1.27
- v3.1.21
- v3.1.19
- v3.1.18
- v3.1.17
- v3.1.15
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.2
- v3.1.1
- v3.1.0
- v3.1.0-rc.21
- v3.1.0-rc.16
- v3.1.0-rc.4
- v3.1.0-rc.2
- v3.1.0-beta.20
- v3.1.0-beta.15
- v3.1.0-beta.9
- v3.1.0-beta.1
- 3.0.x-dev
- v3.0.120
- v3.0.114
- v3.0.113
- v3.0.112
- v3.0.109
- v3.0.106
- v3.0.104
- v3.0.97
- v3.0.85
- v3.0.82
- v3.0.80
- v3.0.70
- v3.0.55
- v3.0.52
- v3.0.51
- v3.0.49
- v3.0.48
- v3.0.46
- v3.0.0
This package is auto-updated.
Last update: 2025-12-12 05:37:09 UTC
README
A comprehensive support component for Hyperf providing essential utilities, helpers, and base classes.
Features
- 🎯 Fluent Dispatch API - Elegant job dispatch with support for async queue, AMQP, and Kafka
- 🔄 Closure Jobs - Execute closures as background jobs with dependency injection
- 🛠️ Helper Functions - Collection of useful helper functions
- 📦 Bus System - Pending dispatch classes for various message systems
- 🧩 Traits & Utilities - Reusable traits and utility classes
- ⏱️ Backoff Strategies - Multiple retry backoff implementations for retry mechanisms
Installation
composer require friendsofhyperf/support
Usage
Dispatch Helper Function
The dispatch() helper function provides a fluent API for dispatching jobs to different systems:
Async Queue (Closure Jobs)
use function FriendsOfHyperf\Support\dispatch; // Simple closure dispatch to async queue dispatch(function () { // Your job logic here logger()->info('Job executed!'); }); // With configuration dispatch(function () { // Your job logic here }) ->onConnection('high-priority') ->delay(60) // Execute after 60 seconds ->setMaxAttempts(5); // With dependency injection dispatch(function (UserService $userService, LoggerInterface $logger) { $users = $userService->getActiveUsers(); $logger->info('Processing ' . count($users) . ' users'); });
AMQP Producer Messages
use Hyperf\Amqp\Message\ProducerMessageInterface; use function FriendsOfHyperf\Support\dispatch; // Dispatch AMQP message dispatch($amqpMessage) ->setPool('default') ->setExchange('my.exchange') ->setRoutingKey('my.routing.key') ->setTimeout(10) ->setConfirm(true);
Kafka Producer Messages
use Hyperf\Kafka\Producer\ProduceMessage; use function FriendsOfHyperf\Support\dispatch; // Dispatch Kafka message dispatch($kafkaMessage) ->setPool('default');
CallQueuedClosure
The CallQueuedClosure class allows you to execute closures as async queue jobs:
use FriendsOfHyperf\Support\CallQueuedClosure; // Create a closure job $job = CallQueuedClosure::create(function () { // Your job logic return 'Job completed!'; }); // Configure max attempts $job->setMaxAttempts(3); // The job can be pushed to queue manually or via dispatch()
Pending Dispatch Classes
PendingAsyncQueueDispatch
Fluent API for async queue job dispatch:
use FriendsOfHyperf\Support\Bus\PendingAsyncQueueDispatch; $pending = new PendingAsyncQueueDispatch($job); $pending ->onConnection('default') ->delay(30) ->when($condition, function ($dispatch) { $dispatch->onConnection('special'); }) ->unless($otherCondition, function ($dispatch) { $dispatch->delay(60); }); // Job is dispatched when object is destroyed
PendingAmqpProducerMessageDispatch
Fluent API for AMQP message dispatch:
use FriendsOfHyperf\Support\Bus\PendingAmqpProducerMessageDispatch; $pending = new PendingAmqpProducerMessageDispatch($message); $pending ->setPool('default') ->setExchange('my.exchange') ->setRoutingKey('my.routing.key') ->setTimeout(5) ->setConfirm(true); // Message is sent when object is destroyed
PendingKafkaProducerMessageDispatch
Fluent API for Kafka message dispatch:
use FriendsOfHyperf\Support\Bus\PendingKafkaProducerMessageDispatch; $pending = new PendingKafkaProducerMessageDispatch($message); $pending->setPool('default'); // Message is sent when object is destroyed
Conditional Execution
All pending dispatch classes support conditional execution:
use function FriendsOfHyperf\Support\dispatch; dispatch($job) ->when($shouldUseHighPriority, function ($dispatch) { $dispatch->onConnection('high-priority'); }) ->unless($isTestMode, function ($dispatch) { $dispatch->delay(10); });
API Reference
dispatch($job)
Creates a pending dispatch instance based on the job type:
Closure→PendingAsyncQueueDispatchwithCallQueuedClosureProducerMessageInterface→PendingAmqpProducerMessageDispatchProduceMessage→PendingKafkaProducerMessageDispatch- Other objects →
PendingAsyncQueueDispatch
PendingAsyncQueueDispatch Methods
onConnection(string $connection): static- Set queue connectiondelay(int $delay): static- Delay job execution (seconds)setMaxAttempts(int $attempts): static- Set max retry attemptswhen(mixed $condition, callable $callback): static- Conditional executionunless(mixed $condition, callable $callback): static- Inverse conditional execution
PendingAmqpProducerMessageDispatch Methods
setPool(string $pool): static- Set AMQP pool namesetExchange(string $exchange): static- Set exchange namesetRoutingKey(array|string $routingKey): static- Set routing key(s)setTimeout(int $timeout): static- Set timeout (seconds)setConfirm(bool $confirm): static- Enable/disable confirm modewhen(mixed $condition, callable $callback): static- Conditional executionunless(mixed $condition, callable $callback): static- Inverse conditional execution
PendingKafkaProducerMessageDispatch Methods
setPool(string $pool): static- Set Kafka pool namewhen(mixed $condition, callable $callback): static- Conditional executionunless(mixed $condition, callable $callback): static- Inverse conditional execution
CallQueuedClosure
create(Closure $closure): static- Create a new closure jobsetMaxAttempts(int $attempts): void- Set max retry attemptshandle(): mixed- Execute the closure (called by queue worker)
Backoff Strategies
The component provides various backoff strategies for retry mechanisms:
ArrayBackoff
Use custom delay intervals defined in an array:
use FriendsOfHyperf\Support\Backoff\ArrayBackoff; // Custom delays $backoff = new ArrayBackoff([100, 500, 1000, 2000, 5000]); // Stop after array is exhausted (returns 0) $backoff = new ArrayBackoff([100, 500, 1000], false); // From comma-separated string $backoff = ArrayBackoff::fromString('100, 500, 1000, 2000'); // From predefined patterns $backoff = ArrayBackoff::fromPattern('short'); // [100, 200, 300, 500, 1000] $backoff = ArrayBackoff::fromPattern('medium'); // [200, 500, 1000, 2000, 5000] $backoff = ArrayBackoff::fromPattern('long'); // [500, 1000, 2000, 5000, 10000, 30000] $backoff = ArrayBackoff::fromPattern('exponential'); // [100, 200, 400, 800, 1600, 3200, 6400] // Usage in retry logic $attempt = 0; while (true) { try { return performOperation(); } catch (Exception $e) { $delay = $backoff->next(); if ($delay === 0) { throw $e; // No more retries } usleep($delay * 1000); // Convert to microseconds } }
Available Backoff Implementations
- ArrayBackoff - Custom intervals from an array
- FixedBackoff - Constant delay between retries
- LinearBackoff - Linear growth with configurable step
- ExponentialBackoff - Exponential growth with optional jitter
- FibonacciBackoff - Fibonacci sequence-based delays
- PoissonBackoff - Statistical distribution-based delays
- DecorrelatedJitterBackoff - Decorrelated jitter for better spreading
Common Backoff Interface
All backoff implementations implement BackoffInterface:
interface BackoffInterface { public function next(): int; // Get next delay in milliseconds public function reset(): void; // Reset attempt counter public function getAttempt(): int; // Get current attempt count public function sleep(): int; // Sleep for calculated delay }