RPC
RPC is a core component, and micro services built inside PSLX (for example instant messaging and email) are concrete implementations of RPC. To build an RPC service using PSLX, one first needs to take a look at the abstract class implementation defined in rpc_base. The following functions are provided
__init__(service_name, rpc_storage)
- Description: Create an RPC instance with given name and underlying storage.
- Arguments:
- service_name: the name of the rpc service. Needs to be unique.
- rpc_storage: the partitioner storage for storing the
GenericRPCRequestResponsePair
. If None, theGenericRPCRequestResponsePair
won't be stored.
Info
Function needs to be implemented by specific application.
get_response_and_status_impl(request)
- Description: get corresponding response and status from the request.
- Arguments:
- request: a proto message containing user defined request information.
- Explanation:
- The request and response can be any proto message type user defines.
- A concrete implementation of
RPCBase
needs to specify the type of request by settingREQUEST_MESSAGE_TYPE
as a class level variable.
In addition to inheriting and implementing a RPC class, one also needs to create a client class inheriting client_base.
The client_base.py
provides the following functions
__init__(client_name, server_url)
- Description: Create an client instance.
- Arguments:
- client_name: the name of the rpc client.
- server_url: the url to the rpc server.
send_request(request)
- Description: Send request to the server and get response.
- Arguments:
- request: the request proto message.
The user of PSLX can also define other function to create a specific request
and call send_request
inside the function. One example
can be found at instant_messaging/client.py.
In this example, a customized function send_message
wraps the send_request
and then allows user to pass more concrete arguments to
interact with rpc server.
Inside PSLX, RPC requests and responses will finally be parsed as Any
proto message and hence the interface is universal. To create a
PSLX server, the user of PSLX needs to bind an RPC to a server using the methods provided by generic_server.
_init__(server_name)
- Description: Create an generic server instance.
- Arguments:
- server_name: the name of the server. Needs to be unique.
create_server(max_worker, server_url)
- Description: Create an generic server.
- Arguments:
- max_worker: the maximum number of workers for the server.
- server_url: the url to the rpc server.
Note
Each generic server is only allowed to bind to one rpc instance.
bind_rpc(rpc)
- Description: Bind an RPC instance to the server.
- Arguments:
- rpc: the rpc instance to be binded.
start_server()
- Description: Start running the server.