github.com/fafucoder/cilium@v1.6.11/pkg/monitor/agent/listener/listener.go (about)

     1  // Copyright 2018 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 listener
    16  
    17  import (
    18  	"net"
    19  	"os"
    20  	"syscall"
    21  
    22  	"github.com/cilium/cilium/pkg/monitor/payload"
    23  )
    24  
    25  // Version is the version of a node-monitor listener client. There are
    26  // two API versions:
    27  // - 1.0 which encodes the gob type information with each payload sent, and
    28  //   adds a meta object before it.
    29  // - 1.2 which maintains a gob session per listener, thus only encoding the
    30  //   type information on the first payload sent. It does NOT prepend the a meta
    31  //   object.
    32  type Version string
    33  
    34  const (
    35  	// VersionUnsupported is here for use in error returns etc.
    36  	VersionUnsupported = Version("unsupported")
    37  
    38  	// Version1_0 is the API 1.0 version of the protocol (see above).
    39  	Version1_0 = Version("1.0")
    40  
    41  	// Version1_2 is the API 1.0 version of the protocol (see above).
    42  	Version1_2 = Version("1.2")
    43  )
    44  
    45  // MonitorListener is a generic consumer of monitor events. Implementers are
    46  // expected to handle errors as needed, including exiting.
    47  type MonitorListener interface {
    48  	// Enqueue adds this payload to the send queue. Any errors should be logged
    49  	// and handled appropriately.
    50  	Enqueue(pl *payload.Payload)
    51  
    52  	// Version returns the API version of this listener
    53  	Version() Version
    54  }
    55  
    56  // IsDisconnected is a convenience function that wraps the absurdly long set of
    57  // checks for a disconnect.
    58  func IsDisconnected(err error) bool {
    59  	if err == nil {
    60  		return false
    61  	}
    62  
    63  	op, ok := err.(*net.OpError)
    64  	if !ok {
    65  		return false
    66  	}
    67  
    68  	syscerr, ok := op.Err.(*os.SyscallError)
    69  	if !ok {
    70  		return false
    71  	}
    72  
    73  	errn := syscerr.Err.(syscall.Errno)
    74  	return errn == syscall.EPIPE
    75  }