Preface
Recently, I was reading byte beating open source RPC frame Kitex Source code , Analysis of the How to use the command line , By a IDL file , Generate client and server Scaffold code , Also analyzed Kitex Log component of klog. Of course Kitex There are many other components : Service registration 、 Find out 、 Load balancing 、 Fuse 、 Current limiting wait , I will continue to analyze it later .
I hope to use this article , Use as few languages as possible , Cooperate with the analysis Go Native net/rpc Part of the core code of the package , Help you get through RPC Knowledge , carding RPC The operation process of , Make you right RPC Have a more comprehensive understanding of .
On this basis , Will help you in reading other open source RPC Framework source code , Compare and explore open source RPC What specific improvements have the framework made .
RPC The process of
Remote procedure call (Remote Procedure Call,RPC) Is a computer communication protocol . Allow a program running on one computer to call a subroutine in another address space ( It is usually a computer in an open network ), Programmers are like calling local programs , No additional interactive programming is required .

Suppose you want to call a Add(a int, b int) int Method , Realize the summation function , But this method is deployed on another machine , How to call ?

This is one time RPC The process of , Even with HTTP request / The response process is very similar to , For now, I will focus on introducing RPC The concept of , It will be introduced in the future HTTP The difference between .
And the so-called Service registration 、 Find out 、 Load balancing 、 Fuse 、 Current limiting Equal word , These are all mature RPC The functional components that a framework should have , Used to ensure a RPC High availability of the framework , But it is not a RPC Required for framework .
RPC The protocol essentially defines a flow of communication , The specific implementation technology is not constrained , Each of these RPC Frameworks have their own implementation methods , For example, you can set your own RPC request / The response contains a message header and a message body , Use gob/json/pb/thrift Serialization / Deserialize message content , Use socket/http2 Network communication , as long as client and server The sending and parsing of messages can correspond to each other . I hope readers will carefully understand ——“ Appointment ” The concept , This will continue throughout .
analysis net/rpc
First, let's explain... In the flow chart Serialization and network transmission part , This is a RPC At the heart of .
Message encoding / decode ( serialize )

above RPC Communication flow chart , One of the most important links is the encoding and decoding of messages , The message is only serialized , To effectively participate in network transmission . By implementing the above figure net/rpc Interface defined by package , You can specify the encoding and decoding method to use , such as net/rpc By default, the package uses gob Binary code :

The server is responsible for the structure of serialization gobServerCodec Realized. ServerCodec Interface , Where the server needs to encode and decode messages , Will be called gobServerCodec The corresponding method of ( The client is a similar implementation , The same is true for gob codec ).
Network transmission of messages
After message serialization , It needs to be used for network transmission , It involves the communication mode between the client and the server .

This is the logic for the server to accept links , Same as most web applications ,server Monitored a ip:port
, then accept After a connection , Will open a go Co process requests and responses .

This is how the client initiates the request , It also confirms socket Communication model of network programming .
I understand RPC After each process of , You can sort it out RPC Framework of the Various components At what level does it work , for example Kitex Network library of netpoll, Although I have never seen its source code implementation , But there is reason to guess that it is in network communication / The transmission part has been improved .
Server End design

This is a service Structure , You can see A service adopt Map Can be bound Methods with multiple names , Provide calls , And corresponding service need Register to the server in advance , In this way, when the client request is reached, it can accurately call .

The main parameters of service registration are serviceName and service Entity .
reflect.xxx(): The main work is through the mechanism of reflection , Resolve the name of the bound service 、 Type, etc .
suitableMethods(): Analyze aserviceAll of the bindingmethod.
serviceMap.LoadOrStore(): takeserviceRegister to the serverserverOf Map, The following isServerStructure :

Client End design

This is a Client Structure :
codec: The concrete implementation of encoding and decoding .seq:RPC The serial number of , The count is incremented with each initiation , Join in Map, And after completion or failure from Map Remove .pending: coordinationseqWorking Map.

This is a specific initiation by the client RPC Process of request , Of course, a specific RPC Requests can be synchronous , It can also be asynchronous :

client.Go()It's asynchronous .client.Call()It's synchronous , And its internal is to callclient.Go(), But because after its call , Before the call completes , Will be blocked inchanOn , So the following RPC The request must wait to be sent .
Summary
So far we have made a superficial analysis net/rpc Some of the core source code , To sort out RPC workflow , It mainly includes :
- RPC The codec ( serialize ) Choice of agreement
- RPC Network communication / Transmission model (Socket Programming )
- RPC The request for / Respond to accept ( Sync / asynchronous )
RPC Functional components of

A mature RPC It is not enough for a framework to implement only basic communication functions , Otherwise it will be very fragile , No ability to cope with service downtime , In high concurrency scenarios, it is also embarrassing , So we need to add a lot of Functional components To improve service reliability :
- Timeout control | Request to retry | Load balancing | Fuse | Current limiter | journal | monitor | Link tracking |...
(Go Native net/rpc The package also has many designs to improve reliability , This article does not expand too much )
Conclusion
This article , I use Go Native net/rpc Part of the core source code of the package , Combed RPC workflow , Trying to help you build RPC Overall concept of , I hope you understand ,RPC The frame is right RPC The concrete realization of communication flow , Each framework improves its own reliability , It also extends a variety of functional components .
In subsequent articles, I will also continue to analyze byte beating open source RPC frame Kitex The core component source code , Mutual encouragement .
Official account 【 Programmer Bai Ze 】, I will share blog posts synchronously .









