github.com/fafucoder/cilium@v1.6.11/pkg/monitor/agent/listener1_2.go (about) 1 // Copyright 2018-2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package agent 16 17 import ( 18 "encoding/gob" 19 "net" 20 21 "github.com/cilium/cilium/pkg/monitor/agent/listener" 22 "github.com/cilium/cilium/pkg/monitor/payload" 23 ) 24 25 // listenerv1_2 implements the ciliim-node-monitor API protocol compatible with 26 // cilium 1.2 27 // cleanupFn is called on exit 28 type listenerv1_2 struct { 29 conn net.Conn 30 queue chan *payload.Payload 31 cleanupFn func(listener.MonitorListener) 32 } 33 34 func newListenerv1_2(c net.Conn, queueSize int, cleanupFn func(listener.MonitorListener)) *listenerv1_2 { 35 ml := &listenerv1_2{ 36 conn: c, 37 queue: make(chan *payload.Payload, queueSize), 38 cleanupFn: cleanupFn, 39 } 40 41 go ml.drainQueue() 42 43 return ml 44 } 45 46 func (ml *listenerv1_2) Enqueue(pl *payload.Payload) { 47 select { 48 case ml.queue <- pl: 49 default: 50 log.Debug("Per listener queue is full, dropping message") 51 } 52 } 53 54 // drainQueue encodes and sends monitor payloads to the listener. It is 55 // intended to be a goroutine. 56 func (ml *listenerv1_2) drainQueue() { 57 defer func() { 58 ml.conn.Close() 59 ml.cleanupFn(ml) 60 }() 61 62 enc := gob.NewEncoder(ml.conn) 63 for pl := range ml.queue { 64 if err := pl.EncodeBinary(enc); err != nil { 65 switch { 66 case listener.IsDisconnected(err): 67 log.Debug("Listener disconnected") 68 return 69 70 default: 71 log.WithError(err).Warn("Removing listener due to write failure") 72 return 73 } 74 } 75 } 76 } 77 78 func (ml *listenerv1_2) Version() listener.Version { 79 return listener.Version1_2 80 }