Comparison of basic features

RPC IDL Communication Network data Compression Attachment Semi-synchronous Asynchronous Streaming
Thrift Binary Framed Thrift TCP Binary Not supported Not supported Supported Not supported Not supported
Thrift Binary HttpTransport Thrift HTTP Binary Not supported Not supported Supported Not supported Not supported
GRPC PB HTTP2 Binary gzip/zlib/lz4/snappy Supported Not supported Supported Supported
BRPC Std PB TCP Binary gzip/zlib/lz4/snappy Supported Not supported Supported Supported
SogouRPC Std PB/Thrift TCP Binary/JSON gzip/zlib/lz4/snappy Supported Supported Supported Not supported
SogouRPC Std HTTP PB/Thrift HTTP Binary/JSON gzip/zlib/lz4/snappy Supported Supported Supported Not supported
tRPC Std PB TCP Binary/JSON gzip/zlib/lz4/snappy Supported Supported Supported Not supported

Basic concepts

  • Communication layer: TCP/TPC_SSL/HTTP/HTTPS/HTTP2
  • Protocol layer: Thrift-binary/BRPC-std/SogouRPC-std/tRPC-std
  • Compression layer: no compression/gzip/zlib/lz4/snappy
  • Data layer: PB binary/Thrift binary/JSON string
  • IDL serialization layer: PB/Thrift serialization
  • RPC invocation layer: Service/Client IMPL

RPC Global

  • Get srpc version srpc::SRPCGlobal::get_instance()->get_srpc_version()

RPC Status Code

Name Value Description
RPCStatusUndefined 0 Undefined
RPCStatusOK 1 Correct/Success
RPCStatusServiceNotFound 2 Cannot find the RPC service name
RPCStatusMethodNotFound 3 Cannot find the RPC function name
RPCStatusMetaError 4 Meta error/ parsing failed
RPCStatusReqCompressSizeInvalid 5 Incorrect size for the request when compressing
RPCStatusReqDecompressSizeInvalid 6 Incorrect size for the request when decompressing
RPCStatusReqCompressNotSupported 7 The compression type for the request is not supported
RPCStatusReqDecompressNotSupported 8 The decompression type for the request is not supported
RPCStatusReqCompressError 9 Failed to compress the request
RPCStatusReqDecompressError 10 Failed to decompress the request
RPCStatusReqSerializeError 11 Failed to serialize the request by IDL
RPCStatusReqDeserializeError 12 Failed to deserialize the request by IDL
RPCStatusRespCompressSizeInvalid 13 Incorrect size for the response when compressing
RPCStatusRespDecompressSizeInvalid 14 Incorrect size for the response when compressing
RPCStatusRespCompressNotSupported 15 The compression type for the response is not supported
RPCStatusRespDecompressNotSupported 16 The decompression type for the response not supported
RPCStatusRespCompressError 17 Failed to compress the response
RPCStatusRespDecompressError 18 Failed to decompress the response
RPCStatusRespSerializeError 19 Failed to serialize the response by IDL
RPCStatusRespDeserializeError 20 Failed to deserialize the response by IDL
RPCStatusIDLSerializeNotSupported 21 IDL serialization type is not supported
RPCStatusIDLDeserializeNotSupported 22 IDL deserialization type is not supported
RPCStatusURIInvalid 30 Illegal URI
RPCStatusUpstreamFailed 31 Upstream is failed
RPCStatusSystemError 100 System error
RPCStatusSSLError 101 SSL error
RPCStatusDNSError 102 DNS error
RPCStatusProcessTerminated 103 Program exited&terminated

RPC IDL

  • Interface Description Languaue file
  • Backward and forward compatibility
  • Protobuf/Thrift

Sample

You can follow the detailed example below:

  • Take pb as an example. First, define an example.proto file with the ServiceName as Example.
  • The name of the rpc interface is Echo, with the input parameter as EchoRequest, and the output parameter as EchoResponse.
  • EchoRequest consists of two strings: message and name.
  • EchoResponse consists of one string: message.
  1. syntax="proto2";
  2. message EchoRequest {
  3. optional string message = 1;
  4. optional string name = 2;
  5. };
  6. message EchoResponse {
  7. optional string message = 1;
  8. };
  9. service Example {
  10. rpc Echo(EchoRequest) returns (EchoResponse);
  11. };

