Skip to content
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

Use time.NewTimer() instead of time.After() #56

Merged
merged 2 commits into from
Sep 19, 2023

Conversation

afriza
Copy link
Contributor

@afriza afriza commented Sep 18, 2023

For efficiency, cancel time.Timer after no longer needed. See https://pkg.go.dev/time#After

Note on behavior change:
timeout duration <= 0 now means waiting forever instead of immediately times out..

@andreykaipov
Copy link
Owner

Very interesting, I didn't know time.After() leaks memory!

The underlying Timer is not recovered by the garbage collector until the timer fires.

Out of curiosity I wanted to see it for myself so I added profiling in #57 and sure enough:

❯ GOOBS_PROFILE=memprofile=mem.out OBS_PORT=4455 go test -v -run=profile client_test.go
❯ go tool pprof -top -sample_index=inuse_space mem.out
Type: inuse_space
Time: Sep 19, 2023 at 6:05pm (CDT)
Showing nodes accounting for 512.05kB, 100% of 512.05kB total
      flat  flat%   sum%        cum   cum%
  512.05kB   100%   100%   512.05kB   100%  time.NewTimer
         0     0%   100%   512.05kB   100%  command-line-arguments_test.Test_profile
         0     0%   100%   512.05kB   100%  github.com/andreykaipov/goobs/api.(*Client).SendRequest
         0     0%   100%   512.05kB   100%  github.com/andreykaipov/goobs/api/requests/scenes.(*Client).GetSceneList
         0     0%   100%   512.05kB   100%  testing.tRunner
         0     0%   100%   512.05kB   100%  time.After (inline)

Hope you don't mind I updated your PR to use defer timer.Stop() instead of potentially waiting forever. Found that in golang/go#27169 and it seems like it's hit others too. Thanks again!

@andreykaipov andreykaipov merged commit b67ee58 into andreykaipov:master Sep 19, 2023
@afriza
Copy link
Contributor Author

afriza commented Sep 20, 2023

@andreykaipov no problem on the updated PR. defer also works for efficiency.

But I am curious about:

  1. As I understand, without the if in my newTimeoutTimer, the timeout duration <= 0 means immediately times out. So how is this better than waiting forever for c.IncomingResponses/authComplete? In my opinion, "no timeout" (duration <= 0) better fits waiting forever than immediately timing out.
  2. I think the possibility of waiting forever mentioned in the golang issue is different from what I mean in (1) since the one in the golang issue is caused by deadlock while the one I mentioned in (1) is about interpretation of duration <= 0.

Thank you for the merge.

@afriza afriza deleted the timer branch September 20, 2023 07:49
@andreykaipov
Copy link
Owner

My apologies -- I thought the motivation of the PR was only for the timer memory efficiency. Sorry for discarding your timeout changes without any comment.

I suppose I was concerned about backwards compatibility for anybody that might've been using a zero timeout already. However thinking about it now... that'd be impossible since immediately timing out wouldn't allow for the connection to establish in the first place.

I'm not against adding a way to specify an indefinite timeout but out of curiosity does using a really large timeout (e.g. 24 hours) not meet your needs?

@afriza
Copy link
Contributor Author

afriza commented Sep 29, 2023

@andreykaipov
The default timeout works for me and setting large timeout is not an issue for me if needed.
I agree that the need of unlimited timeout is almost as unreasonable as immediate timeout.
But in my opinion, unlimited makes more sense than immediate timeout.
I just wanted to clarify between the deadlock issue vs unlimited timeout.
Not really against immediate timeout though, but my preference is over unlimited in the case of a value <= 0 .
If you prefer to keep the existing behavior, that is fine as well.

@andreykaipov andreykaipov added bug Something isn't working fix labels Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants