RPC
RoadRunner provides a powerful RPC (Remote Procedure Call) interface for communication between PHP applications and the server using Goridge library.
Goridge
Goridge is a high-performance PHP-to-Golang/Golang-to-PHP library developed specifically for communication between PHP applications and RoadRunner. It is designed to provide a reliable and efficient way to communicate between the two components, allowing PHP developers to take advantage of the performance benefits of Golang-based systems while still writing their applications in PHP.
Installation
To use Goridge, you first need to install it via Composer.
Configuration
You can change the RPC port from the default (127.0.0.1:6001
) using the following configuration:
Connecting to RoadRunner
Once you have installed Goridge, you can connect to the RoadRunner server. To do so, create an instance of the Spiral\Goridge\RPC\RPC
.
Here's an example:
Or you can use the Spiral\RoadRunner\Environment
class to get the RPC address from environment variables:
The Environment::getRPCAddress()
method returns the RPC address from the RR_RPC
environment variable and can be used only inside PHP worker.
Calling RPC Methods
Once you have created $rpc
instance, you can use it to call embedded RPC services.
In the case of running workers in debug mode http: { pool.debug: true }
the number of http workers will be zero (i.e. an empty array []
will be returned).
This behavior may be changed in the future, you should not rely on this result to check that the RoadRunner was launched in development mode.
Available RPC Methods
RoadRunner provides several built-in RPC methods that you can use in your PHP applications:
rpc.Version
: Returns the RoadRunner version.rpc.Config
: Returns the RoadRunner configuration.
There are also several plugins that provide RPC methods, but not described in the documentation. You may be able to find the RPC Go definitions for these plugins in the following repositories:
Jobs - Provides a way to create and manage job pipelines and push jobs to the queue.
KV - Provides a way to store and retrieve key-value pairs.
Resetter - Provides a way to reset workers globally or separately for each plugin.
Async PHP RPC interface
You can use Spiral\Goridge\RPC\AsyncRPCInterface
and implementation with multiple relays to effectively offer non-blocking IO in regard to the Roadrunner communication.
The interface provides the following new methods:
callIgnoreResponse(string $method, mixed $payload): void
- Invoke the remote RoadRunner service method using the given payload (free form) non-blocking and ignore the response.callAsync(string $method, mixed $payload): int
- Invokes the specified method with the specified payload and returns an integer identifier that can be used to retrieve the response when it's ready.hasResponse(int $seq): bool, getResponse(int $seq, mixed $options = null): mixed
hasResponses(array $seqs): array
getResponses(array $seqs, mixed $options = null): iterable
- methods to check for and retrieve one or more results of executed requests.
The callIgnoreResponse method can be used to invoke RPC methods without waiting for a response. If you don't need a response, this can greatly improve performance. For example, consider sending metric data.
This code is greatly simplified and does not include error handling, for example. However, it demonstrates an example of using the new functionality.
The callAsync
method allows you to invoke an RPC method and obtain a request identifier, but it does not immediately request a response and does not block further execution while waiting for a response. Instead, you can save the identifier and continue executing other operations that do not require an immediate response from the service. Afterward, you can request the response using the saved identifier.
Using this method, we can implement, for example, sending data to a cache and reading the response only after sending all the necessary data. The code in the example below will be greatly simplified, and the implementation of some methods will be omitted:
Usage:
What's Next?
Writing a custom plugin - Learn how to create your own services and RPC methods.
Last updated