background
Last week, I wrote an article, go daily a library of ants, in-depth analysisants
Implementation of this goroutine pool. After repeatedly reading panjf2000 aboutants
The article on the origin of GMP concurrency scheduler — the deep parsing hand of GMP concurrency scheduler, rolling out a high-performance goroutine pool, I feel full of harvest. This article is of great reference value for understanding the goroutine concurrency mechanism of go, and it is strongly recommended to read it. Then I spent hours reading it in detailants
Source code, the code is written very well, very beautiful. Then I wrote an article and analyzed itants
For the source code, seeants
Appreciation of source code. Writing an introductionants
In the process of reading the source code, online materials refer to the implementation of goroutine pool in go language, which is often brought with youtunny
This library. So I went to study it againtunny
Source code, output an article, go, a library every day, tunny.
In readingtunny
Source code, I found that there are some problems in the implementation of a method. I also pointed out in the tunny of go daily library:
principle
We know that slice structure has a pointer to array. Suppose at firsttunny
There are 5 workers in the, and the schematic diagram is as follows:
Each element in the array is a pointer to aworker
Structure. Then we uses := s[:4]
The volume is reduced and the inlet becomes as follows:
Now the last element can’t be accessed through the slice, but it is referenced by the underlying array and can’t be cleaned up by the GC of go runtime. 360 software housekeeper can’t. There is a memory leak. Although the leakage is not serious, first, the amount can not be large because the number of workers is limited. Second, after the next capacity expansion, the original worker can be released (because there is no pointer referencing it).
ants
There are similar operations in the source code, but the shrunk elements will be set tonil
。 for exampleworker_stack.go
Mediumreset()
method:
func (wq *workerStack) reset() {
for i := 0; i < wq.len(); i++ {
wq.items[i].task <- nil
wq.items[i] = nil
}
wq.items = wq.items[:0]
}
PR
Identified the problem. I forked firsttunny
Our warehouse. Click the fork button:
Then download my own warehouse after fork:
$ git clone [email protected]:darjun/tunny.git
Modify the code, commit and push to my remote warehouse. Here are my changes:
Then gotunny
Source warehouse, clickPull Request
Button. Then click the on the rightNew pull request
Button:
Create a new PR and clickcompare across forks
, select my fork:
Select the submission I made the modification, then fill in the information and submit, because I have submitted pr. There is no difference in this comparison.
Then wait for the authors to merge. One day later, the author incorporated this modification:
summary
GitHub is not difficult to submit pr. you can mention PR from new features to a spelling error. I believe many people have heard that Linus Torvalds personally merged a PR on spelling errors in Linux source code comments submitted by an 11-year-old child.
Many star projects on GitHub are not perfect, and there must be a lot to improve. Therefore, when reading the source code, you need to have a certain spirit of doubt and don’t take what as a standard.
If you find a fun and easy-to-use go language library, you are welcome to submit an issue on GitHub, the daily library of go
reference resources
- tunny GitHub:github.com/Jeffail/tunny
- ants GitHub:github.com/panjf2000/ants
- Go one library a day GitHub: https://github.com/darjun/go-daily-lib
I
My blog: https://darjun.github.io
Welcome to my WeChat official account, GoUpUp, learn together and make progress together.