Skip to content

Conversation

nathanjsweet
Copy link
Member

@nathanjsweet nathanjsweet commented Dec 11, 2020

time.After is not garbage collected until after
its channel fires once. In situations where it is called
repeatedly this can lead to significant memory build-up.
Instead of creating a new timer object for every iteration,
a single timer, with a hard-reset mechanism, is used in
situations where time.After is being called repeatedly.

See the following article for details:
https://medium.com/@oboturov/golang-time-after-is-not-garbage-collected-4cbc94740082

pkg: Add inctimer

The new inctimer package is useful for boiling down the monotonous
task of "hard" resetting the reused timer. In order to reuse a timer
to solve the time.After garbage collection problem the timer must
be stopped, have its channel cleared (or not, if it was already read),
and then reset. Duplicating this code all over the cilium code base
seems superfluous, a small package that mimics the more terse
time.After method seems more desirable.

Signed-off-by: Nate Sweet nathanjsweet@pm.me

fixes: #14219

@nathanjsweet nathanjsweet requested review from a team December 11, 2020 20:06
@nathanjsweet nathanjsweet requested review from a team as code owners December 11, 2020 20:06
@nathanjsweet nathanjsweet requested a review from a team December 11, 2020 20:06
@nathanjsweet nathanjsweet requested a review from a team as a code owner December 11, 2020 20:06
@nathanjsweet nathanjsweet requested a review from a team December 11, 2020 20:06
@nathanjsweet nathanjsweet requested review from a team as code owners December 11, 2020 20:06
@nathanjsweet nathanjsweet requested a review from a team December 11, 2020 20:06
@nathanjsweet nathanjsweet requested review from a team as code owners December 11, 2020 20:06
@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Dec 11, 2020
@nathanjsweet nathanjsweet requested a review from glibsm December 11, 2020 20:06
@nathanjsweet nathanjsweet marked this pull request as draft December 16, 2020 18:00
@nathanjsweet nathanjsweet force-pushed the pr/nathanjsweet/remove-time-after branch from 7aa2fd5 to 8734c83 Compare December 16, 2020 18:13
@nathanjsweet nathanjsweet removed the dont-merge/blocked Another PR must be merged before this one. label Dec 16, 2020
@nathanjsweet nathanjsweet force-pushed the pr/nathanjsweet/remove-time-after branch 2 times, most recently from 6d02ddf to f1cd386 Compare December 16, 2020 18:35
@nathanjsweet
Copy link
Member Author

test-me-please

@nathanjsweet nathanjsweet marked this pull request as ready for review December 16, 2020 18:38
Copy link
Member

@aanm aanm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nathanjsweet the tests are failing in this line

sudo -u vagrant -H -E make LOCKDEBUG=1 SKIP_K8S_CODE_GEN_CHECK=false SKIP_DOCS=true

@nathanjsweet nathanjsweet force-pushed the pr/nathanjsweet/remove-time-after branch from f1cd386 to e061447 Compare December 17, 2020 22:08
`time.After` is not garbage collected until after
its channel fires once. In situations where it is called
repeatedely this *can* lead to significant memory build up.
Instead of creating a new timer object for every iteration,
a single timer, with a hard-reset mechanism, is used in
situations where `time.After` is being called repeatedely.

See the following article for details:
https://medium.com/@oboturov/golang-time-after-is-not-garbage-collected-4cbc94740082

pkg: Add inctimer

The new inctimer package is useful for boiling down the monotonous
taks of "hard" resetting the reused timer. In order to reuse a timer
to solve the `time.After` garbage collection problem the timer must
be stopped, have its channel cleared (or not, if it was already read),
and then reset. Duplicating this code all over the cilium code base
seems superfluous, a small package that mimics the more terse
`time.After` method seems more desireable.

Signed-off-by: Nate Sweet <nathanjsweet@pm.me>
@nathanjsweet nathanjsweet force-pushed the pr/nathanjsweet/remove-time-after branch from e061447 to c626046 Compare December 17, 2020 22:12
@nathanjsweet
Copy link
Member Author

test-me-please

@nebril
Copy link
Member

nebril commented Dec 18, 2020

looks like we hit some kind of GH outage, rerunning tests

@nebril
Copy link
Member

nebril commented Dec 18, 2020

test-me-please

@aanm
Copy link
Member

aanm commented Dec 21, 2020

retest-runtime

@aanm
Copy link
Member

aanm commented Dec 21, 2020

retest-gke

@aanm
Copy link
Member

aanm commented Dec 21, 2020

The previous runtime test failed with

	 level=error msg="arping failed" error="i/o timeout" interface=veth0 ipAddr=9.9.9.250 subsys=linux-datapath
	 node_linux_test.go:1038:
	     c.Assert(found, check.Equals, true)
	 ... obtained bool = false
	 ... expected bool = true

@aanm aanm merged commit f0ae32c into master Dec 22, 2020
@aanm aanm deleted the pr/nathanjsweet/remove-time-after branch December 22, 2020 10:32
@christarazi
Copy link
Member

Marking for backport to v1.9 as this is an important change to have for stable releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/CI Continuous Integration testing issue or flake area/CI-improvement Topic or proposal to improve the Continuous Integration workflow area/misc Impacts miscellaneous areas of the code not otherwise owned by another area. release-note/misc This PR makes changes that have no direct user impact.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove time.After usages
10 participants