2016-01-17 00:28:57 +00:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2016-01-18 00:10:04 +00:00
|
|
|
"crypto/tls"
|
2016-01-17 00:28:57 +00:00
|
|
|
"time"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2016-01-17 00:28:57 +00:00
|
|
|
)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Options struct holds the transport options
|
2016-01-17 00:28:57 +00:00
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Meter used for metrics
|
2021-01-22 23:32:33 +03:00
|
|
|
Meter meter.Meter
|
2021-03-06 19:45:13 +03:00
|
|
|
// Tracer used for tracing
|
2021-01-22 23:32:33 +03:00
|
|
|
Tracer tracer.Tracer
|
2021-03-06 19:45:13 +03:00
|
|
|
// Codec used for marshal/unmarshal messages
|
|
|
|
Codec codec.Codec
|
|
|
|
// Logger used for logging
|
|
|
|
Logger logger.Logger
|
|
|
|
// Context holds external options
|
2016-01-17 00:28:57 +00:00
|
|
|
Context context.Context
|
2021-03-06 19:45:13 +03:00
|
|
|
// TLSConfig holds tls.TLSConfig options
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
// Name holds the transport name
|
|
|
|
Name string
|
|
|
|
// Addrs holds the transport addrs
|
|
|
|
Addrs []string
|
|
|
|
// Timeout holds the timeout
|
|
|
|
Timeout time.Duration
|
2016-01-17 00:28:57 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2021-01-22 23:32:33 +03:00
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
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
|
|
|
// DialOptions struct
|
2016-01-17 00:28:57 +00:00
|
|
|
type DialOptions struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds the external options
|
2016-01-18 00:10:04 +00:00
|
|
|
Context context.Context
|
2021-03-06 19:45:13 +03:00
|
|
|
// Timeout holds the timeout
|
|
|
|
Timeout time.Duration
|
|
|
|
// Stream flag
|
|
|
|
Stream bool
|
2016-01-18 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
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 00:10:04 +00:00
|
|
|
type ListenOptions struct {
|
|
|
|
// TODO: add tls options when listening
|
|
|
|
// Currently set in global options
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds the external options
|
2016-01-17 00:28:57 +00:00
|
|
|
Context context.Context
|
2021-03-06 19:45:13 +03:00
|
|
|
// TLSConfig holds the *tls.Config options
|
|
|
|
TLSConfig *tls.Config
|
2016-01-17 00:28:57 +00:00
|
|
|
}
|
|
|
|
|
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-15 22:25:32 +00: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Meter sets the meter
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 18:56:57 +00: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 18:56:57 +00:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Codec = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 16:31:27 +01:00
|
|
|
// Timeout sets the timeout for Send/Recv execution
|
|
|
|
func Timeout(t time.Duration) Option {
|
2016-07-28 18:38:17 +01:00
|
|
|
return func(o *Options) {
|
2016-08-01 16:31:27 +01:00
|
|
|
o.Timeout = t
|
2016-07-28 18:38:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 00:10:04 +00:00
|
|
|
// 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 00:28:57 +00: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 00:28:57 +00:00
|
|
|
func WithTimeout(d time.Duration) DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Timeout = d
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 23:32:33 +03:00
|
|
|
|
|
|
|
// Tracer to be used for tracing
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:07:35 +03:00
|
|
|
|
|
|
|
// Name sets the name
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|