2019-08-07 19:44:33 +02:00
|
|
|
// Package tunnel provides gre network tunnelling
|
2019-08-05 18:52:57 +02:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2019-09-04 10:48:05 +02:00
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
2019-08-05 18:52:57 +02:00
|
|
|
"github.com/micro/go-micro/transport"
|
|
|
|
)
|
|
|
|
|
2019-10-15 16:40:04 +02:00
|
|
|
const (
|
2019-10-15 16:55:08 +02:00
|
|
|
// send over one link
|
2019-10-15 16:40:04 +02:00
|
|
|
Unicast Mode = iota
|
2019-10-15 16:55:08 +02:00
|
|
|
// send to all channel listeners
|
2019-10-15 16:40:04 +02:00
|
|
|
Multicast
|
2019-10-15 17:08:38 +02:00
|
|
|
// send to all links
|
2019-10-15 16:40:04 +02:00
|
|
|
Broadcast
|
|
|
|
)
|
|
|
|
|
2019-09-04 13:00:11 +02:00
|
|
|
var (
|
|
|
|
// DefaultDialTimeout is the dial timeout if none is specified
|
|
|
|
DefaultDialTimeout = time.Second * 5
|
2019-09-05 19:13:02 +02: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 20:57:41 +02:00
|
|
|
// ErrLinkNotFound is returned when a link is specified at dial time and does not exist
|
|
|
|
ErrLinkNotFound = errors.New("link not found")
|
2019-09-04 13:00:11 +02:00
|
|
|
)
|
|
|
|
|
2019-10-15 16:55:08 +02:00
|
|
|
// Mode of the session
|
2019-10-15 16:40:04 +02:00
|
|
|
type Mode uint8
|
|
|
|
|
2019-08-31 18:32:20 +02:00
|
|
|
// Tunnel creates a gre tunnel on top of the go-micro/transport.
|
|
|
|
// It establishes multiple streams using the Micro-Tunnel-Channel header
|
2019-08-07 19:44:33 +02:00
|
|
|
// and Micro-Tunnel-Session header. The tunnel id is a hash of
|
|
|
|
// the address being requested.
|
2019-08-05 18:52:57 +02:00
|
|
|
type Tunnel interface {
|
2019-08-07 22:58:25 +02:00
|
|
|
Init(opts ...Option) error
|
2019-08-21 13:55:10 +02:00
|
|
|
// Address the tunnel is listening on
|
|
|
|
Address() string
|
2019-08-05 18:52:57 +02:00
|
|
|
// Connect connects the tunnel
|
|
|
|
Connect() error
|
|
|
|
// Close closes the tunnel
|
|
|
|
Close() error
|
2019-09-11 21:49:27 +02:00
|
|
|
// All the links the tunnel is connected to
|
|
|
|
Links() []Link
|
2019-08-30 21:05:00 +02:00
|
|
|
// Connect to a channel
|
2019-09-04 10:48:05 +02:00
|
|
|
Dial(channel string, opts ...DialOption) (Session, error)
|
2019-08-30 21:05:00 +02:00
|
|
|
// Accept connections on a channel
|
2019-10-15 16:40:04 +02:00
|
|
|
Listen(channel string, opts ...ListenOption) (Listener, error)
|
2019-08-20 18:21:35 +02:00
|
|
|
// Name of the tunnel implementation
|
|
|
|
String() string
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 16:41:57 +02:00
|
|
|
// Link represents internal links to the tunnel
|
|
|
|
type Link interface {
|
|
|
|
// The id of the link
|
|
|
|
Id() string
|
2019-10-22 17:53:47 +02:00
|
|
|
// Delay is the current load on the link (lower is better)
|
2019-10-22 17:50:00 +02:00
|
|
|
Delay() int64
|
|
|
|
// Length returns the roundtrip time as nanoseconds (lower is better)
|
|
|
|
Length() int64
|
2019-10-22 18:02:22 +02:00
|
|
|
// Current transfer rate as bits per second (lower is better)
|
|
|
|
Rate() float64
|
2019-10-25 15:16:22 +02:00
|
|
|
// State of the link e.g connected/closed
|
|
|
|
State() string
|
2019-09-04 16:41:57 +02:00
|
|
|
// honours transport socket
|
|
|
|
transport.Socket
|
|
|
|
}
|
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
// The listener provides similar constructs to the transport.Listener
|
|
|
|
type Listener interface {
|
2019-08-31 18:32:20 +02:00
|
|
|
Accept() (Session, error)
|
2019-08-30 21:05:00 +02:00
|
|
|
Channel() string
|
2019-08-07 19:44:33 +02:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
// Session is a unique session created when dialling or accepting connections on the tunnel
|
|
|
|
type Session interface {
|
2019-08-31 18:32:20 +02:00
|
|
|
// The unique session id
|
2019-08-07 19:44:33 +02:00
|
|
|
Id() string
|
2019-08-31 18:32:20 +02:00
|
|
|
// The channel name
|
2019-08-30 21:05:00 +02:00
|
|
|
Channel() string
|
2019-09-11 21:07:43 +02:00
|
|
|
// The link the session is on
|
|
|
|
Link() string
|
2019-08-07 19:44:33 +02:00
|
|
|
// a transport socket
|
|
|
|
transport.Socket
|
2019-08-05 20:41:48 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
// NewTunnel creates a new tunnel
|
2019-08-05 20:41:48 +02:00
|
|
|
func NewTunnel(opts ...Option) Tunnel {
|
|
|
|
return newTunnel(opts...)
|
2019-08-05 18:52:57 +02:00
|
|
|
}
|