How to read the source code of an open source project?

Source: Internet
Author: User
Tags golang ide
This is a creation in Article, where the information may have evolved or changed.

About half a month ago, I learned a little bit about the theoretical knowledge of some distributed systems. But they are boring, and some things, in my opinion, are obsolete. So, I think, find a Distributed System project source code, to understand the implementation of distributed systems. First I thought of kubernetes, but I was afraid that because I didn't know much about it, I couldn't understand it successfully. So I chose to study Docker first.

As we all know, Docker is written in Golang language. I knew nothing about Golang until I read the source of Docker.

In fact, I was a long time ago, I would like to find an open source project source to read. So I searched the internet for a lot of articles on how to read the source code of an open source project, as well as questions and answers. But they are all in the previous chapter. Did not provide any substantive help.

So, this time, I learned a little bit about the Golang language (it took me about a morning to go through the tutorial on the official website), and then I started to roll up my sleeves and dry it.

From reading the source of the Docker to the present, there are also some reading the source code of the sources of inspiration, here to record and share with you. Of course, the source code of this project I did not finish reading, read for about a week. So, if there is any sentiment in the future, I will update this article.

Choose a right open source project

There are several reasons why we read the source code for an open source project. One is interested, want to understand its implementation principle. or for the framework class, we use it, in the use of the process encountered a pit, Google can not solve our problems, we have to look at the source code to find the problem. Or just like me, to see how a system is implemented in the project.

For example, if you want to know how to implement a server. Open source has Apache, Nginx, Lighttpd. So how to choose?

My criteria for selection are:

    • Clear structure
    • Use the language I am familiar with
    • Feature comparison Refinement

The most important of these is the first and third, the well-structured code allows us to have a clearer picture of the structure, and the streamlined code allows us to focus on our focus.

It doesn't matter what language it is, even if you use unfamiliar language, it's just a little bit of time to get familiar with grammar.

Before reading the source code, if you can find a corresponding project from the Internet, the architecture diagram, it will be more effective.

Understand the use of the corresponding open source project

This is also important. If you do not know how to use the open source project, it will not be clear to find the entry point, and can not focus on the reading source.

For example, you should read Docker's source code, and you don't even know how to use Docker. That's a clear excuse.

Select the correct version

I started pulling out the version of Docker's master just from GitHub. Compiled is version 17.04. And I'm using the 1.12.6 version on this machine. When I read the containerd section, I wanted to debug it, however, when I was in containerd for a health check, I was always confronted with a libcontainerd:containerd-check returned Error:rpc Error:code = desc = grpc:the Connection is unavailable this problem. Read this part of the source code, did not find any problem. Guessing is a libcontainerd version of the problem. So it checkout 1.12.x source code, recompile, no problem.

Also, if you choose a source that is inconsistent with the version you are using to study, if it adds something, you may not be able to read the source code.

Choosing the Right IDE

For projects with a project structure that is not complex, it is possible to use VIM to study the code. But what about the Docker project, you try it with one? Make sure you're devastated. So I chose the IntelliJ idea + Golang plugin. When the JetBrains family's Golang IDE comes out, switch to the new one.

For VIM, which does not jump to the variable definition, the method definition of the editor, it is difficult to see.

Remember to write notes

Annotations are important. In particular, open source projects are generally less commented. If you don't write a note, you'll soon forget the meaning of the source code you've seen. Also, when writing comments, it is best to take the steps. For example, Docker's Docker daemon starts with the main function, and I'll write the following comment:

// Reading: 1 - Initialize the configuration of daemon// Reading: 2 - Config the api server// Reading: 3 - Config the log// Reading: 4 - Check daemon root

where //Reading: The section indicates that this comment was added to facilitate comprehension when I was reading. 1- section indicates that this is the first step in the daemon startup process. Initialize The configuration of the daemon section represents the role of this part of the code.

By documenting the next steps, you will become more aware of its architecture and its processes.

You can also record the process by drawing a flowchart.

Search for relevant information

In general, the implementation of this open source project involves some features that we are unfamiliar with in the kernel. For example, in NiO, epoll is used, and Docker is implemented with namespace and Cgroup . This part is going to take a lot of energy to query the relevant theoretical knowledge.

Related Article

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.