github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/memberlist/transport.go (about) 1 package memberlist 2 3 import ( 4 "fmt" 5 "net" 6 "time" 7 ) 8 9 //go:generate mockgen -destination ./mock/mock_transport.go -package mock -source=./transport.go 10 11 // Packet is used to provide some metadata about incoming packets from peers 12 // over a packet connection, as well as the packet payload. 13 type Packet struct { 14 // Buf has the raw contents of the packet. 15 Buf []byte 16 17 // From has the address of the peer. This is an actual net.Addr so we 18 // can expose some concrete details about incoming packets. 19 From net.Addr 20 21 // Timestamp is the time when the packet was received. This should be 22 // taken as close as possible to the actual receipt time to help make an 23 // accurate RTT measurement during probes. 24 Timestamp time.Time 25 } 26 27 // Transport is used to abstract over communicating with other peers. The packet 28 // interface is assumed to be best-effort and the stream interface is assumed to 29 // be reliable. 30 type Transport interface { 31 // FinalAdvertiseAddr is given the user's configured values (which 32 // might be empty) and returns the desired IP and port to advertise to 33 // the rest of the cluster. 34 FinalAdvertiseAddr(ip string, port int) (string, int, error) 35 36 // WriteTo is a packet-oriented interface that fires off the given 37 // payload to the given address in a connectionless fashion. This should 38 // return a time stamp that's as close as possible to when the packet 39 // was transmitted to help make accurate RTT measurements during probes. 40 // 41 // This is similar to net.PacketConn, though we didn't want to expose 42 // that full set of required methods to keep assumptions about the 43 // underlying plumbing to a minimum. We also treat the address here as a 44 // string, similar to Dial, so it's network neutral, so this usually is 45 // in the form of "host:port". 46 WriteTo(b []byte, addr string) (time.Time, error) 47 48 // PacketCh returns a channel that can be read to receive incoming 49 // packets from other peers. How this is set up for listening is left as 50 // an exercise for the concrete transport implementations. 51 PacketCh() <-chan *Packet 52 53 // DialTimeout is used to create a connection that allows us to perform 54 // two-way communication with a peer. This is generally more expensive 55 // than packet connections so is used for more infrequent operations 56 // such as anti-entropy or fallback probes if the packet-oriented probe 57 // failed. 58 DialTimeout(addr string, timeout time.Duration) (net.Conn, error) 59 60 // StreamCh returns a channel that can be read to handle incoming stream 61 // connections from other peers. How this is set up for listening is 62 // left as an exercise for the concrete transport implementations. 63 StreamCh() <-chan net.Conn 64 65 // Shutdown is called when memberlist is shutting down; this gives the 66 // transport a chance to clean up any listeners. 67 Shutdown() error 68 } 69 70 type Address struct { 71 // Addr is a network address as a string, similar to Dial. This usually is 72 // in the form of "host:port". This is required. 73 Addr string 74 75 // Name is the name of the node being addressed. This is optional but 76 // transports may require it. 77 Name string 78 } 79 80 func (a *Address) String() string { 81 if a.Name != "" { 82 return fmt.Sprintf("%s (%s)", a.Name, a.Addr) 83 } 84 return a.Addr 85 } 86 87 // IngestionAwareTransport is not used. 88 // 89 // Deprecated: IngestionAwareTransport is not used and may be removed in a future 90 // version. Define the interface locally instead of referencing this exported 91 // interface. 92 type IngestionAwareTransport interface { 93 IngestPacket(conn net.Conn, addr net.Addr, now time.Time, shouldClose bool) error 94 IngestStream(conn net.Conn) error 95 } 96 97 type NodeAwareTransport interface { 98 Transport 99 WriteToAddress(b []byte, addr Address) (time.Time, error) 100 DialAddressTimeout(addr Address, timeout time.Duration) (net.Conn, error) 101 } 102 103 type shimNodeAwareTransport struct { 104 Transport 105 } 106 107 func NewshimNodeAwareTransport(t Transport) *shimNodeAwareTransport { 108 return &shimNodeAwareTransport{ 109 t, 110 } 111 } 112 113 var _ NodeAwareTransport = (*shimNodeAwareTransport)(nil) 114 115 func (t *shimNodeAwareTransport) WriteToAddress(b []byte, addr Address) (time.Time, error) { 116 return t.WriteTo(b, addr.Addr) 117 } 118 119 func (t *shimNodeAwareTransport) DialAddressTimeout(addr Address, timeout time.Duration) (net.Conn, error) { 120 return t.DialTimeout(addr.Addr, timeout) 121 }