RPC Service

  • It is the basic unit for sogouRPC services.
  • Each service must be generated by one type of IDLs.
  • Service is determined by IDL type, not by specific network communication protocol.

Sample

You can follow the detailed example below:

  • Use the same example.proto IDL above.
  • Run the official protoc example.proto --cpp_out=./ --proto_path=./ to get two files: example.pb.h and example.pb.cpp.
  • Run the srpc_generator protobuf ./example.proto ./ in SogouRPC to get example.srpc.h.
  • Derive Example::Service to implement the rpc business logic, which is an RPC Service.
  • Please note that this Service does not involve any concepts such as network, port, communication protocol, etc., and it is only responsible for completing the business logic that convert EchoRequest to EchoResponse.
  1. class ExampleServiceImpl : public Example::Service
  2. {
  3. public:
  4. void Echo(EchoRequest *request, EchoResponse *response, RPCContext *ctx) override
  5. {
  6. response->set_message("Hi, " + request->name());
  7. printf("get_req:\n%s\nset_resp:\n%s\n",
  8. request->DebugString().c_str(),
  9. response->DebugString().c_str());
  10. }
  11. };

RPC Server

  • Each server corresponds to one port
  • Each server corresponds to one specific network communication protocol
  • One service may be added into multiple Servers
  • One Server may have one or more Services, but the ServiceName must be unique within that Server
  • Services from different IDLs can be added into the same Server

Sample

You can follow the detailed example below:

  • Follow the above ExampleServiceImpl Service
  • First, create an RPC Server and determine the proto file.
  • Then, create any number of Service instances and any number of Services for different protocols, and add these services to the Server through the add_service()interface.
  • Finally, use start() or serve() to start the services in the Server and handle the upcoming rpc requests through the Server.
  • Imagine that we can also derive more Serivce from Example::Service, which have different implementations of rpc Echo.
  • Imagine that we can create N different RPC Servers on N different ports, serving on different network protocols.
  • Imagine that we can use add_service() to add the same ServiceIMPL instance on different Servers, or we can use add_service() to add different ServiceIMPL instances on the same server.
  • Imagine that we can use the same ExampleServiceImpl, serving BPRC-STD, SogouRPC-STD, SogouRPC-Http at three different ports at the same time.
  • And we can use add_service() to add one ExampleServiceImpl related to Protobuf IDL and one AnotherThriftServiceImpl related to Thrift IDL to the same SogouRPC-STD Server, and the two IDLs work perfectly on the same port!
  1. int main()
  2. {
  3. SRPCServer server_srpc;
  4. SRPCHttpServer server_srpc_http;
  5. BRPCServer server_brpc;
  6. ThriftServer server_thrift;
  7. ExampleServiceImpl impl_pb;
  8. AnotherThriftServiceImpl impl_thrift;
  9. server_srpc.add_service(&impl_pb);
  10. server_srpc.add_service(&impl_thrift);
  11. server_srpc_http.add_service(&impl_pb);
  12. server_srpc_http.add_service(&impl_thrift);
  13. server_brpc.add_service(&impl_pb);
  14. server_thrift.add_service(&impl_thrift);
  15. server_srpc.start(1412);
  16. server_srpc_http.start(8811);
  17. server_brpc.start(2020);
  18. server_thrift.start(9090);
  19. pause();
  20. server_thrift.stop();
  21. server_brpc.stop();
  22. server_srpc_http.stop();
  23. server_srpc.stop();
  24. return 0;
  25. }

RPC Client

  • Each Client corresponds to one specific target/one specific cluster
  • Each Client corresponds to one specific network communication protocol
  • Each Client corresponds to one specific IDL

Sample

