forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
monitor: new listener v1.2 with reusing en/decoder
We previously encoded the payload with a new gob encoder per message. This had a significant overhead because gob introspected and generated type information for the encoding. This increased CPU usage and the amount of data sent to listeners. The new v1.2 encoding scheme reuses a gob encoder per listener, thus only sending the type information once per session. Signed-off-by: Ray Bejjani <ray@covalent.io>
- Loading branch information
1 parent
c2011e6
commit b88afa2
Showing
9 changed files
with
208 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,80 @@ | ||
// Copyright 2018 Authors of Cilium | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"encoding/gob" | ||
"net" | ||
|
||
"github.com/cilium/cilium/monitor/listener" | ||
"github.com/cilium/cilium/monitor/payload" | ||
) | ||
|
||
// listenerv1_2 implements the ciliim-node-monitor API protocol compatible with | ||
// cilium 1.2 | ||
// cleanupFn is called on exit | ||
type listenerv1_2 struct { | ||
conn net.Conn | ||
queue chan *payload.Payload | ||
cleanupFn func(listener.MonitorListener) | ||
} | ||
|
||
func newListenerv1_2(c net.Conn, queueSize int, cleanupFn func(listener.MonitorListener)) *listenerv1_2 { | ||
ml := &listenerv1_2{ | ||
conn: c, | ||
queue: make(chan *payload.Payload, queueSize), | ||
cleanupFn: cleanupFn, | ||
} | ||
|
||
go ml.drainQueue() | ||
|
||
return ml | ||
} | ||
|
||
func (ml *listenerv1_2) Enqueue(pl *payload.Payload) { | ||
select { | ||
case ml.queue <- pl: | ||
default: | ||
log.Debugf("Per listener queue is full, dropping message") | ||
} | ||
} | ||
|
||
// drainQueue encodes and sends monitor payloads to the listener. It is | ||
// intended to be a goroutine. | ||
func (ml *listenerv1_2) drainQueue() { | ||
defer func() { | ||
ml.conn.Close() | ||
ml.cleanupFn(ml) | ||
}() | ||
|
||
enc := gob.NewEncoder(ml.conn) | ||
for pl := range ml.queue { | ||
if err := pl.EncodeBinary(enc); err != nil { | ||
switch { | ||
case listener.IsDisconnected(err): | ||
log.Info("Listener disconnected") | ||
return | ||
|
||
default: | ||
log.WithError(err).Warn("Removing listener due to write failure") | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (ml *listenerv1_2) Version() listener.Version { | ||
return listener.Version1_2 | ||
} |
This file contains 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 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 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 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 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