- Notifications
You must be signed in to change notification settings - Fork928
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||||||
} | ||||||||||||
}() | ||||||||||||
Member 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. Why are we duplicating logic from Edit: Ah, nevermind, just realized this is only here to handle the user provided context. I think this could be greatly simplified: func (l*LogSender)WaitUntilEmpty(ctx context.Context)error {select {case<-ctx.Done():returnctx.Err()case<-l.allSent:returnnil}} 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. The problem with an 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 commentThe 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: func (l*LogSender)WaitUntilEmpty(ctx context.Context)error {wait:=make(chanstruct{})l.waitUntilEmpty<-waitselect {case<-ctx.Done():returnctx.Err()case<-wait:returnnil}}// SendLoopvarwaiters []chanstruct{}for {select {case<-tick:case<-l.waitUntilEmpty:waiters=append(waiters,wait)}// ...iflen(l.queues)==0 {for_,wait:=rangewaiters {close(wait)}waiters=nil} 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 commentThe reason will be displayed to describe this comment to others.Learn more. The problem there is that it requires Channels are great for communicating between goroutines. Here what we really, actually want is to know when acondition is satisfied, regardless of other running goroutines, and for that | ||||||||||||
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 | ||||||||||||