-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
monitor: Introduce channel to buffer notifications and listeners #2933
Conversation
9b5f617
to
833f26e
Compare
No functional change of code Signed-off-by: Thomas Graf <thomas@cilium.io>
833f26e
to
20d6702
Compare
monitor/launch/launcher.go
Outdated
@@ -96,3 +121,76 @@ func (nm *NodeMonitor) setState(state *models.MonitorStatus) { | |||
nm.state = state | |||
nm.Mutex.Unlock() | |||
} | |||
|
|||
func (nm *NodeMonitor) SendEvent(typ int, event interface{}) error { |
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.
exported method NodeMonitor.SendEvent should have comment or be unexported
* Makes SendEvent() lockless and non-blocking. Notifications are enqueued to a channel and in case the channel is full, the notification is dropped. * Lost notifications are accounted for and reported * The writing to the pipe is made via a single Write() call to maximise the chances that either none of the message buffer or all of it is written to the pipe. Signed-off-by: Thomas Graf <thomas@cilium.io>
Signed-off-by: Thomas Graf <thomas@cilium.io>
This decouples multiple readers and avoids blocking other readers if one of the readers can't keep it up. Signed-off-by: Thomas Graf <thomas@cilium.io>
20d6702
to
2061e70
Compare
test-me-please |
monitor/launch/launcher.go
Outdated
return fmt.Errorf("Unable to encode metadata: %s", err) | ||
} | ||
|
||
msgBuf := append(metaBuf, payloadBuf...) |
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.
Can you add a comment into this code? I'm failing to understand why you are appending payloadBuf
to metaBuf
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 meta header comes before the payload. I'll move it into a func.
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.
Fixed
|
||
var ( | ||
mutex lock.Mutex | ||
listeners = list.New() | ||
listeners = make(map[*monitorListener]struct{}) |
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.
Can't you use map[monitorListener]chan []byte
instead?
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.
What's the advantage of that? It's not extendable.
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.
I was trying to solve the problem of using a pointer as a key of a map. That can give problems when doing something like
func Foo(m monitorListener){
_, exists := listeners[&m]
// exists will always be false
}
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.
Sure, because it's a different address.
Signed-off-by: Thomas Graf <thomas@cilium.io>
test-me-please |
} | ||
|
||
select { | ||
case nm.queue <- append([]byte{byte(typ)}, buf.Bytes()...): |
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.
Isn't typ already in the event object? In either case, doesn't this break the API we're presenting current clients?
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.
Oh, nevermind. This typ is different than the one you replaced.
The current monitor code has two drawbacks:
sendEvent()
blocks until the notification has been read, this can block fast path operations such as policy regeneration or even processing of L7 requests.Changes:
to a channel and in case the channel is full, the notification is dropped.
chances that either none of the message buffer or all of it is written to
the pipe.