fix: close goroutine pool to prevent goroutine leaks after multiple generate calls #499
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Hello,
While using this library in my service, I encountered an issue with goroutine leaks. Below is the graph from New Relic showing the issue I discovered:
![image](https://private-user-images.githubusercontent.com/44210574/394026488-6b794a64-6c0e-4c39-9909-0dc056c10f88.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxMzE2NzAsIm5iZiI6MTczOTEzMTM3MCwicGF0aCI6Ii80NDIxMDU3NC8zOTQwMjY0ODgtNmI3OTRhNjQtNmMwZS00YzM5LTk5MDktMGRjMDU2YzEwZjg4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA5VDIwMDI1MFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM5NDVjMjBlNGY5NzA4NzE5ZGQ3NWYwMjEzZDJhZjBiOWQ5ZmYyYmIxZGViNDM0NDYwOGZmYWYxMzRiODdjMmEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.dRqlyMh3CGvWxckYnAYaB3-PFtP1SOo-pOhK4VyQd2A)
My application has more than 7k open goroutines.
To identify the cause of the leak, I used pprof and discovered a bug in Maroto. Below is the result of pprof before generating any documents:
pprof goroutines before generate documents
After generating just 10 documents, the total number of open goroutines is:
pprof goroutines after generate documents
Upon analyzing the code, I noticed that the pool of goroutines was never closed, leading to the leak. I have refactored the code to ensure that the goroutines are closed after execution. Additionally, I moved the pool initialization to the Generate function to ensure that a new pool is created each time Generate is called, preventing any unnecessary goroutines from being left open.
I also added unit tests to ensure that no further goroutine leaks occur, particularly in concurrent mode.
Related Issue
Checklist
func (<first letter of struct> *struct) method() {}
name style.when,should
naming pattern.m := mocks.NewConstructor(t)
.m.EXPECT().MethodName()
method to mock methods.example_test.go
.make dod
with none issues pointed out bygolangci-lint