You can follow the detailed example below:

  • Following the above example, the client is relatively simple and you can call the method directly.
  • Use Example::XXXClient to create a client instance of some RPC. The IP+port or URL of the target is required.
  • With the client instance, directly call the rpc function Echo. This is an asynchronous request, and the callback function will be invoked after the request is completed.
  • For the usage of the RPC Context, please check RPC Context.
  1. #include <stdio.h>
  2. #include "example.srpc.h"
  3. using namespace srpc;
  4. int main()
  5. {
  6. Example::SRPCClient client("127.0.0.1", 1412);
  7. EchoRequest req;
  8. req.set_message("Hello, sogou rpc!");
  9. req.set_name("Li Yingxin");
  10. client.Echo(&req, [](EchoResponse *response, RPCContext *ctx) {
  11. if (ctx->success())
  12. printf("%s\n", response->DebugString().c_str());
  13. else
  14. printf("status[%d] error[%d] errmsg:%s\n",
  15. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  16. });
  17. pause();
  18. return 0;
  19. }

RPC Context

  • RPCContext is used specially to assist asynchronous interfaces, and can be used in both Service and Client.
  • Each asynchronous interface will provide a Context, which offers higher-level functions, such as obtaining the remote IP, the connection seqid, and so on.
  • Some functions on Context are unique to Server or Client. For example, you can set the compression mode of the response data on Server, and you can obtain the success or failure status of a request on Client.
  • On the Context, you can use get_series() to obtain the SeriesWork, which is seamlessly integrated with the asynchronous mode of Workflow.

RPCContext API - Common

long long get_seqid() const;

One complete communication consists of request+response. The sequence id of the communication on the current socket connection can be obtained, and seqid=0 indicates the first communication.

std::string get_remote_ip() const;

Get the remote IP address. IPv4/IPv6 is supported.

int get_peer_addr(struct sockaddr *addr, socklen_t *addrlen) const;

Get the remote address. The in/out parameter is the lower-level data structure sockaddr.

const std::string& get_service_name() const;

Get RPC Service Name.

const std::string& get_method_name() const;

Get RPC Method Name.

SeriesWork *get_series() const;

Get the SeriesWork of the current ServerTask/ClientTask.

RPCContext API - Only for client done

bool success() const;

For client only. The success or failure of the request.

int get_status_code() const;

For client only. The rpc status code of the request.

const char *get_errmsg() const;

For client only. The error info of the request.

int get_error() const;

For client only. The error code of the request.

void *get_user_data() const;

For client only. Get the user_data of the ClientTask. If a user generates a task through the create_xxx_task() interface, the context can be recorded in the user_data field. You can set that field when creating the task, and retrieve it in the callback function.

RPCContext API - Only for server process

void set_data_type(RPCDataType type);

For Server only. Set the data packaging type

  • RPCDataProtobuf
  • RPCDataThrift
  • RPCDataJson

void set_compress_type(RPCCompressType type);

For Server only. Set the data compression type (note: the compression type for the Client is set on Client or Task)

  • RPCCompressNone
  • RPCCompressSnappy
  • RPCCompressGzip
  • RPCCompressZlib
  • RPCCompressLz4

void set_attachment_nocopy(const char *attachment, size_t len);

For Server only. Set the attachment.

bool get_attachment(const char **attachment, size_t *len) const;

For Server only. Get the attachment.

void set_reply_callback(std::function<void (RPCContext *ctx)> cb);

For Server only. Set reply callback, which is called after the operating system successfully writes the data into the socket buffer.

void set_send_timeout(int timeout);

For Server only. Set the maximum time for sending the message, in milliseconds. -1 indicates unlimited time.

void set_keep_alive(int timeout);

For Server only. Set the maximum connection keep-alive time, in milliseconds. -1 indicates unlimited time.

RPC Options

Server Parameters

Name Default value Description
max_connections 2000 The maximum number of connections for the Server. The default value is 2000.
peer_response_timeout: 10* 1000 The maximum time for each read IO. The default value is 10 seconds.
receive_timeout: -1 The maximum read time of each complete message. -1 indicates unlimited time.
keep_alive_timeout: 60 * 1000 Maximum keep_alive time for idle connection. -1 indicates no disconnection. 0 indicates short connection. The default keep-alive time for long connection is 60 seconds
request_size_limit 2LL * 1024 * 1024 * 1024 Request packet size limit. Maximum: 2 GB
ssl_accept_timeout: 10 * 1000 SSL connection timeout. The default value is 10 seconds.

Client Parameters

Name Default value Description
host “” Target host, which can be an IP address or a domain name
port 1412 Destination port number. The default value is 1412
is_ssl false SSL switch. The default value is off
url “” The URL is valid only when the host is empty. URL will override three items: host/port/is_ssl
task_params The default configuration for tasks See the configuration items below

Task Parameters

Name Default value Description
send_timeout -1 Maximum time for sending the message. The default value is unlimited time.
receive_timeout -1 Maximum time for sending a response. The default value is unlimited time.
watch_timeout 0 The maximum time of the first reply from remote. The default value is 0, indicating unlimited time.
keep_alive_timeout 30 * 1000 The maximum keep-alive time for idle connections. -1 indicates no disconnection. The default value is 30 seconds.
retry_max 0 Maximum number of retries. The default value is 0, indicating no retry.
compress_type RPCCompressNone Compression type. The default value is no compression.
data_type RPCDataUndefined The data type of network packets. The default value is consistent with the default value for RPC. For SRPC-Http protocol, it is JSON, and for others, they are the corresponding IDL types.

Integrating with the asynchronous Workflow framework

Server

You can follow the detailed example below:

  • Echo RPC sends an HTTP request to the upstream modules when it receives the request.
  • After the request to the upstream modules is completed, the server populates the body of HTTP response into the message of the response and send a reply to the client.
  • We don’t want to block/occupy the handler thread, so the request to the upstream must be asynchronous.
  • First, we can use WFTaskFactory::create_http_task() of the factory of Workflow to create an asynchronous http_task.
  • Then, we use ctx->get_series() of the RPCContext to get the SeriesWork of the current ServerTask.
  • Finally, we use the push_back() interface of the SeriesWork to append the http_task to the SeriesWork.
  1. class ExampleServiceImpl : public Example::Service
  2. {
  3. public:
  4. void Echo(EchoRequest *request, EchoResponse *response, RPCContext *ctx) override
  5. {
  6. auto *http_task = WFTaskFactory::create_http_task("https://www.sogou.com", 0, 0,
  7. [request, response](WFHttpTask *task) {
  8. if (task->get_state() == WFT_STATE_SUCCESS)
  9. {
  10. const void *data;
  11. size_t len;
  12. task->get_resp()->get_parsed_body(&data, &len);
  13. response->mutable_message()->assign((const char *)data, len);
  14. }
  15. else
  16. response->set_message("Error: " + std::to_string(task->get_error()));
  17. printf("Server Echo()\nget_req:\n%s\nset_resp:\n%s\n",
  18. request->DebugString().c_str(),
  19. response->DebugString().c_str());
  20. });
  21. ctx->get_series()->push_back(http_task);
  22. }
  23. };

Client

You can follow the detailed example below:

  • We send two requests in parallel. One is an RPC request and the other is an HTTP request.
  • After both requests are finished, we initiate a calculation task again to calculate the sum of the squares of the two numbers.
  • First, use create_Echo_task() of the RPC Client to create an rpc_task, which is an asynchronous RPC network request.
  • Then, use WFTaskFactory::create_http_task and WFTaskFactory::create_go_task in the the factory of Workflow to create an asynchronous network task http_task and an asynchronous computing task calc_task respectively.
  • Finally, use the serial-parallel graph to organize three asynchronous tasks, in which the multiplication sign indicates parallel tasks and the greater than sign indicates serial tasks and then execute start().
  1. void calc(int x, int y)
  2. {
  3. int z = x * x + y * y;
  4. printf("calc result: %d\n", z);
  5. }
  6. int main()
  7. {
  8. Example::SRPCClient client("127.0.0.1", 1412);
  9. auto *rpc_task = client.create_Echo_task([](EchoResponse *response, RPCContext *ctx) {
  10. if (ctx->success())
  11. printf("%s\n", response->DebugString().c_str());
  12. else
  13. printf("status[%d] error[%d] errmsg:%s\n",
  14. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  15. });
  16. auto *http_task = WFTaskFactory::create_http_task("https://www.sogou.com", 0, 0, [](WFHttpTask *task) {
  17. if (task->get_state() == WFT_STATE_SUCCESS)
  18. {
  19. std::string body;
  20. const void *data;
  21. size_t len;
  22. task->get_resp()->get_parsed_body(&data, &len);
  23. body.assign((const char *)data, len);
  24. printf("%s\n\n", body.c_str());
  25. }
  26. else
  27. printf("Http request fail\n\n");
  28. });
  29. auto *calc_task = WFTaskFactory::create_go_task(calc, 3, 4);
  30. EchoRequest req;
  31. req.set_message("Hello, sogou rpc!");
  32. req.set_name("1412");
  33. rpc_task->serialize_input(&req);
  34. ((*http_task * rpc_task) > calc_task).start();
  35. pause();
  36. return 0;
  37. }