2019-08-07 20:44:33 +03:00
|
|
|
// Package tunnel provides gre network tunnelling
|
2019-08-05 19:52:57 +03:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2020-11-03 02:02:32 +03:00
|
|
|
"context"
|
2019-09-04 11:48:05 +03:00
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
2020-09-20 16:57:54 +03:00
|
|
|
"github.com/unistack-org/micro/v3/network/transport"
|
2019-08-05 19:52:57 +03:00
|
|
|
)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// DefaultTunnel contains default tunnel implementation
|
|
|
|
var DefaultTunnel Tunnel
|
2020-08-28 11:52:51 +03:00
|
|
|
|
2019-10-15 17:40:04 +03:00
|
|
|
const (
|
2021-02-14 16:16:01 +03:00
|
|
|
// Unicast send over one link
|
2019-10-15 17:40:04 +03:00
|
|
|
Unicast Mode = iota
|
2021-02-14 16:16:01 +03:00
|
|
|
// Multicast send to all channel listeners
|
2019-10-15 17:40:04 +03:00
|
|
|
Multicast
|
2021-02-14 16:16:01 +03:00
|
|
|
// Broadcast send to all links
|
2019-10-15 17:40:04 +03:00
|
|
|
Broadcast
|
|
|
|
)
|
|
|
|
|
2019-09-04 14:00:11 +03:00
|
|
|
var (
|
|
|
|
// DefaultDialTimeout is the dial timeout if none is specified
|
|
|
|
DefaultDialTimeout = time.Second * 5
|
2019-09-05 20:13:02 +03:00
|
|
|
// ErrDialTimeout is returned by a call to Dial where the timeout occurs
|
|
|
|
ErrDialTimeout = errors.New("dial timeout")
|
|
|
|
// ErrDiscoverChan is returned when we failed to receive the "announce" back from a discovery
|
|
|
|
ErrDiscoverChan = errors.New("failed to discover channel")
|
2019-09-11 21:57:41 +03:00
|
|
|
// ErrLinkNotFound is returned when a link is specified at dial time and does not exist
|
|
|
|
ErrLinkNotFound = errors.New("link not found")
|
2020-01-13 23:06:02 +03:00
|
|
|
// ErrLinkDisconnected is returned when a link we attempt to send to is disconnected
|
|
|
|
ErrLinkDisconnected = errors.New("link not connected")
|
2021-02-14 16:16:01 +03:00
|
|
|
// ErrLinkLoopback is returned when attempting to send an outbound message over loopback link
|
2020-01-13 23:06:02 +03:00
|
|
|
ErrLinkLoopback = errors.New("link is loopback")
|
|
|
|
// ErrLinkRemote is returned when attempting to send a loopback message over remote link
|
|
|
|
ErrLinkRemote = errors.New("link is remote")
|
2019-12-08 02:28:39 +03:00
|
|
|
// ErrReadTimeout is a timeout on session.Recv
|
|
|
|
ErrReadTimeout = errors.New("read timeout")
|
2019-12-12 20:10:32 +03:00
|
|
|
// ErrDecryptingData is for when theres a nonce error
|
|
|
|
ErrDecryptingData = errors.New("error decrypting data")
|
2019-09-04 14:00:11 +03:00
|
|
|
)
|
|
|
|
|
2019-10-15 17:55:08 +03:00
|
|
|
// Mode of the session
|
2019-10-15 17:40:04 +03:00
|
|
|
type Mode uint8
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
// Tunnel creates a gre tunnel on top of the micro/transport.
|
2019-08-31 19:32:20 +03:00
|
|
|
// It establishes multiple streams using the Micro-Tunnel-Channel header
|
2019-08-07 20:44:33 +03:00
|
|
|
// and Micro-Tunnel-Session header. The tunnel id is a hash of
|
|
|
|
// the address being requested.
|
2019-08-05 19:52:57 +03:00
|
|
|
type Tunnel interface {
|
2019-12-05 18:50:32 +03:00
|
|
|
// Init initializes tunnel with options
|
2019-08-07 23:58:25 +03:00
|
|
|
Init(opts ...Option) error
|
2019-12-05 18:50:32 +03:00
|
|
|
// Address returns the address the tunnel is listening on
|
2019-08-21 14:55:10 +03:00
|
|
|
Address() string
|
2019-08-05 19:52:57 +03:00
|
|
|
// Connect connects the tunnel
|
2020-11-03 02:02:32 +03:00
|
|
|
Connect(ctx context.Context) error
|
2019-08-05 19:52:57 +03:00
|
|
|
// Close closes the tunnel
|
2020-11-03 02:02:32 +03:00
|
|
|
Close(ctx context.Context) error
|
2019-12-05 18:50:32 +03:00
|
|
|
// Links returns all the links the tunnel is connected to
|
2019-09-11 22:49:27 +03:00
|
|
|
Links() []Link
|
2019-12-05 18:50:32 +03:00
|
|
|
// Dial allows a client to connect to a channel
|
2020-11-03 02:02:32 +03:00
|
|
|
Dial(ctx context.Context, channel string, opts ...DialOption) (Session, error)
|
2019-12-05 18:50:32 +03:00
|
|
|
// Listen allows to accept connections on a channel
|
2020-11-03 02:02:32 +03:00
|
|
|
Listen(ctx context.Context, channel string, opts ...ListenOption) (Listener, error)
|
2019-12-05 18:50:32 +03:00
|
|
|
// String returns the name of the tunnel implementation
|
2019-08-20 19:21:35 +03:00
|
|
|
String() string
|
2019-08-07 20:44:33 +03:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:41:57 +03:00
|
|
|
// Link represents internal links to the tunnel
|
|
|
|
type Link interface {
|
2019-12-05 18:50:32 +03:00
|
|
|
// Id returns the link unique Id
|
2019-09-04 17:41:57 +03:00
|
|
|
Id() string
|
2019-10-22 18:53:47 +03:00
|
|
|
// Delay is the current load on the link (lower is better)
|
2019-10-22 18:50:00 +03:00
|
|
|
Delay() int64
|
|
|
|
// Length returns the roundtrip time as nanoseconds (lower is better)
|
|
|
|
Length() int64
|
2019-10-22 19:02:22 +03:00
|
|
|
// Current transfer rate as bits per second (lower is better)
|
|
|
|
Rate() float64
|
2019-12-07 22:54:29 +03:00
|
|
|
// Is this a loopback link
|
|
|
|
Loopback() bool
|
|
|
|
// State of the link: connected/closed/error
|
2019-10-25 16:16:22 +03:00
|
|
|
State() string
|
2019-09-04 17:41:57 +03:00
|
|
|
// honours transport socket
|
|
|
|
transport.Socket
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Listener provides similar constructs to the transport.Listener
|
2019-08-07 20:44:33 +03:00
|
|
|
type Listener interface {
|
2019-08-31 19:32:20 +03:00
|
|
|
Accept() (Session, error)
|
2019-08-30 22:05:00 +03:00
|
|
|
Channel() string
|
2019-08-07 20:44:33 +03:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
// Session is a unique session created when dialling or accepting connections on the tunnel
|
|
|
|
type Session interface {
|
2019-08-31 19:32:20 +03:00
|
|
|
// The unique session id
|
2019-08-07 20:44:33 +03:00
|
|
|
Id() string
|
2019-08-31 19:32:20 +03:00
|
|
|
// The channel name
|
2019-08-30 22:05:00 +03:00
|
|
|
Channel() string
|
2019-09-11 22:07:43 +03:00
|
|
|
// The link the session is on
|
|
|
|
Link() string
|
2019-08-07 20:44:33 +03:00
|
|
|
// a transport socket
|
|
|
|
transport.Socket
|
2019-08-05 21:41:48 +03:00
|
|
|
}
|