micro/network/tunnel/options.go

193 lines
3.8 KiB
Go
Raw Permalink Normal View History

package tunnel
import (
"time"
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/meter"
"go.unistack.org/micro/v4/network/transport"
"go.unistack.org/micro/v4/tracer"
"go.unistack.org/micro/v4/util/id"
)
var (
// DefaultAddress is default tunnel bind address
2019-08-21 14:55:10 +03:00
DefaultAddress = ":0"
// DefaultToken the shared default token
2019-11-29 20:27:29 +03:00
DefaultToken = "go.micro.tunnel"
)
// Option func signature
type Option func(*Options)
// Options provides network configuration options
type Options struct {
// Logger used for logging
Logger logger.Logger
// Meter used for metrics
Meter meter.Meter
// Tracer used for tracing
Tracer tracer.Tracer
// Transport used for communication
Transport transport.Transport
// Token the shared auth token
Token string
// Name holds the tunnel name
Name string
// ID holds the tunnel id
ID string
// Address holds the tunnel address
Address string
// Nodes holds the tunnel nodes
Nodes []string
}
// DialOption func
type DialOption func(*DialOptions)
// DialOptions provides dial options
type DialOptions struct {
2019-09-11 21:57:41 +03:00
// Link specifies the link to use
Link string
2019-10-15 17:40:04 +03:00
// specify mode of the session
Mode Mode
2019-12-11 17:37:03 +03:00
// Wait for connection to be accepted
Wait bool
// the dial timeout
Timeout time.Duration
}
// ListenOption func
2019-10-15 17:40:04 +03:00
type ListenOption func(*ListenOptions)
// ListenOptions provides listen options
2019-10-15 17:40:04 +03:00
type ListenOptions struct {
// Mode specify mode of the session
2019-10-15 17:40:04 +03:00
Mode Mode
// Timeout the read timeout
2019-12-08 02:28:39 +03:00
Timeout time.Duration
2019-10-15 17:40:04 +03:00
}
// ID sets the tunnel id
func ID(id string) Option {
return func(o *Options) {
o.ID = id
}
}
// Logger sets the logger
func Logger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
// Meter sets the meter
func Meter(m meter.Meter) Option {
return func(o *Options) {
o.Meter = m
}
}
// Address sets the tunnel address
func Address(a string) Option {
return func(o *Options) {
o.Address = a
}
}
// Nodes specify remote network nodes
2019-08-07 20:44:33 +03:00
func Nodes(n ...string) Option {
return func(o *Options) {
o.Nodes = n
}
}
2019-08-30 22:05:00 +03:00
// Token sets the shared token for auth
func Token(t string) Option {
return func(o *Options) {
o.Token = t
}
}
// Transport listens for incoming connections
func Transport(t transport.Transport) Option {
return func(o *Options) {
o.Transport = t
}
}
// ListenMode option
2019-10-15 18:08:38 +03:00
func ListenMode(m Mode) ListenOption {
2019-10-15 17:40:04 +03:00
return func(o *ListenOptions) {
2019-10-15 18:08:38 +03:00
o.Mode = m
2019-10-15 17:40:04 +03:00
}
}
// ListenTimeout for reads and writes on the listener session
2019-12-08 02:28:39 +03:00
func ListenTimeout(t time.Duration) ListenOption {
return func(o *ListenOptions) {
o.Timeout = t
}
}
// DialMode multicast sets the multicast option to send only to those mapped
2019-10-15 18:08:38 +03:00
func DialMode(m Mode) DialOption {
return func(o *DialOptions) {
2019-10-15 18:08:38 +03:00
o.Mode = m
}
}
2019-09-11 21:57:41 +03:00
// DialTimeout sets the dial timeout of the connection
func DialTimeout(t time.Duration) DialOption {
return func(o *DialOptions) {
o.Timeout = t
}
}
2019-09-11 21:57:41 +03:00
// DialLink specifies the link to pin this connection to.
// This is not applicable if the multicast option is set.
func DialLink(id string) DialOption {
return func(o *DialOptions) {
o.Link = id
}
}
2019-12-08 02:28:39 +03:00
2019-12-11 17:37:03 +03:00
// DialWait specifies whether to wait for the connection
// to be accepted before returning the session
func DialWait(b bool) DialOption {
return func(o *DialOptions) {
o.Wait = b
}
}
// NewOptions returns router default options with filled values
func NewOptions(opts ...Option) Options {
options := Options{
ID: id.Must(),
Address: DefaultAddress,
Token: DefaultToken,
Logger: logger.DefaultLogger,
Meter: meter.DefaultMeter,
Tracer: tracer.DefaultTracer,
2019-12-08 02:28:39 +03:00
}
for _, o := range opts {
o(&options)
}
return options
2019-12-08 02:28:39 +03:00
}
// Tracer to be used for tracing
func Tracer(t tracer.Tracer) Option {
return func(o *Options) {
o.Tracer = t
}
}
// Name sets the name
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}