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

Reuse time.Timer in ack and rtx timer utilities #336

Merged
merged 1 commit into from
Apr 16, 2024
Merged

Conversation

paulwe
Copy link
Contributor

@paulwe paulwe commented Apr 16, 2024

No description provided.

Copy link

codecov bot commented Apr 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.18%. Comparing base (45982cd) to head (608e170).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #336      +/-   ##
==========================================
+ Coverage   81.13%   81.18%   +0.04%     
==========================================
  Files          49       49              
  Lines        3250     3247       -3     
==========================================
- Hits         2637     2636       -1     
+ Misses        470      469       -1     
+ Partials      143      142       -1     
Flag Coverage Δ
go 81.18% <100.00%> (+0.04%) ⬆️
wasm 66.76% <100.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@edaniels
Copy link
Member

@paulwe is this for performance by any chance?

@paulwe
Copy link
Contributor Author

paulwe commented Apr 16, 2024

@edaniels yes, we're seeing high runtime.siftdownTimer when serving large numbers of clients. these two utilities are responsible for creating over 99% of the time.Timer instances in our code.

Copy link
Member

@edaniels edaniels left a comment

Choose a reason for hiding this comment

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

LGTM

ack_timer.go Outdated
case ackTimerStarted:
t.state = ackTimerStopped
defer t.observer.onAckTimeout()
case ackTimerClosed:
Copy link
Member

Choose a reason for hiding this comment

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

is it worth emptying t.cancel here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

state and writes to the cancel channel are synced.

  • if we are in this critical section and the state is started we cannot have written to cancel.
  • if we change the state from started to stopped in this critical section then call stop afterward the state will be stopped and we won't won't write to cancel.
  • cancel could be dirty if the state is closed but we won't reuse the timer later so we don't care.

ack_timer.go Outdated
}

cancelCh := make(chan struct{})
t.state = ackTimerStarted

Choose a reason for hiding this comment

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

Is there a chance of race here? Is there a need to drain timer channel or t.cancel?

Thinking aloud

  1. stop() called: that will write to t.cancel and set state to stopped
  2. Assume, the go routine below has not drained t.cancel yet.
  3. start() happens: this will try to restart the timer as state is stopped
  4. This will launch another go routine. Will this clash with go routine in Step 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch. invocations of the callback are interchangeable and for this to happen the time between calls has to be practically zero so we can eat the extra cancel and the earlier goroutine will serve as the timeout for the latest call to start.

@paulwe paulwe force-pushed the timer-reuse branch 2 times, most recently from 415e0bb to 9fb4b50 Compare April 16, 2024 05:00
@paulwe paulwe merged commit e90e787 into master Apr 16, 2024
15 checks passed
@paulwe paulwe deleted the timer-reuse branch April 16, 2024 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants