PHPProcessManager
Implements
- AsyncDisposable
Index
Constructors
Methods
Constructors
constructor
Parameters
optionaloptions: ProcessManagerOptions
Returns PHPProcessManager
Methods
[asyncDispose]
Returns Promise<void>
acquirePHPInstance
Get a PHP instance.
It could be either the primary PHP instance, an idle disposable PHP instance, or a newly spawned PHP instance – depending on the resource availability.
Parameters
considerPrimary: { considerPrimary?: boolean } = {}
Whether to consider the primary PHP instance. It matters because PHP.cli() sets the SAPI to CLI and kills the entire process after it finishes running, making the primary PHP instance non-reusable for subsequent .run() calls. This is fine for one-off child PHP instances, but not for the primary PHP that's meant to continue working for the entire duration of the ProcessManager lifetime. Therefore, we don't consider the primary PHP instance by default unless the caller explicitly requests it.
optionalconsiderPrimary: boolean = true
Returns Promise<SpawnedPHP>
getPrimaryPhp
Get the primary PHP instance.
If the primary PHP instance is not set, it will be spawned using the provided phpFactory.
Returns Promise<PHP>
A PHP Process manager.
Maintains:
When a new request comes in, PHPProcessManager yields the idle instance to handle it, and immediately starts initializing a new idle instance. In other words, for n concurrent requests, there are at most n+1 PHP instances running at the same time.
A slight nuance is that the first idle instance is not initialized until the first concurrent request comes in. This is because many use-cases won't involve parallel requests and, for those, we can avoid eagerly spinning up a second PHP instance.
This strategy is inspired by Cowboy, an Erlang HTTP server. Handling a single extra request can happen immediately, while handling multiple extra requests requires extra time to spin up a few PHP instances. This is a more resource-friendly tradeoff than keeping 5 idle instances at all times.