-
Notifications
You must be signed in to change notification settings - Fork 338
PB-927: introduce Baton and BatonHolder #3722
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
Closed
zhming0
wants to merge
1
commit into
pb-927-update-agent-to-consume-the-new-connectrpc-endpoint
from
pb-927-baton
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package agent | ||
|
|
||
| // Baton coordinates exclusive access between multiple BatonHolders. | ||
| // At most one holder has the baton at any time. Holders interact through | ||
| // channels, making the baton composable with select statements. | ||
| type Baton struct { | ||
| ch chan struct{} | ||
| } | ||
|
|
||
| // NewBaton creates a new Baton. No one holds it initially. | ||
| func NewBaton() *Baton { | ||
| return &Baton{ch: make(chan struct{}, 1)} | ||
| } | ||
|
|
||
| // Holder returns a BatonHolder that does not initially hold the baton. | ||
| func (b *Baton) Holder() *BatonHolder { | ||
| return &BatonHolder{ch: b.ch} | ||
| } | ||
|
|
||
| // BatonHolder is a per-consumer handle to a Baton. It tracks whether this | ||
| // consumer currently holds the baton and provides idempotent Release. | ||
| type BatonHolder struct { | ||
| ch chan struct{} | ||
| held bool | ||
| } | ||
|
|
||
| // Acquire returns a channel that receives when the baton is available. | ||
| // | ||
| // Ideally, receiving from the channel and updating the holder's state would | ||
| // happen atomically. However, Go's select statement requires a bare channel | ||
| // for case expressions, so the caller must explicitly call Acquired after | ||
| // a successful receive: | ||
|
Comment on lines
+31
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is a key problem in trying to abstract this well. |
||
| // | ||
| // select { | ||
| // case <-holder.Acquire(): | ||
| // holder.Acquired() | ||
| // defer holder.Release() | ||
| // case <-ctx.Done(): | ||
| // } | ||
| func (h *BatonHolder) Acquire() <-chan struct{} { | ||
| return h.ch | ||
| } | ||
|
|
||
| // Acquired marks this holder as holding the baton. | ||
| // Must be called after successfully receiving from Acquire. | ||
| func (h *BatonHolder) Acquired() { | ||
| h.held = true | ||
| } | ||
|
|
||
| // Held reports whether this holder currently holds the baton. | ||
| func (h *BatonHolder) Held() bool { | ||
| return h.held | ||
| } | ||
|
|
||
| // Release makes the baton available for another holder to acquire. | ||
| // It is idempotent — calling Release when not holding is a no-op. | ||
| func (h *BatonHolder) Release() { | ||
| if !h.held { | ||
| return | ||
| } | ||
| h.held = false | ||
| h.ch <- struct{}{} | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This refactor seems to be the main win of this PR.