Erlang -- hotwheels source code analysis

Source: Internet
Author: User

Overall architecture:

Janus_app (supervisor, pid_root) supervises the following four tasks:

Janus_transport_sup (essentially transport, supervisor, client instance supervisor): Each TCP session creates a transport process to process requests from the corresponding client.

Janus_topman_sup (essentially topman, worker, topic manager): creates multiple pubsup modules (gen_server) processes through the topman (gen_server) module to manage topics.

Janus_proxy_mapper_sup (essentially Mapper, worker, client proxy Mapper): Records of the client proxy module.

Janus_sup (essentially janus_acceptor, worker, TCP server): receives and processes TCP requests.

The above architecture is only a preliminary division of modules. Next I will talk about the more detailed and essential parts of TCP network requests. If there are any errors or deficiencies, please take a photo of me. Thank you!

Start Process:

Janus. app -- "janus_app.erl (supervisor), create the above four processes, here only track janus_acceptor.erl. -- In janus_acceptor.erl, acceptor_init () generates listensocket through gen_tcp: Listen (). In this case, {active, false} is blocked, and then enters the acceptor_loop to start loop listening to client TCP requests. This cycle mainly involves three processes:

1. gen_accept () generates socket;

2. gen_tcp: controlling_process () assigns the socket communication to the newly generated transport process for processing;

3. set the {active, once} of transport to start receiving the message -- handle_info ({tcp, socket, Bin}, state) after the message is received and transferred to the newly created janus_flash: Process () it is necessary to extract the server's business logic to a new module, in my current project, due to the immature Design Concept of Erlang at that time, a single module was extremely expanded and there was not enough time for reconstruction. -- Janus_flash creates client_proxy (gen_server) client proxy to handle session management operations such as heartbeat. The heartbeat here will be introduced below, and Facebook also uses this idea to maintain sessions, dear user. -- "Records the Client Session proxy in mapper (gen_server.

It is worth noting that proc_lib: start_link () and proc_lib: init_ack () pairs are used before and after janus_acceptor's gen_tcp: Listen () to ensure the atomicity of Process Creation.

Proc_lib: start_link () and proc_lib: init_ack () implement a small Process Creation or related atomic operations through the message transmission mechanism. In the most familiar gen_server, gen_fsm, this processing method is applied in gen_event. Speaking of the standard library of Erlang, I personally have some improvements and extensions in the standard library functions. I will write a series of blogs about how to learn Erlang and how to use Erlang.CodeStyle to write better product-level code.

Other modules in hotwheels are as follows:

Flashbot. erl & barrier. erl & bot. erl & histo. erl & launcher. erl: data statistics, latence calculation, etc.

Bin. erl: uses the re module to calculate binary Regular Expressions (similar to regular expression matching rules of Perl ).

Common. erl: provides a function that generates random tokens.

T. erl: Debug related processing, using the dbg module.

Util. erl: Manages processes.

The message push solution on mobile phones currently has the following common methods:

1. Regular query: connect to the server for the latest message according to the specified interval. It is easy to implement and non-real-time. If the query time is too short, the traffic consumption is high and the power consumption is high.

2. Long polling (applicable to Android): basically the same method used by many websites (Web aliwangwang, Weibo, and Renren ). The client initiates a long time-out request, and the server blocks the request when there is no message (the request is never returned) until it is about to time out, and a response is returned when a message arrives. The client initiates a request immediately after receiving the response or timeout.

This is a good way to get messages to the client in a timely manner. However, considering the characteristics of the mobile Internet (the network is unstable and the device memory is small), this method cannot ensure that important messages can be pushed to the client, in addition, the anroid may kill the process that is not very active while waiting for the push message when the phone memory is small.

3. push (applicable to iPhone): Because the iPhone app has no network connection in the background, messages can only be pushed through the push on the Apple official website, but there is a depressing situation, that is to say, after the switch to the background, the forced shutdown means will not be able to get notifications to close the push, so there are often someProgramYou didn't enable it, but he still pushed it. This is not necessarily the app's "robber sales", but it may be caused by this. So I read the official website of Apple and found a suitable option, that is, the destroy method of Apple push to obtain the inactive users of the app and release the push Relationship.

4. XMPP: open-source IM communication protocol, which seems to be supported by other IM groups except QQ. The disadvantage is that there must be registration, login, and other processes first. The effect of connection in the wireless network environment is not very good. Moreover, Sina's XMPP server is not stable...

5. mqtt: a message transmission protocol based on the proxy publishing/subscription mode. It is said that it is suitable for wireless mobile apps. You can check it out when you have time, and there are also open-source projects of Erlang, unfortunately, only version 0.1.0 is available... Https://github.com/erylee/eMQTT

I have just learned Erlang and recently found the learning status. I will write a cowboy source code analysis tomorrow. You are also welcome to discuss other Erlang issues with me. No figure, no table. Sorry, there is no way. On the machine, there is only a Linux system that I am not familiar ......

I really want to go back to C ++...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.