1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/transport/options.go

162 lines
3.6 KiB
Go
Raw Normal View History

2016-01-17 02:28:57 +02:00
package transport
import (
2018-03-03 13:53:52 +02:00
"context"
2016-01-18 02:10:04 +02:00
"crypto/tls"
"net"
2016-01-17 02:28:57 +02:00
"time"
2021-10-12 13:55:53 +02:00
"go-micro.dev/v4/codec"
"go-micro.dev/v4/logger"
2016-01-17 02:28:57 +02:00
)
var (
DefaultBufSizeH2 = 4 * 1024 * 1024
)
2016-01-17 02:28:57 +02:00
type Options struct {
2019-07-07 13:23:03 +02: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
Codec codec.Marshaler
// 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 02:10:04 +02:00
TLSConfig *tls.Config
2016-08-01 17:31:27 +02:00
// Timeout sets the timeout for Send/Recv
Timeout time.Duration
2016-01-17 02:28:57 +02:00
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
// Logger is the underline logger
Logger logger.Logger
// BuffSizeH2 is the HTTP2 buffer size
BuffSizeH2 int
2016-01-17 02:28:57 +02:00
}
type DialOptions struct {
2019-07-07 13:23:03 +02: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 02:28:57 +02:00
Timeout time.Duration
// ConnClose sets the Connection header to close
ConnClose bool
// InsecureSkipVerify skip TLS verification.
InsecureSkipVerify bool
2016-01-17 02:28:57 +02:00
2022-09-30 16:27:07 +02:00
// TODO: add tls options when dialing
2016-01-18 02:10:04 +02:00
// Currently set in global options
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
}
type ListenOptions struct {
// TODO: add tls options when listening
// Currently set in global options
2016-01-17 02:28:57 +02:00
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
}
2022-09-30 16:27:07 +02:00
// Addrs to use for transport.
2016-03-16 00:25:32 +02:00
func Addrs(addrs ...string) Option {
return func(o *Options) {
o.Addrs = addrs
}
}
2016-12-06 20:56:57 +02:00
// Codec sets the codec used for encoding where the transport
2022-09-30 16:27:07 +02:00
// does not support message headers.
2019-01-10 11:42:02 +02:00
func Codec(c codec.Marshaler) Option {
2016-12-06 20:56:57 +02:00
return func(o *Options) {
o.Codec = c
}
}
2022-09-30 16:27:07 +02:00
// Timeout sets the timeout for Send/Recv execution.
2016-08-01 17:31:27 +02:00
func Timeout(t time.Duration) Option {
2016-07-28 19:38:17 +02:00
return func(o *Options) {
2016-08-01 17:31:27 +02:00
o.Timeout = t
2016-07-28 19:38:17 +02:00
}
}
2016-01-18 02:10:04 +02:00
// Use secure communication. If TLSConfig is not specified we
2022-09-30 16:27:07 +02:00
// use InsecureSkipVerify and generate a self signed cert.
2016-01-18 02:10:04 +02:00
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
}
}
2022-09-30 16:27:07 +02:00
// Indicates whether this is a streaming connection.
2016-01-17 02:28:57 +02:00
func WithStream() DialOption {
return func(o *DialOptions) {
o.Stream = true
}
}
func WithTimeout(d time.Duration) DialOption {
return func(o *DialOptions) {
o.Timeout = d
}
}
// WithConnClose sets the Connection header to close.
func WithConnClose() DialOption {
return func(o *DialOptions) {
o.ConnClose = true
}
}
func WithInsecureSkipVerify(b bool) DialOption {
return func(o *DialOptions) {
o.InsecureSkipVerify = b
}
}
// Logger sets the underline logger.
func Logger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
// BuffSizeH2 sets the HTTP2 buffer size.
// Default is 4 * 1024 * 1024.
func BuffSizeH2(size int) Option {
return func(o *Options) {
o.BuffSizeH2 = size
}
}
// InsecureSkipVerify sets the TLS options to skip verification.
2022-09-30 16:27:07 +02:00
// NetListener Set net.Listener for httpTransport.
func NetListener(customListener net.Listener) ListenOption {
return func(o *ListenOptions) {
if customListener == nil {
return
}
if o.Context == nil {
o.Context = context.TODO()
}
o.Context = context.WithValue(o.Context, netListener{}, customListener)
}
}