-
Notifications
You must be signed in to change notification settings - Fork 889
feat: add WaitUntilEmpty to LogSender #12159
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
Merged
Merged
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
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -437,6 +437,7 @@ func (l *LogSender) SendLoop(ctx context.Context, dest logDest) error { | |||||||||||
l.exceededLogLimit = true | ||||||||||||
// no point in keeping anything we have queued around, server will not accept them | ||||||||||||
l.queues = make(map[uuid.UUID]*logQueue) | ||||||||||||
l.Broadcast() // might unblock WaitUntilEmpty | ||||||||||||
return LogLimitExceededError | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
@@ -451,6 +452,7 @@ func (l *LogSender) SendLoop(ctx context.Context, dest logDest) error { | |||||||||||
if len(q.logs) == 0 { | ||||||||||||
// no empty queues | ||||||||||||
delete(l.queues, src) | ||||||||||||
l.Broadcast() // might unblock WaitUntilEmpty | ||||||||||||
continue | ||||||||||||
} | ||||||||||||
q.lastFlush = time.Now() | ||||||||||||
|
@@ -487,6 +489,34 @@ func (l *LogSender) GetScriptLogger(logSourceID uuid.UUID) ScriptLogger { | |||||||||||
return ScriptLogger{srcID: logSourceID, sender: l} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// WaitUntilEmpty waits until the LogSender's queues are empty or the given context expires. | ||||||||||||
func (l *LogSender) WaitUntilEmpty(ctx context.Context) error { | ||||||||||||
ctxDone := false | ||||||||||||
nevermind := make(chan struct{}) | ||||||||||||
defer close(nevermind) | ||||||||||||
go func() { | ||||||||||||
select { | ||||||||||||
case <-ctx.Done(): | ||||||||||||
l.L.Lock() | ||||||||||||
defer l.L.Unlock() | ||||||||||||
ctxDone = true | ||||||||||||
l.Broadcast() | ||||||||||||
return | ||||||||||||
case <-nevermind: | ||||||||||||
return | ||||||||||||
} | ||||||||||||
}() | ||||||||||||
l.L.Lock() | ||||||||||||
defer l.L.Unlock() | ||||||||||||
for len(l.queues) != 0 && !ctxDone { | ||||||||||||
l.Wait() | ||||||||||||
} | ||||||||||||
if len(l.queues) == 0 { | ||||||||||||
return nil | ||||||||||||
} | ||||||||||||
return ctx.Err() | ||||||||||||
Comment on lines
+514
to
+517
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.
Suggested change
We don't actually need this check, this way we give priority to the context cancellation, even if we happen to be done at the same time (this can be preferable in some cases). |
||||||||||||
} | ||||||||||||
|
||||||||||||
type ScriptLogger struct { | ||||||||||||
sender *LogSender | ||||||||||||
srcID uuid.UUID | ||||||||||||
|
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
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.
Why are we duplicating logic from
SendLoop
here? Since this method doesn't attempt to send, it's quite pointless unless the signaling is happening from a runningSendLoop
anyway.Edit: Ah, nevermind, just realized this is only here to handle the user provided context.
I think this could be greatly simplified:
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.
The problem with an
allSent
channel is how to arrange when it should read. Closing the channel won't work, because you can't "unclose" it if more data gets queued.Writing to the channel won't work if there are more than one caller to WaitUntilEmpty.
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.
True, it would work better/simpler if the send loop was channel-based as well. In that case, one approach could be this:
But it's not quite as nice when retrofitted into the mutex style loop.
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.
The problem there is that it requires
SendLoop
to actually be running in order forWaitUntilEmpty()
to return, which it might not be but we are still empty.Channels are great for communicating between goroutines. Here what we really, actually want is to know when a condition is satisfied, regardless of other running goroutines, and for that
sync.Cond
is your friend.