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

There is a rule in using go channel: never close a channel in the #45291

Changes from all commits
File filter

Filter by extension

Filter by extension

Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions pkg/kubelet/eviction/threshold_notifier_linux.go
Expand Up @@ -90,28 +90,32 @@ func NewMemCGThresholdNotifier(path, attribute, threshold, description string, h
}, nil }, nil
} }

func getThresholdEvents(eventfd int, eventCh chan<- int) { func getThresholdEvents(eventfd int, eventCh chan<- struct{}, stopCh <-chan struct{}) {
for { for {
buf := make([]byte, 8) buf := make([]byte, 8)
_, err := syscall.Read(eventfd, buf) _, err := syscall.Read(eventfd, buf)
if err != nil { if err != nil {
return return
} }
eventCh <- 0
select {
case eventCh <- struct{}{}:
case <-stopCh:
} }
} }

func (n *memcgThresholdNotifier) Start(stopCh <-chan struct{}) { func (n *memcgThresholdNotifier) Start(stopCh <-chan struct{}) {
eventCh := make(chan int, 1) eventCh := make(chan struct{})
go getThresholdEvents(n.eventfd, eventCh) go getThresholdEvents(n.eventfd, eventCh, stopCh)
for { for {
select { select {
case <-stopCh: case <-stopCh:
glog.V(2).Infof("eviction: stopping threshold notifier") glog.V(2).Infof("eviction: stopping threshold notifier")
syscall.Close(n.watchfd) syscall.Close(n.watchfd)
syscall.Close(n.controlfd) syscall.Close(n.controlfd)
syscall.Close(n.eventfd) syscall.Close(n.eventfd)
return return
case <-eventCh: case <-eventCh:
glog.V(2).Infof("eviction: threshold crossed") glog.V(2).Infof("eviction: threshold crossed")
Expand Down