2016-01-17 03:28:57 +03:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2016-01-18 03:10:04 +03:00
|
|
|
"crypto/tls"
|
2016-01-17 03:28:57 +03:00
|
|
|
"time"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2020-08-29 17:44:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2016-01-17 03:28:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
2019-07-07 14:23:03 +03:00
|
|
|
// Addrs is the list of intermediary addresses to connect to
|
|
|
|
Addrs []string
|
|
|
|
// Codec is the codec interface to use where headers are not supported
|
|
|
|
// by the transport and the entire payload must be encoded
|
2020-11-23 16:18:47 +03:00
|
|
|
Codec codec.Codec
|
2019-07-07 14:23:03 +03:00
|
|
|
// Secure tells the transport to secure the connection.
|
|
|
|
// In the case TLSConfig is not specified best effort self-signed
|
|
|
|
// certs should be used
|
|
|
|
Secure bool
|
|
|
|
// TLSConfig to secure the connection. The assumption is that this
|
|
|
|
// is mTLS keypair
|
2016-01-18 03:10:04 +03:00
|
|
|
TLSConfig *tls.Config
|
2016-08-01 18:31:27 +03:00
|
|
|
// Timeout sets the timeout for Send/Recv
|
|
|
|
Timeout time.Duration
|
2020-12-09 12:10:25 +03:00
|
|
|
// Logger sets the logger
|
2020-08-29 17:44:49 +03:00
|
|
|
Logger logger.Logger
|
2016-01-17 03:28:57 +03:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// NewOptions returns new options
|
2020-09-20 16:52:26 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// DialOptions struct
|
2016-01-17 03:28:57 +03:00
|
|
|
type DialOptions struct {
|
2019-07-07 14:23:03 +03:00
|
|
|
// Tells the transport this is a streaming connection with
|
|
|
|
// multiple calls to send/recv and that send may not even be called
|
|
|
|
Stream bool
|
|
|
|
// Timeout for dialing
|
2016-01-17 03:28:57 +03:00
|
|
|
Timeout time.Duration
|
|
|
|
|
2016-01-18 03:10:04 +03:00
|
|
|
// TODO: add tls options when dialling
|
|
|
|
// Currently set in global options
|
|
|
|
|
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// NewDialOptions returns new DialOptions
|
2020-09-20 16:52:26 +03:00
|
|
|
func NewDialOptions(opts ...DialOption) DialOptions {
|
|
|
|
options := DialOptions{
|
2020-11-06 11:18:12 +03:00
|
|
|
Timeout: DefaultDialTimeout,
|
2020-09-20 16:52:26 +03:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// ListenOptions struct
|
2016-01-18 03:10:04 +03:00
|
|
|
type ListenOptions struct {
|
|
|
|
// TODO: add tls options when listening
|
|
|
|
// Currently set in global options
|
|
|
|
|
2016-01-17 03:28:57 +03:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// NewListenOptions returns new ListenOptions
|
2020-09-20 16:52:26 +03:00
|
|
|
func NewListenOptions(opts ...ListenOption) ListenOptions {
|
|
|
|
options := ListenOptions{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2016-03-16 01:25:32 +03:00
|
|
|
// Addrs to use for transport
|
|
|
|
func Addrs(addrs ...string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Addrs = addrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// Context sets the context
|
2020-10-16 09:38:57 +03:00
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:56:57 +03:00
|
|
|
// Codec sets the codec used for encoding where the transport
|
|
|
|
// does not support message headers
|
2020-11-23 16:18:47 +03:00
|
|
|
func Codec(c codec.Codec) Option {
|
2016-12-06 21:56:57 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Codec = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 18:31:27 +03:00
|
|
|
// Timeout sets the timeout for Send/Recv execution
|
|
|
|
func Timeout(t time.Duration) Option {
|
2016-07-28 20:38:17 +03:00
|
|
|
return func(o *Options) {
|
2016-08-01 18:31:27 +03:00
|
|
|
o.Timeout = t
|
2016-07-28 20:38:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 03:10:04 +03:00
|
|
|
// Use secure communication. If TLSConfig is not specified we
|
|
|
|
// use InsecureSkipVerify and generate a self signed cert
|
|
|
|
func Secure(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Secure = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSConfig to be used for the transport.
|
|
|
|
func TLSConfig(t *tls.Config) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TLSConfig = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// WithStream indicates whether this is a streaming connection
|
2016-01-17 03:28:57 +03:00
|
|
|
func WithStream() DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Stream = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// WithTimeout used when dialling the remote side
|
2016-01-17 03:28:57 +03:00
|
|
|
func WithTimeout(d time.Duration) DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Timeout = d
|
|
|
|
}
|
|
|
|
}
|