Download the PHP package hollodotme/fast-cgi-proxy without Composer
On this page you can find all versions of the php package hollodotme/fast-cgi-proxy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hollodotme/fast-cgi-proxy
More information about hollodotme/fast-cgi-proxy
Files in hollodotme/fast-cgi-proxy
Package fast-cgi-proxy
Short Description A proxy for distributing (a)sync requests to multiple php-fpm sockets/pools.
License MIT
Informations about the package fast-cgi-proxy
FastCGI Proxy
Description
A proxy for distributing (a)sync requests to multiple fastCGI servers.
Installation
Usage
Request distribution
The proxy can distribute requests to multiple fastCGI servers in the following ways:
- Randomly
- Via round robin
Random distribution
To set up random distribution use the following example code:
When sending requests now the proxy will randomly choose one of the fastCGI servers to process the request.
Round robin distribution
To set up round robin distribution use the following example code:
The proxy will send your requests to the next fastCGI server in the same order they were added to the RoundRobin
instance.
In this example it will send to:
127.0.0.1:9001
10.100.10.42:9001
,/var/run/php7.1-fpm.sock
127.0.0.1:9001
(start from the beginning again)- and so on...
Sending requests
The Proxy
class has the same methods as the underlying Client class for sending (a)sync requests and retrieving responses (reactively).
So please consult the documentation of hollodotme/fast-cgi-client for further information.
Here is just a short list of available methods:
-
$proxy->sendRequest(ProvidesRequestData $request) : ProvidesResponseData
Sends a synchronous request and returns the response. (blocking) -
$proxy->sendAsyncRequest(ProvidesRequestData $request) : int
Sends an asynchronous request and returns the request ID. (non-blocking) -
$proxy->readResponse(int $requestId, ?int $timeoutMs = null) : ProvidesResponseData
Reads and returns the response of a previously obtained request ID.
(blocking until response was read or read timed out) -
$proxy->readResponses(?int $timeoutMs = null, int ...$requestIds) : \Generator|ProvidesResponseData[]
Reads and yields the responses of previously obtained request IDs in the order of the given request IDs.
(blocking until all responses were read or read timed out) -
$proxy->readReadyResponses(?int $timeoutMs = null) : \Generator|ProvidesResponseData[]
Reads and yields the responses of all finished requests.
(non-blocking, meant to be used in a loop) -
$proxy->waitForResponse(int $requestId, ?int $timeoutMs = null) : void
Waits for the response of a previously obtained request ID and calls the request's response callback.
(blocking until response was read or read timed out) -
$proxy->waitForResponses(?int $timeoutMs = null) : void
Waits for the responses of the previously obtained request IDs in the order of finished requests and calls the corresponding response callbacks.
(blocking until all responses were read or read timed out) -
$proxy->hasResponse(int $requestId) : bool
Returns whether the given request ID has a response or not. (non-blocking) -
$proxy->handleResponse(int $requestId, ?int $timeoutMs = null) : void
Calls the corresponding response callback of an already finished request.
(If request ID has a response must be checked before calling this method, see$proxy->hasResponse(int $requestId)
). -
$proxy->hasUnhandledResponses() : bool
Returns TRUE if there are unhandles responses left, otherwise FALSE. -
$proxy->getRequestIdsHavingResponse() : array
Returns all request IDs that have responses. (non-blocking) -
$proxy->handleResponses(?int $timeoutMs = null, int ...$requestIds) : void
Calls the corresponding response callbacks of already finished requests in the order of the given request Ids.
(If request IDs have a response must be checked before calling this method, see$proxy->hasResponse(int $requestId)
or$proxy->getRequestIdsHavingResponse() : array
.) $proxy->handleReadyResponses(?int $timeoutMs = null) : void
Calls the corresponding response callbacks in the order of finished requests.
(non-blocking, short for$proxy->handleResponses($timeoutMs, int ...$proxy->getRequestIdsHavingResponse())
)
Cluster requests
This feature is available since v0.2.0
of this library.
In order to process a single request on a multitude of fastCGI servers, the ClusterProxy
class was introduced.
So in order to distribute the request to one of the configured fastCGI servers, the cluster proxy will send the same
request to ALL configured fastCGI servers and allows you to read/handle their responses (reactively).
As per concept of cluster requests, there is always a one-to-many relation for request & responses.
That's why the ClusterProxy
class does not offer synchronous requests and reading of single responses based on a request ID.
To set up a cluster proxy, use the following example code:
The following reduced set of methods to send requests and handle responses are available in the cluster proxy class:
-
$clusterProxy->sendAsyncRequest(ProvidesRequestData $request) : void
Sends an asynchronous request to all connections in the cluster. (non-blocking) -
$clusterProxy->readReadyResponses(?int $timeoutMs = null) : \Generator|ProvidesResponseData[]
Reads and yields the responses of all finished requests.
(non-blocking, meant to be used in a loop) -
$clusterProxy->waitForResponses(?int $timeoutMs = null) : void
Waits for the responses of the previously obtained request IDs in the order of finished requests and calls the corresponding response callbacks.
(blocking until all responses were read or read timed out) -
$clusterProxy->hasUnhandledResponses() : bool
Returns TRUE if there are unhandles responses left, otherwise FALSE. $clusterProxy->handleReadyResponses(?int $timeoutMs = null) : void
Calls the corresponding response callbacks in the order of finished requests.
(non-blocking, meant to be used in a loop in combination with$clusterProxy->hasUnhandledResponses()
)
Cluster status
This feature is available since v0.2.0
of this library.
In order to retrieve the status of all fastCGI servers in a cluster, the method ClusterProxy#getStatus()
was introduced.
Currently this method solely supports status response implementation for PHP-FPM,
but can easily be extended for other fastCGI servers by implementing the interface
hollodotme\FastCGI\Interfaces\ProvidesServerStatus
.
Cluster status example
The following code reads the status of all 3 php-fpm containers that are part of the docker-compose setup of this library.
Please note: If the status endpoint is not enabled in the server's config (pm.status_path
for PHP-FPM),
the ClusterProxy#getStatus()
method will throw a RuntimeException
.
examples/cluster_status.php
This script produces for example the following output
Contributing
Contributions are welcome and will be fully credited. Please see the contribution guide for details.