-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add support for multiple subscribers to CRI container events #9661
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
AkihiroSuda
merged 2 commits into
containerd:main
from
dmcgowan:update-cri-container-events-multisub
Jan 27, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
Copyright The containerd Au 8000 thors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package eventq | ||
|
||
import ( | ||
"io" | ||
"time" | ||
) | ||
|
||
type EventQueue[T any] struct { | ||
events chan<- T | ||
subscriberC chan<- eventSubscription[T] | ||
shutdownC chan struct{} | ||
} | ||
|
||
type eventSubscription[T any] struct { | ||
c chan<- T | ||
closeC chan struct{} | ||
} | ||
|
||
func (sub eventSubscription[T]) publish(event T) bool { | ||
select { | ||
case <-sub.closeC: | ||
return false | ||
case sub.c <- event: | ||
return true | ||
} | ||
} | ||
|
||
func (sub eventSubscription[T]) Close() error { | ||
select { | ||
case <-sub.closeC: | ||
default: | ||
close(sub.closeC) | ||
} | ||
return nil | ||
} | ||
|
||
// New provides a queue for sending messages to one or more | ||
// subscribers. Messages are held for the given discardAfter duration | ||
// if there are no subscribers. | ||
func New[T any](discardAfter time.Duration, discardFn func(T)) EventQueue[T] { | ||
events := make(chan T) | ||
subscriberC := make(chan eventSubscription[T]) | ||
shutdownC := make(chan struct{}) | ||
|
||
go func() { | ||
type queuedEvent struct { | ||
event T | ||
discardAt time.Time | ||
} | ||
|
||
var discardQueue []queuedEvent | ||
var discardTime <-chan time.Time | ||
var subscribers []eventSubscription[T] | ||
for { | ||
select { | ||
case <-shutdownC: | ||
for _, event := range discardQueue { | ||
discardFn(event.event) | ||
} | ||
for _, sub := range subscribers { | ||
close(sub.c) | ||
} | ||
return | ||
case event := <-events: | ||
if len(subscribers) > 0 { | ||
active := subscribers[:0] | ||
for _, sub := range subscribers { | ||
if sub.publish(event) { | ||
active = append(active, sub) | ||
} | ||
} | ||
subscribers = active | ||
} | ||
if len(subscribers) == 0 { | ||
discardQueue = append(discardQueue, queuedEvent{ | ||
event: event, | ||
discardAt: time.Now().Add(discardAfter), | ||
}) | ||
if discardTime == nil { | ||
discardTime = time.After(time.Until(discardQueue[0].discardAt).Abs()) | ||
} | ||
} | ||
case s := <-subscriberC: | ||
var closed bool | ||
for i, event := range discardQueue { | ||
if !s.publish(event.event) { | ||
discardQueue = discardQueue[i:] | ||
closed = true | ||
break | ||
} | ||
} | ||
if !closed { | ||
discardQueue = nil | ||
discardTime = nil | ||
subscribers = append(subscribers, s) | ||
} | ||
case t := <-discardTime: | ||
toDiscard := discardQueue | ||
discardQueue = nil | ||
for i, event := range toDiscard { | ||
if t.After(event.discardAt) { | ||
discardFn(event.event) | ||
} else { | ||
discardQueue = toDiscard[i:] | ||
break | ||
} | ||
} | ||
if len(discardQueue) == 0 { | ||
discardTime = nil | ||
} else { | ||
// Wait until next item in discard queue plus a small buffer to collect a burst of events | ||
discardTime = time.After(time.Until(discardQueue[0].discardAt).Abs() + 10*time.Millisecond) | ||
} | ||
} | ||
|
||
} | ||
}() | ||
|
||
return EventQueue[T]{ | ||
events: events, | ||
subscriberC: subscriberC, | ||
shutdownC: shutdownC, | ||
} | ||
} | ||
|
||
func (eq *EventQueue[T]) Shutdown() { | ||
defer close(eq.shutdownC) | ||
eq.shutdownC <- struct{}{} | ||
} | ||
|
||
func (eq *EventQueue[T]) Send(event T) { | ||
select { | ||
case <-eq.shutdownC: | ||
case eq.events <- event: | ||
} | ||
} | ||
|
||
func (eq *EventQueue[T]) Subscribe() (<-chan T, io.Closer) { | ||
c := make(chan T, 100) | ||
subscription := eventSubscription[T]{ | ||
c: c, | ||
closeC: make(chan struct{}), | ||
} | ||
eq.subscriberC <- subscription | ||
|
||
return c, subscription | ||
} |
Oops, something went wrong.
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.
publish
will be blocked ifsub.c
is full. So one consumer may block the others from receiving updates. Is it acceptable?Uh oh!
There was an error while loading. Please reload this page.
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.
We could consider adding a log after a timeout if this condition is hit, but retry after. Reliably and efficiently sending events to multiple subscribers can get very sophisticated. Much of that sophistication is probably unnecessary, but it would be good to have visibility into any case which is blocking.
My hypothesis is that if the subscriber is active, then the 100 event buffer is more than enough for it to keep up with the the flow of events. Previously there was a single 1000 event buffer that would fill up when there were no subscribers to pull them out of the channel, I don't believe there was a case where there was an active subscriber and the channel still filled up. It would be good to know that though.