micro/network/transport/options.go

176 lines
3.3 KiB
Go
Raw Permalink Normal View History

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"
"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 03:28:57 +03:00
)
// Options struct holds the transport options
2016-01-17 03:28:57 +03:00
type Options struct {
// Meter used for metrics
Meter meter.Meter
// Tracer used for tracing
Tracer tracer.Tracer
// Codec used for marshal/unmarshal messages
Codec codec.Codec
// Logger used for logging
Logger logger.Logger
// Context holds external options
2016-01-17 03:28:57 +03:00
Context context.Context
// 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 03:28:57 +03:00
}
// NewOptions returns new options
func NewOptions(opts ...Option) Options {
options := Options{
Logger: logger.DefaultLogger,
Meter: meter.DefaultMeter,
Tracer: tracer.DefaultTracer,
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
return options
}
// DialOptions struct
2016-01-17 03:28:57 +03:00
type DialOptions struct {
// Context holds the external options
2016-01-18 03:10:04 +03:00
Context context.Context
// Timeout holds the timeout
Timeout time.Duration
// Stream flag
Stream bool
2016-01-18 03:10:04 +03:00
}
// NewDialOptions returns new DialOptions
func NewDialOptions(opts ...DialOption) DialOptions {
options := DialOptions{
Timeout: DefaultDialTimeout,
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
return options
}
// ListenOptions struct
2016-01-18 03:10:04 +03:00
type ListenOptions struct {
// TODO: add tls options when listening
// Currently set in global options
// Context holds the external options
2016-01-17 03:28:57 +03:00
Context context.Context
// TLSConfig holds the *tls.Config options
TLSConfig *tls.Config
2016-01-17 03:28:57 +03:00
}
// NewListenOptions returns new ListenOptions
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
}
}
// 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
}
}
// Context sets the context
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
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
// TLSConfig to be used for the transport.
func TLSConfig(t *tls.Config) Option {
return func(o *Options) {
o.TLSConfig = t
}
}
// 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
}
}
// 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
}
}
// 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
}
}