One PR submitted for tunny

Time:2022-1-13

background

Last week, I wrote an article, go daily a library of ants, in-depth analysisantsImplementation of this goroutine pool. After repeatedly reading panjf2000 aboutantsThe 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 detailantsSource code, the code is written very well, very beautiful. Then I wrote an article and analyzed itantsFor the source code, seeantsAppreciation of source code. Writing an introductionantsIn the process of reading the source code, online materials refer to the implementation of goroutine pool in go language, which is often brought with youtunnyThis library. So I went to study it againtunnySource code, output an article, go, a library every day, tunny.

In readingtunnySource 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:

One PR submitted for tunny

principle

We know that slice structure has a pointer to array. Suppose at firsttunnyThere are 5 workers in the, and the schematic diagram is as follows:

One PR submitted for tunny

Each element in the array is a pointer to aworkerStructure. Then we uses := s[:4]The volume is reduced and the inlet becomes as follows:

One PR submitted for tunny

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).

antsThere are similar operations in the source code, but the shrunk elements will be set tonil。 for exampleworker_stack.goMediumreset()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 firsttunnyOur warehouse. Click the fork button:

One PR submitted for tunny

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:

One PR submitted for tunny

Then gotunnySource warehouse, clickPull RequestButton. Then click the on the rightNew pull requestButton:

One PR submitted for tunny

Create a new PR and clickcompare across forks, select my fork:

One PR submitted for tunny

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:

One PR submitted for tunny

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

  1. tunny GitHub:github.com/Jeffail/tunny
  2. ants GitHub:github.com/panjf2000/ants
  3. 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.