-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explicitly stop the timer when the connection is closed. #138
Conversation
This won't work either, because the goroutine started on each inocation of broadcastAfter() will not actually return on timer.Stop() (it doesn't actually close the channel). I'm trying to see if there is a solution that involves a single long living broadcaster goroutine |
Then how do you cancel an in-flight timer ? I am under the impression that calling timer.Stop and then calling broadcast will wake the goroutine, which will call Stop again, which will return false, and then it will then drain the channel and terminate... Or am I mistaken ? I'm not very familiar, so any correction will be appreciated. EDIT: Yeah, I tested, it doesn't work |
Codecov Report
@@ Coverage Diff @@
## master #138 +/- ##
==========================================
+ Coverage 67.68% 67.77% +0.09%
==========================================
Files 37 37
Lines 2392 2402 +10
==========================================
+ Hits 1619 1628 +9
- Misses 610 611 +1
Partials 163 163
Continue to review full report at Codecov.
|
I don't think golang's for {
select {
case <-p.timer.C:
p.rwCond.Broadcast()
case <-p.closeCh:
return
}
} Alternatively, we could probably do this for {
<-p.timer.C
if p.closed { // Checking this is technically a race condition, but this can be easily solved by using atomics
return
} else {
p.rwCond.Broadcast()
}
} And in Close() we do |
Even if you unblock the goroutine by selecting on another channel that gets filled during close, don't you still need to clean up the timer (there's a runtimeTimer inside) and a channel also inside that need be cleaned up, otherwise it's still leaking... I think I prefer the https://stackoverflow.com/questions/50223771/how-to-stop-a-timer-correctly |
7a41059
to
2594df7
Compare
OK i implemented the second channel solution (in my updated push). But memory usage is still climbing... Just that the profile now changed. I will update the bug report. #137 |
2594df7
to
208a7f2
Compare
@cbeuw Please check the updated branch, the issue is much much better now, almost solved. I'll leave it running for long time to confirm it. |
Memory use is even better now, only 25 MB RAM usage stable with 110 active sessions! |
That's great to hear. Thanks a lot for the help! |
Fixes #137 again.