gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/tunnel/tunnel.go (about) 1 // Package tunnel provides gre network tunnelling 2 package tunnel 3 4 import ( 5 "errors" 6 "time" 7 8 "gitee.com/liuxuezhan/go-micro-v1.18.0/transport" 9 ) 10 11 const ( 12 // send over one link 13 Unicast Mode = iota 14 // send to all channel listeners 15 Multicast 16 // send to all links 17 Broadcast 18 ) 19 20 var ( 21 // DefaultDialTimeout is the dial timeout if none is specified 22 DefaultDialTimeout = time.Second * 5 23 // ErrDialTimeout is returned by a call to Dial where the timeout occurs 24 ErrDialTimeout = errors.New("dial timeout") 25 // ErrDiscoverChan is returned when we failed to receive the "announce" back from a discovery 26 ErrDiscoverChan = errors.New("failed to discover channel") 27 // ErrLinkNotFound is returned when a link is specified at dial time and does not exist 28 ErrLinkNotFound = errors.New("link not found") 29 // ErrReadTimeout is a timeout on session.Recv 30 ErrReadTimeout = errors.New("read timeout") 31 ) 32 33 // Mode of the session 34 type Mode uint8 35 36 // Tunnel creates a gre tunnel on top of the go-micro/transport. 37 // It establishes multiple streams using the Micro-Tunnel-Channel header 38 // and Micro-Tunnel-Session header. The tunnel id is a hash of 39 // the address being requested. 40 type Tunnel interface { 41 // Init initializes tunnel with options 42 Init(opts ...Option) error 43 // Address returns the address the tunnel is listening on 44 Address() string 45 // Connect connects the tunnel 46 Connect() error 47 // Close closes the tunnel 48 Close() error 49 // Links returns all the links the tunnel is connected to 50 Links() []Link 51 // Dial allows a client to connect to a channel 52 Dial(channel string, opts ...DialOption) (Session, error) 53 // Listen allows to accept connections on a channel 54 Listen(channel string, opts ...ListenOption) (Listener, error) 55 // String returns the name of the tunnel implementation 56 String() string 57 } 58 59 // Link represents internal links to the tunnel 60 type Link interface { 61 // Id returns the link unique Id 62 Id() string 63 // Delay is the current load on the link (lower is better) 64 Delay() int64 65 // Length returns the roundtrip time as nanoseconds (lower is better) 66 Length() int64 67 // Current transfer rate as bits per second (lower is better) 68 Rate() float64 69 // Is this a loopback link 70 Loopback() bool 71 // State of the link: connected/closed/error 72 State() string 73 // honours transport socket 74 transport.Socket 75 } 76 77 // The listener provides similar constructs to the transport.Listener 78 type Listener interface { 79 Accept() (Session, error) 80 Channel() string 81 Close() error 82 } 83 84 // Session is a unique session created when dialling or accepting connections on the tunnel 85 type Session interface { 86 // The unique session id 87 Id() string 88 // The channel name 89 Channel() string 90 // The link the session is on 91 Link() string 92 // a transport socket 93 transport.Socket 94 } 95 96 // NewTunnel creates a new tunnel 97 func NewTunnel(opts ...Option) Tunnel { 98 return newTunnel(opts...) 99 }