Aller au contenu principal

PHPProcessManager

A PHP Process manager.

Maintains:

  • A single "primary" PHP instance that's never killed – it contains the reference filesystem used by all other PHP instances.
  • A pool of disposable PHP instances that are spawned to handle a single request and reaped immediately after.

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.

Implements

  • AsyncDisposable

Index

Constructors

constructor

Methods

[asyncDispose]

  • [asyncDispose](): Promise<void>
  • Returns Promise<void>

acquirePHPInstance

  • acquirePHPInstance(considerPrimary?: { considerPrimary?: boolean }): Promise<SpawnedPHP>
  • 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.

    @throws

    when the maximum number of PHP instances is reached and the waiting timeout is exceeded.


    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

  • getPrimaryPhp(): Promise<PHP>
  • Get the primary PHP instance.

    If the primary PHP instance is not set, it will be spawned using the provided phpFactory.

    @throws

    when called twice before the first call is resolved.


    Returns Promise<PHP>