github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/network/tunnel/tunnel.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/network/tunnel/tunnel.go
    14  
    15  // Package tunnel provides gre network tunnelling
    16  package tunnel
    17  
    18  import (
    19  	"errors"
    20  	"time"
    21  
    22  	"github.com/tickoalcantara12/micro/v3/service/network/transport"
    23  )
    24  
    25  const (
    26  	// send over one link
    27  	Unicast Mode = iota
    28  	// send to all channel listeners
    29  	Multicast
    30  	// send to all links
    31  	Broadcast
    32  )
    33  
    34  var (
    35  	// DefaultDialTimeout is the dial timeout if none is specified
    36  	DefaultDialTimeout = time.Second * 5
    37  	// ErrDialTimeout is returned by a call to Dial where the timeout occurs
    38  	ErrDialTimeout = errors.New("dial timeout")
    39  	// ErrDiscoverChan is returned when we failed to receive the "announce" back from a discovery
    40  	ErrDiscoverChan = errors.New("failed to discover channel")
    41  	// ErrLinkNotFound is returned when a link is specified at dial time and does not exist
    42  	ErrLinkNotFound = errors.New("link not found")
    43  	// ErrLinkDisconnected is returned when a link we attempt to send to is disconnected
    44  	ErrLinkDisconnected = errors.New("link not connected")
    45  	// ErrLinkLoppback is returned when attempting to send an outbound message over loopback link
    46  	ErrLinkLoopback = errors.New("link is loopback")
    47  	// ErrLinkRemote is returned when attempting to send a loopback message over remote link
    48  	ErrLinkRemote = errors.New("link is remote")
    49  	// ErrReadTimeout is a timeout on session.Recv
    50  	ErrReadTimeout = errors.New("read timeout")
    51  	// ErrDecryptingData is for when theres a nonce error
    52  	ErrDecryptingData = errors.New("error decrypting data")
    53  )
    54  
    55  // Mode of the session
    56  type Mode uint8
    57  
    58  // Tunnel creates a gre tunnel on top of the go-micro/transport.
    59  // It establishes multiple streams using the Micro-Tunnel-Channel header
    60  // and Micro-Tunnel-Session header. The tunnel id is a hash of
    61  // the address being requested.
    62  type Tunnel interface {
    63  	// Init initializes tunnel with options
    64  	Init(opts ...Option) error
    65  	// Address returns the address the tunnel is listening on
    66  	Address() string
    67  	// Connect connects the tunnel
    68  	Connect() error
    69  	// Close closes the tunnel
    70  	Close() error
    71  	// Links returns all the links the tunnel is connected to
    72  	Links() []Link
    73  	// Dial allows a client to connect to a channel
    74  	Dial(channel string, opts ...DialOption) (Session, error)
    75  	// Listen allows to accept connections on a channel
    76  	Listen(channel string, opts ...ListenOption) (Listener, error)
    77  	// String returns the name of the tunnel implementation
    78  	String() string
    79  }
    80  
    81  // Link represents internal links to the tunnel
    82  type Link interface {
    83  	// Id returns the link unique Id
    84  	Id() string
    85  	// Delay is the current load on the link (lower is better)
    86  	Delay() int64
    87  	// Length returns the roundtrip time as nanoseconds (lower is better)
    88  	Length() int64
    89  	// Current transfer rate as bits per second (lower is better)
    90  	Rate() float64
    91  	// Is this a loopback link
    92  	Loopback() bool
    93  	// State of the link: connected/closed/error
    94  	State() string
    95  	// honours transport socket
    96  	transport.Socket
    97  }
    98  
    99  // The listener provides similar constructs to the transport.Listener
   100  type Listener interface {
   101  	Accept() (Session, error)
   102  	Channel() string
   103  	Close() error
   104  }
   105  
   106  // Session is a unique session created when dialling or accepting connections on the tunnel
   107  type Session interface {
   108  	// The unique session id
   109  	Id() string
   110  	// The channel name
   111  	Channel() string
   112  	// The link the session is on
   113  	Link() string
   114  	// a transport socket
   115  	transport.Socket
   116  }