WorkerPool
in package
Tags
Table of Contents
Methods
- actor() : self
- Register a class-based actor (implements ActorHandler).
- behavior() : self
- Register a behavior-based actor via a factory closure.
- configure() : self
- Full control: receive the WorkerNode directly to spawn actors, wire dependencies, etc. The closure MUST be static.
- onStart() : self
- Register a main-thread callback to run after all workers are ready.
- run() : void
- Boot the worker pool. Blocks until the pool exits.
- stateful() : self
- Register a stateful class-based actor (implements StatefulActorHandler).
- withCpuThreads() : self
- Create a pool with one thread per CPU core (swoole_cpu_num()).
- withLogger() : self
- Inject a PSR-3 logger by class name. Each thread calls new $loggerClass().
- withLoggerFactory() : self
- Inject a PSR-3 logger via factory closure. Each thread deserializes and calls the factory to create its own logger instance.
- withName() : self
- Set the actor system name prefix. Each worker system is named "{prefix}-{workerId}".
- withThreads() : self
- Create a pool with an explicit thread count.
Methods
actor()
Register a class-based actor (implements ActorHandler).
public
actor(string $name, ActorHandler> $actorClass[, MailboxConfig|null $mailbox = null ]) : self
The class is instantiated fresh in each thread via Props::fromFactory.
Parameters
- $name : string
- $actorClass : ActorHandler>
- $mailbox : MailboxConfig|null = null
Return values
selfbehavior()
Register a behavior-based actor via a factory closure.
public
behavior(string $name, callable(): Behavior<string|int, T> $factory[, MailboxConfig|null $mailbox = null ]) : self
The factory MUST be static. Example: ->behavior('pings', static fn (): Behavior => Behavior::receive( static fn (ActorContext $ctx, object $msg): Behavior => Behavior::same() ))
Parameters
- $name : string
- $factory : callable(): Behavior<string|int, T>
- $mailbox : MailboxConfig|null = null
Tags
Return values
selfconfigure()
Full control: receive the WorkerNode directly to spawn actors, wire dependencies, etc. The closure MUST be static.
public
configure(callable(WorkerNode): void $setup) : self
Parameters
- $setup : callable(WorkerNode): void
Return values
selfonStart()
Register a main-thread callback to run after all workers are ready.
public
onStart(callable(WorkerPoolHandle): void $callback) : self
The closure receives a WorkerPoolHandle for message injection and stopping.
IMPORTANT: This callback runs in the main thread (not serialized — no opis needed). The closure does NOT need to be static and can capture any values. Call $handle->stop() when done, or simply return (stop is called automatically).
Parameters
- $callback : callable(WorkerPoolHandle): void
Return values
selfrun()
Boot the worker pool. Blocks until the pool exits.
public
run() : void
stateful()
Register a stateful class-based actor (implements StatefulActorHandler).
public
stateful(string $name, StatefulActorHandler> $actorClass[, MailboxConfig|null $mailbox = null ]) : self
Parameters
- $name : string
- $actorClass : StatefulActorHandler>
- $mailbox : MailboxConfig|null = null
Return values
selfwithCpuThreads()
Create a pool with one thread per CPU core (swoole_cpu_num()).
public
static withCpuThreads() : self
Return values
selfwithLogger()
Inject a PSR-3 logger by class name. Each thread calls new $loggerClass().
public
withLogger(LoggerInterface> $loggerClass) : self
The class must have a no-argument constructor.
Parameters
- $loggerClass : LoggerInterface>
Return values
selfwithLoggerFactory()
Inject a PSR-3 logger via factory closure. Each thread deserializes and calls the factory to create its own logger instance.
public
withLoggerFactory(callable(): LoggerInterface $factory) : self
The closure MUST be static and capture only serializable values.
Parameters
- $factory : callable(): LoggerInterface
Return values
selfwithName()
Set the actor system name prefix. Each worker system is named "{prefix}-{workerId}".
public
withName(string $name) : self
Default: "worker" → "worker-0", "worker-1", …
Parameters
- $name : string
Return values
selfwithThreads()
Create a pool with an explicit thread count.
public
static withThreads(int $count) : self
Parameters
- $count : int